package com.example.gridview03;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

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;


public class MainActivity extends Activity 
{
		GridView gridView;
		List<String> ItemsList;
		String selectedItem;
		TextView GridViewItems,BackSelectedItem;
		int backposition = -1;
		String[] itemsName = new String[]{
											 "1", "2", "3", "4", "5", "6", "7", 
											 "1", "2", "3", "4", "5", "6", "7",
											 "1", "2", "3", "4", "5", "6", "7",
											 "1", "2", "3", "4", "5", "6", "7",
											 "1", "2", "3", "4", "5", "6", "7",
											 "1", "2", "3", "4", "5", "6", "7" };
		
		
		 private ImageView previousButton, nextButton;
	     private static TextView currentDate;
	     private GridView calendarGridView;
	     private Button addEventButton, btn_agenda_view;
	     
	     private int MAX_CALENDAR_COLUMN = 42;
	     private SimpleDateFormat formatter = new SimpleDateFormat("MMMM yyyy", Locale.ENGLISH);
	     private Calendar cal = Calendar.getInstance(Locale.ENGLISH);
	     Map<String, String> dictionnaire = new HashMap<String, String>();
	     int shift_day_begin, shift_day_end;

	     List<Long> list_appointment; // = new ArrayList<Date>();
	     
	     private GridviewAdapter mAdapter;
	     
	     private GridviewAdapterbis mAdapterbis;
	     
	     
	     private ArrayList<String> listJours;
	     
	     private LayoutInflater mInflater;

	     
		
		 @Override
		 protected void onCreate(Bundle savedInstanceState)
		 {
			 	super.onCreate(savedInstanceState);
			 	setContentView(R.layout.activity_main);
			 	
			    // prepared arraylist and passed it to the Adapter class
			 	//List<String> List = new ArrayList<String>(Arrays.asList(items));
			 	
			 	prepareList();
			 	setupCalendar();
			 	
		        //mAdapter = new GridviewAdapter(this, listJours);
		        //gridView = (GridView) findViewById(R.id.calendar_grid);
		        //gridView.setAdapter(mAdapter);
			 	
		        /* OK */
			 	/*
			 	gridView = (GridView)findViewById(R.id.calendar_grid);
			 	ItemsList = new ArrayList<String>(Arrays.asList(itemsName));
			 	gridView.setAdapter(new TextAdapter(this));
			 	gridView.setOnItemClickListener(new OnItemClickListener() 
			 	{
			 		@Override
			 		public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			 		{
			 			selectedItem = parent.getItemAtPosition(position).toString();
			 			GridViewItems = (TextView) view;
			 			GridViewItems.setBackgroundColor(Color.parseColor("#814f00"));
			 			GridViewItems.setTextColor(Color.parseColor("#fdfcfa"));
			 			BackSelectedItem = (TextView) gridView.getChildAt(backposition);
			 			if (backposition != -1)
			 			{
			 				BackSelectedItem.setSelected(false);
			 				BackSelectedItem.setBackgroundColor(Color.parseColor("#fbdcbb"));
			 				BackSelectedItem.setTextColor(Color.parseColor("#040404"));
			 			}
			 			backposition = position;
			 		}
			 	});
		 		*/
		 }
		 
