package com.androidbegin.buttonclicktutorial;

import android.app.Activity;
import android.content.Context;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


import java.util.ArrayList;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
 

// http://www.androidhub4you.com/2012/10/custom-calendar-in-android.html
//https://github.com/nevalla/CalendarView/blob/master/src/com/examples/android/calendar/CalendarAdapter.java

// https://github.com/nevalla/CalendarView/blob/master/res/layout/calendar_item.xml
// https://github.com/nevalla/CalendarView
// http://androidlearnsteps.blogspot.fr/2014/09/custom-calendar-demo-in-android.html

//https://www.javacodegeeks.com/2013/08/android-custom-grid-view-example-with-image-and-text.html  ++++
	

//http://www.technotalkative.com/android-gridview-example/

// http://stacktips.com/tutorials/android/android-gridview-example-building-image-gallery-in-android

//http://androidexample.com/Custom_Grid_Layout_-_Android_Example/index.php?view=article_discription&aid=76

//http://www.learn-android-easily.com/2013/09/android-custom-gridview-example.html

public class GridAdapter extends ArrayAdapter 
{
   // private static final String TAG = GridAdapter.class.getSimpleName();
    private LayoutInflater mInflater;
    private List<Date> monthlyDates;
    private Calendar currentDate;
    //private List<EventObjects> allEvents;
    private List<Long> allEvents;
    
    Context context;
    Activity activity;
    
    //public GridAdapter(Context context, List<Date> monthlyDates, Calendar currentDate, List<EventObjects> allEvents) 
    //public GridAdapter(Context context, List<Date> monthlyDates, Calendar currentDate, List<Long> allEvents) 
    public GridAdapter(Context context, List<Date> monthlyDates, Calendar currentDate, List<Long> allEvents) 
     
    {
        super(context, R.layout.single_cell_layout);
        //super(activity);
       // this.context = context;
    //	super(context, monthlyDates, allEvents);
        this.monthlyDates = monthlyDates;
        this.currentDate = currentDate;
        this.allEvents = allEvents;
       // mInflater = LayoutInflater.from(context);
    }
    
    //@NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
    	
        Date mDate = monthlyDates.get(position);
        Calendar dateCal = Calendar.getInstance();
        dateCal.setTime(mDate);
        int dayValue = dateCal.get(Calendar.DAY_OF_MONTH);
        int displayMonth = dateCal.get(Calendar.MONTH) + 1;
        int displayYear = dateCal.get(Calendar.YEAR);
        int currentMonth = currentDate.get(Calendar.MONTH) + 1;
        int currentYear = currentDate.get(Calendar.YEAR);
        View view = convertView;
        
        if(view == null)
        {
            view = mInflater.inflate(R.layout.single_cell_layout, parent, false);
        }
        
        if(displayMonth == currentMonth && displayYear == currentYear)
        {
        	
            view.setBackgroundColor(Color.parseColor("#ebeef0")); // FF5733
        }
        else
        {
            view.setBackgroundColor(Color.parseColor("#cccccc"));
        }
        
        //Add day to calendar
        TextView cellNumber = (TextView)view.findViewById(R.id.calendar_date_id);
        cellNumber.setText(String.valueOf(dayValue));
        //Add events to the calendar
        TextView eventIndicator = (TextView)view.findViewById(R.id.event_id);
        Calendar eventCalendar = Calendar.getInstance();
       
        for(int i = 0; i < allEvents.size(); i++)
        {
        	
        	long timestamp = allEvents.get(i);
        	eventCalendar.setTimeInMillis(timestamp);
        	if(dayValue == eventCalendar.get(Calendar.DAY_OF_MONTH) && displayMonth == eventCalendar.get(Calendar.MONTH) + 1 && displayYear == eventCalendar.get(Calendar.YEAR))
        	{
                eventIndicator.setBackgroundColor(Color.parseColor("#FF4081"));
            }
        	
        	
        	
        	/*
        	String timestamp = allEvents.get(i).getTimestamp();
        	eventCalendar.setTimeInMillis(Long.parseLong(timestamp));
        	
        	if(dayValue == eventCalendar.get(Calendar.DAY_OF_MONTH) && displayMonth == eventCalendar.get(Calendar.MONTH) + 1 && displayYear == eventCalendar.get(Calendar.YEAR))
        	{
                eventIndicator.setBackgroundColor(Color.parseColor("#FF4081"));
            }
        	*/
        }
        
        /*
        for(int i = 0; i < allEvents.size(); i++)
        {
            eventCalendar.setTime(allEvents.get(i).getDate());
            if(dayValue == eventCalendar.get(Calendar.DAY_OF_MONTH) && displayMonth == eventCalendar.get(Calendar.MONTH) + 1
                    && displayYear == eventCalendar.get(Calendar.YEAR)){
                eventIndicator.setBackgroundColor(Color.parseColor("#FF4081"));
            }
        }
        */
        
        return view;
    }
    
    @Override
    public int getCount() 
    {
        return monthlyDates.size();
    }
    
    //@Nullable
    @Override
    public Object getItem(int position) 
    {
        return monthlyDates.get(position);
    }
    /*
    @Override
    public int getPosition(Object item)
    {
        return monthlyDates.indexOf(item);
    }*/

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
}