/*
 * Copyright (c) 2014 Personal-Health-Monitoring-System
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cse3310.phms.ui.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.cse3310.phms.R;
import com.cse3310.phms.model.Appointment;
import com.cse3310.phms.ui.activities.AddAppointmentActivity_;
import com.cse3310.phms.ui.cards.AppointmentCard;
import com.cse3310.phms.ui.utils.DatabaseHandler;
import com.cse3310.phms.ui.views.CardListDialogFragment_;
import com.squareup.timessquare.CalendarPickerView;
import it.gmariotti.cardslib.library.internal.Card;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.OptionsItem;
import org.androidannotations.annotations.ViewById;
import org.apache.commons.lang.time.DateUtils;

import java.util.*;

@EFragment(R.layout.appointment_screen)
public class AppointmentScreenFragment extends SherlockFragment implements CalendarPickerView.OnDateSelectedListener {
    private boolean isAddMode = false;

    @ViewById(R.id.calendar_view)
    CalendarPickerView mCalendarPickerView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @AfterViews
    void onSetupCalendarPickerView() {
        Calendar nextYear = Calendar.getInstance();
        nextYear.add(Calendar.MONTH, 2);

        Calendar minDate = Calendar.getInstance();
        minDate.add(Calendar.MONTH, -2);

        Date today = new Date();
        mCalendarPickerView.init(minDate.getTime(), nextYear.getTime()).withSelectedDate(today);
        mCalendarPickerView.setOnDateSelectedListener(this);

        Set<Date> dates = new HashSet<Date>();
        Log.d("aaaaaaaa", "" + dates.size());

        for (Appointment appointment : DatabaseHandler.getAllRows(Appointment.class)) {
            dates.add(new Date(appointment.getTime()));
        }

        mCalendarPickerView.highlightDates(dates);
    }



    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.clear();
        inflater.inflate(R.menu.add_menu, menu);
        inflater.inflate(R.menu.overflow_menu, menu);
    }

    @OptionsItem(R.id.add_icon)
    void menuAddAppointment() {
        Toast.makeText(getActivity(), "Select a date to add a new appointment.", Toast.LENGTH_LONG).show();
        isAddMode = true;
    }

    @Override
    public void onDateSelected(Date date) {
        if (isAddMode) {
            Intent intent = new Intent(getActivity(), AddAppointmentActivity_.class);
            intent.putExtra("date", date);
            startActivity(intent);

            isAddMode = false;
        } else {
            final List<Card> cardList = new ArrayList<Card>();
            cardList.clear();

            for (Appointment appointment : DatabaseHandler.getAllRows(Appointment.class)) {
                if (DateUtils.isSameDay(date, new Date(appointment.getTime()))) {
                    cardList.add(new AppointmentCard(getActivity(), appointment));
                }
            }

            if (!cardList.isEmpty()) {
                CardListDialogFragment_.newInstance(cardList).show(getFragmentManager(), "tag");
            }
        }
    }

    @Override
    public void onDateUnselected(Date date) {

    }
}

@@ -713,33 +713,8 @@
 le;%0A
-import android.util.Log;%0A
 impo
@@ -2035,24 +2035,75 @@
 ckerView() %7B
+%0A%0A    %7D%0A%0A    @Override%0A    public void onResume() %7B
 %0A        Cal
@@ -2108,24 +2108,23 @@
 alendar 
-nextYear
+maxDate
  = Calen
@@ -2150,24 +2150,23 @@
         
-nextYear
+maxDate
 .add(Cal
@@ -2304,24 +2304,85 @@
  new Date();
+%0A%0A        // set range of calendar and highlight current date
 %0A        mCa
@@ -2426,16 +2426,15 @@
 (), 
-nextYear
+maxDate
 .get
@@ -2540,93 +2540,96 @@
     
-Set%3CDate%3E dates = new HashSet%3CDate%3E();%0A        Log.d(%22aaaaaaaa%22, %22%22 + dates.size());%0A
+// get all dates that have an appointment%0A        Set%3CDate%3E dates = new HashSet%3CDate%3E();
 %0A   
@@ -2792,58 +2792,284 @@
     
-mCalendarPickerView.highlightDates(dates);%0A    %7D%0A%0A
+// highlight those dates that have a least one appointment%0A        mCalendarPickerView.highlightDates(dates);%0A        super.onResume();%0A    %7D%0A%0A    @Override%0A    public void onPause() %7B%0A        mCalendarPickerView.setOnDateSelectedListener(null);%0A        super.onPause();%0A    %7D
 %0A%0A  
@@ -3568,24 +3568,111 @@
 sAddMode) %7B%0A
+            // start an activity to add new appointment%0A            isAddMode = false;%0A
             
@@ -3823,25 +3823,24 @@
 ntent);%0A
-%0A
         
     isAd
@@ -3835,47 +3835,71 @@
     
-    isAddMode = false;%0A        %7D else %7B
+%7D else %7B // use has not click the add icon i.e. not in add mode
 %0A   
@@ -3985,24 +3985,95 @@
 t.clear();%0A%0A
+            // get appointment with the same date as the selected date%0A
             
@@ -4342,32 +4342,124 @@
 %0A            %7D%0A%0A
+            // open up a dialog with a list of appointments for dates that have appointment%0A
             if (

update highlight dates onResume()