		 public void prepareList()
		    {
		    	  listJours = new ArrayList<String>();
		    	  
		    	  listJours.add("1");
		    	  listJours.add("2");
		    	  listJours.add("3");
		    	  listJours.add("4");
		    	  listJours.add("5");
		    	  listJours.add("6");
		    	  listJours.add("7");
		    	  
		    	  listJours.add("1");
		    	  listJours.add("2");
		    	  listJours.add("3");
		    	  listJours.add("4");
		    	  listJours.add("5");
		    	  listJours.add("6");
		    	  listJours.add("7");
		    	  
		    	  listJours.add("1");
		    	  listJours.add("2");
		    	  listJours.add("3");
		    	  listJours.add("4");
		    	  listJours.add("5");
		    	  listJours.add("6");
		    	  listJours.add("7");
		    	  
		    	  listJours.add("1");
		    	  listJours.add("2");
		    	  listJours.add("3");
		    	  listJours.add("4");
		    	  listJours.add("5");
		    	  listJours.add("6");
		    	  listJours.add("7");
		    	  
		    	  listJours.add("1");
		    	  listJours.add("2");
		    	  listJours.add("3");
		    	  listJours.add("4");
		    	  listJours.add("5");
		    	  listJours.add("6");
		    	  listJours.add("7");
		    	  
		    	  listJours.add("1");
		    	  listJours.add("2");
		    	  listJours.add("3");
		    	  listJours.add("4");
		    	  listJours.add("5");
		    	  listJours.add("6");
		    	  listJours.add("7");

		        
		    }         
		 
		 
		 private void initializeUILayout()
		 {
		        //LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		        //View view = inflater.inflate(R.layout.calendar_layout, this);
		        
		        //View view = getLayoutInflater().inflate(R.layout.calendar_layout, null);
		        
		        //previousButton = (ImageView)view.findViewById(R.id.previous_month);
		        //nextButton = (ImageView)view.findViewById(R.id.next_month);
		        //currentDate = (TextView)view.findViewById(R.id.display_current_date);
		        //addEventButton = (Button)view.findViewById(R.id.add_calendar_event);
		        //calendarGridView = (GridView)view.findViewById(R.id.calendar_grid);
		        //btn_agenda_view = (Button)view.findViewById(R.id.btn_agenda_view);

			 
			 
			 	previousButton = (ImageView)findViewById(R.id.previous_month);
		        nextButton = (ImageView)findViewById(R.id.next_month);
		        currentDate = (TextView)findViewById(R.id.display_current_date);
		        addEventButton = (Button)findViewById(R.id.add_calendar_event);
		        calendarGridView = (GridView)findViewById(R.id.calendar_grid);
		        btn_agenda_view = (Button)findViewById(R.id.btn_agenda_view);
		        
		 }
		 
		 
		  protected void setupCalendar()
		  {
		        List<Date> dayValueInCells = new ArrayList<Date>();
		        //mQuery = new DatabaseQuery(context);
		        //List<EventObjects> mEvents = mQuery.getAllFutureEvents();
		        
		       // List<EventObjects> mEvents = db.getAllFutureEvents();
		        
		        Calendar mCal = (Calendar)cal.clone();
		        mCal.set(Calendar.DAY_OF_MONTH, 1);
		        int firstDayOfTheMonth = mCal.get(Calendar.DAY_OF_WEEK) - 1;
		        mCal.add(Calendar.DAY_OF_MONTH, -firstDayOfTheMonth);
		        
		        while(dayValueInCells.size() < MAX_CALENDAR_COLUMN)
		        {
		            dayValueInCells.add(mCal.getTime());
		            mCal.add(Calendar.DAY_OF_MONTH, 1);
		        }
		        
		        Log.d("---->", "Number of date " + dayValueInCells.size());
		        String sDate = formatter.format(cal.getTime());
		        
		        //currentDate.setText(sDate);
		        
		       // mAdapter = new GridAdapter(context, dayValueInCells, cal, mEvents);
		       // calendarGridView.setAdapter(mAdapter);
		        
		                
		        String[] decoupe = sDate.split(" ");
		        dictionnaire.put("January", "0");
		        dictionnaire.put("February", "1");
		        dictionnaire.put("March", "2");
		        dictionnaire.put("April", "3");
		        dictionnaire.put("May", "4");
		        dictionnaire.put("June", "5");
		        dictionnaire.put("July", "6");
		        dictionnaire.put("August", "7");
		        dictionnaire.put("September", "8");
		        dictionnaire.put("October", "9");
		        dictionnaire.put("November", "10");
		        dictionnaire.put("December", "11");

		        String month = dictionnaire.get(decoupe[0]);
		        String year = decoupe[1];
		        
		        Calendar calendar = Calendar.getInstance();
		        calendar.set(Calendar.YEAR,  Integer.parseInt(year) );
		        calendar.set(Calendar.MONTH,  Integer.parseInt(month) );
		        calendar.set(Calendar.DAY_OF_MONTH,1 );
		        calendar.set(Calendar.HOUR_OF_DAY,0 );
		        calendar.set(Calendar.MINUTE, 0);
		        calendar.set(Calendar.SECOND, 0);
		        calendar.set(Calendar.MILLISECOND, 0);
		        

		        Calendar cal1 = (Calendar) calendar.clone();
		        Calendar cal2 = (Calendar) calendar.clone();
		        Calendar cal3 = (Calendar) calendar.clone();
		        Calendar cal4 = (Calendar) calendar.clone();
		        
		        int numDays = calendar.getActualMaximum(Calendar.DATE);
		        
		        // premier jour du mois
		        SimpleDateFormat format_date = new SimpleDateFormat("dd MM yyyy");     
		        Date mois_premier_jour = calendar.getTime();             
		        String mois_premier_jour_string = format_date.format(mois_premier_jour);  
		        
		        // dernier jour du mois
		        int lastDate = cal3.getActualMaximum(cal3.DATE);
		        cal3.set(Calendar.DATE, lastDate);
		        int lastDay = cal3.get(Calendar.DAY_OF_WEEK);
		        Date mois_dernier_jour = cal3.getTime();             
		        String mois_dernier_jour_string = format_date.format(mois_dernier_jour);           

		        // premier jour matrice
		        shift_day_begin = firstDayOfTheMonth;
		        cal1.add(cal1.DATE, -shift_day_begin);  
		        long timestamp_start = cal1.getTimeInMillis();
		        Date premier_jour_matrice = cal1.getTime();             
		        String premier_jour_matrice_string = format_date.format(premier_jour_matrice);           
		        
		        // dernier jour matrice
		        shift_day_end = numDays + shift_day_begin - 1 ;
		        int zzz = 41 + 1 - numDays - shift_day_begin;  
		        cal3.add(cal3.DATE, zzz);
		        long timestamp_end = cal3.getTimeInMillis();

		        Date dernier_jour_matrice = cal3.getTime();             
		        String dernier_jour_matrice_string = format_date.format(dernier_jour_matrice);           
		     
		        
		        list_appointment = null;
		        
		        // mAdapter = new GridAdapter(context, dayValueInCells, cal, mEvents);
		       // mAdapter = new GridAdapter(context, dayValueInCells, cal, list_appointment);
//		        calendarGridView.setAdapter(mAdapter);

		        
		        
//		        copy_dayValueInCells = new ArrayList<Date>();  
		        
		        mAdapterbis = new GridviewAdapterbis(this, dayValueInCells, cal);
		        gridView = (GridView) findViewById(R.id.calendar_grid);
		        gridView.setAdapter(mAdapterbis);

		        
		        
		        String concat =  mois_premier_jour_string + "  " + mois_dernier_jour_string;
		 
		        concat =  premier_jour_matrice_string + "  " + sDate + "  " + dernier_jour_matrice_string;
		        
//		        copy_dayValueInCells = dayValueInCells;
		        
		        //currentDate.setText(concat);
		       
		       // currentDate.setText("sDate");

		    }


		  
		 /*******************************************************/
		 
		  private class GridviewAdapterbis extends BaseAdapter
			 {
				 	private ArrayList<String> listJours;
					private Activity activity;
					private List<Date> monthlyDates;
				    private Calendar currentDate;
					
					public GridviewAdapterbis(Activity activity, List<Date> monthlyDates, Calendar currentDate) 
					{
						super();
						this.activity = activity;
						this.monthlyDates = monthlyDates;
				        this.currentDate = currentDate;
					}
					
					@Override
					public int getCount() 
					{
						// TODO Auto-generated method stub
						return monthlyDates.size();
					}

					@Override
					public Object getItem(int position)
					{
						// TODO Auto-generated method stub
						return monthlyDates.get(position);
					}

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

					public class ViewHolder
					{
						public TextView txtViewTitle;
					}
					
					
					@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);
					       
				    	
						ViewHolder view;
						LayoutInflater inflator = activity.getLayoutInflater();
						
						
						
						
						if(convertView==null)
						{
							view = new ViewHolder();
							convertView = inflator.inflate(R.layout.single_cell_layout, null);
							
							view.txtViewTitle = (TextView) convertView.findViewById(R.id.calendar_date_id);
							convertView.setTag(view);
							
							 if(displayMonth == currentMonth && displayYear == currentYear)
						        {
						        	
						            view.txtViewTitle.setBackgroundColor(Color.parseColor("#61ff21")); // FF5733  61ff21  ebeef0
						        }
						        else
						        {
						            view.txtViewTitle.setBackgroundColor(Color.parseColor("#FF7330")); // cccccc
						        }

							
						}
						else
						{
							view = (ViewHolder) convertView.getTag();
						}
						
						
						
						//view.txtViewTitle.setText(listJours.get(position));
						view.txtViewTitle.setText(String.valueOf(dayValue));
						
						 TextView eventIndicator = (TextView)findViewById(R.id.event_id);
			           //     eventIndicator.setBackgroundColor(Color.parseColor("#FF4081"));

						
						return convertView;
						
					
				    }
				    
				 
			 }

		  
		  
		 /*******************************************************/ 
 
		 private class GridviewAdapter extends BaseAdapter
		 {
			 	private ArrayList<String> listJours;
				private ArrayList<Integer> listFlag;
				private Activity activity;
				
				public GridviewAdapter(Activity activity,ArrayList<String> listJours) 
				{
					super();
					this.listJours = listJours;
					this.activity = activity;
				}
				
				@Override
				public int getCount() 
				{
					// TODO Auto-generated method stub
					return listJours.size();
				}

				@Override
				public String getItem(int position)
				{
					// TODO Auto-generated method stub
					return listJours.get(position);
				}

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

				public class ViewHolder
				{
					public ImageView imgViewFlag;
					public TextView txtViewTitle;
				}
				
				@Override
				public View getView(int position, View convertView, ViewGroup parent)
				{
					// TODO Auto-generated method stub
					ViewHolder view;
					LayoutInflater inflator = activity.getLayoutInflater();
					
					if(convertView==null)
					{
						view = new ViewHolder();
						convertView = inflator.inflate(R.layout.single_cell_layout, null);
						
						view.txtViewTitle = (TextView) convertView.findViewById(R.id.calendar_date_id);
						//view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);
						
						convertView.setTag(view);
					}
					else
					{
						view = (ViewHolder) convertView.getTag();
					}
					
					view.txtViewTitle.setText(listJours.get(position));
					//view.imgViewFlag.setImageResource(listFlag.get(position));
					
					return convertView;
			}
			 		
			 
		 }
		  
		 private class TextAdapter extends BaseAdapter
		 {

			 Context context;
 
			 public TextAdapter(Context context) 
			 {
				 this.context = context;
			 }
 
			 @Override
			 public int getCount()
			 {
				 return itemsName.length;
			 }

			 @Override
			 public Object getItem(int position)
			 {
				 return itemsName[position];
			 }

			 @Override
			 public long getItemId(int position) 
			 {
				 return position;
			 }

			 @Override
			 public View getView(int position, View convertView, ViewGroup parent) 
			 {
				 TextView text = new TextView(this.context);
				 text.setText(itemsName[position]);
				 text.setGravity(Gravity.CENTER);
				 text.setBackgroundColor(Color.parseColor("#fbdcbb"));
				 text.setTextColor(Color.parseColor("#040404"));
				 text.setLayoutParams(new GridView.LayoutParams(144, 144));
				 return text;
			 }
 
 
 }

}
