package com.androidbegin.buttonclicktutorial;


import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.net.ParseException;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

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


public class agenda extends Activity 
{
    private static final String TAG = MainActivity.class.getSimpleName();
    private ImageView previousDay;
    private ImageView nextDay;
    private TextView DisplayDate;
    private Calendar cal = Calendar.getInstance();
    private DatabaseQuery mQuery;
    private RelativeLayout mLayout;
    private int eventIndex;
    
    
    Calendar cal_start = Calendar.getInstance();
    Calendar cal_end = Calendar.getInstance();
    
	DB_alarm db;
	private SQLiteDatabase dataBase;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.agenda_view);
        db = new DB_alarm(getApplicationContext());

        //setContentView(R.layout.new_view);
        
        Bundle extras = getIntent().getExtras();
		String date_selection = extras.getString("EXTRA");
		DisplayDate=(TextView)findViewById(R.id.display_current_date); 
		DisplayDate.setText(date_selection);
		
		SimpleDateFormat formatter = new SimpleDateFormat("dd MM yyyy");
		try
		{
			Date date = (Date)formatter.parse(date_selection);
		} 
		catch (java.text.ParseException e1) 
		{
				e1.printStackTrace();
		}
        
        mQuery = new DatabaseQuery(this);
        mLayout = (RelativeLayout)findViewById(R.id.left_event_column);
        eventIndex = mLayout.getChildCount();
      
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd MM yyyy");
        Date convertedDate = new Date();
        try
        {
				convertedDate = dateFormat.parse(date_selection);
		} 
        catch (java.text.ParseException e) 
        {
				e.printStackTrace();
		}

        long timestamp = convertedDate.getTime();
        timestamp =  1484780400000L;
        String stimestamp = String.valueOf(timestamp);
        
        // lire la table 
        //String requete = "SELECT * FROM table_appointment WHERE datetime >= " + max + " AND datetime <= " + min + " ORDER BY datetime ASC" ;
        String requete = "SELECT * FROM table_appointment WHERE date = " + stimestamp ;
    	
        Cursor Cursor = db.DB_query(requete);
		int val = Cursor.getCount();
		
		if(val != 0)
		{
			/*
				ArrayList<String> record_id = new ArrayList<String>();
				ArrayList<String> record_titre = new ArrayList<String>();
				ArrayList<String> record_date = new ArrayList<String>();
				ArrayList<String> record_startime = new ArrayList<String>();
				ArrayList<String> record_endtime = new ArrayList<String>();
				ArrayList<String> record_status = new ArrayList<String>();
				ArrayList<String> record_createdtime = new ArrayList<String>();
				ArrayList<String> record_modifiedtime = new ArrayList<String>();
			*/	
				if (Cursor.moveToFirst()) 
				{
					do 
					{
						/*
						record_id.add(Cursor.getString(Cursor.getColumnIndex(DB_alarm.KEY_ID )));
						record_titre.add(Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_TITRE )));
						record_date.add(Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_DATE )));
						record_startime.add(Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_STARTDATE ))); 
						record_endtime.add(Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_ENDTIME ))); 
						record_status.add(Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_STATUS ))); 
						record_createdtime.add(Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_CREATEDTIME ))); 
						record_modifiedtime.add(Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_MODIFIEDTIME ))); 
						*/
						
						String titre = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_TITRE ));
						String start_timestamp = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_STARTDATE ));
						String end_timestamp = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_ENDTIME ));
						
						Date date_start = new Date(Long.parseLong(start_timestamp));
						Date date_end = new Date(Long.parseLong(end_timestamp));

						// pour affichage de la plage horaire
						Calendar cal = Calendar.getInstance();
						cal.setTime(date_start);
						String year_start = String.valueOf(cal.get(Calendar.YEAR));
						String month_start = String.valueOf(cal.get(Calendar.MONTH));
						String day_start = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
						String hour_start = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
						String minute_start = String.valueOf(cal.get(Calendar.MINUTE));
						
						cal.setTime(date_end);
						String year_end = String.valueOf(cal.get(Calendar.YEAR));
						String month_end = String.valueOf(cal.get(Calendar.MONTH));
						String day_end = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
						String hour_end = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
						String minute_end = String.valueOf(cal.get(Calendar.MINUTE));
						
						String infos = hour_start + ":" + minute_start + " à " + hour_end + ":" + minute_end; 
				        display_appointment(date_start, date_end, "zebra", "#3F51B5", 123, infos);

						
						
						// construction de s1 et s2 qui alimante aaaaa()
						
						//String s1 = "2017-0-7-15-54";
				        //String s2 = "2017-0-7-18-02";
				        //aaaaaa(s1, s2, "zebra", "#3F51B5", 123);
				        

				        //s1 = "2017-0-7-13-10";
				        //s2 = "2017-0-7-13-48";
				        //aaaaaa(s1, s2, "plouf", "#FFE1B5", 768);
						
						
						
					} while (Cursor.moveToNext());
					
				}
		
		}
        
        //final Calendar cal = Calendar.getInstance();
        //cal.setTimeInMillis(System.currentTimeMillis());
        //Date date = cal.getTime();
        //mHour = date.getHours();
        //mMinute = date.getMinutes();
        
        
        
        //displayDailyEvents();
        
        
        //boucle();
        //String s1 = "2017-0-7-15-54";
        //String s2 = "2017-0-7-18-02";
        //aaaaaa(s1, s2, "zorro", "#3F51B5", 123);

        //s1 = "2017-0-7-13-10";
        //s2 = "2017-0-7-13-48";
        //aaaaaa(s1, s2, "alpha", "#FFE1B5", 768);

        
        previousDay = (ImageView)findViewById(R.id.previous_day);
        nextDay = (ImageView)findViewById(R.id.next_day);
        
        
        previousDay.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                previousCalendarDate();
            }
        });
        
        
        nextDay.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v)
            {
                nextCalendarDate();
            }
        });
    }
    
    private void gestion_appointment()
    {
    		long timestamp =  1484780400000L;
    		String stimestamp = String.valueOf(timestamp);
         
    		// lire la table 
    		//	String requete = "SELECT * FROM table_appointment WHERE datetime >= " + max + " AND datetime <= " + min + " ORDER BY datetime ASC" ;
    		String requete = "SELECT * FROM table_appointment WHERE date = " + stimestamp ;
     	
    		Cursor Cursor = db.DB_query(requete);
    		int val = Cursor.getCount();
 		
    		if(val != 0)
    		{
 				if (Cursor.moveToFirst()) 
 				{
	 					do 
	 					{
		 						String titre = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_TITRE ));
		 						String start_timestamp = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_STARTDATE ));
		 						String end_timestamp = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_ENDTIME ));
		 						
		 						Date date_start = new Date(Long.parseLong(start_timestamp));
		 						Date date_end = new Date(Long.parseLong(end_timestamp));
		
		 						// pour affichage de la plage horaire
		 						Calendar cal = Calendar.getInstance();
		 						cal.setTime(date_start);
		 						String year_start = String.valueOf(cal.get(Calendar.YEAR));
		 						String month_start = String.valueOf(cal.get(Calendar.MONTH));
		 						String day_start = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
		 						String hour_start = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
		 						String minute_start = String.valueOf(cal.get(Calendar.MINUTE));
		 						
		 						cal.setTime(date_end);
		 						String year_end = String.valueOf(cal.get(Calendar.YEAR));
		 						String month_end = String.valueOf(cal.get(Calendar.MONTH));
		 						String day_end = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
		 						String hour_end = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
		 						String minute_end = String.valueOf(cal.get(Calendar.MINUTE));
		 						
		 						String infos = hour_start + ":" + minute_start + " à " + hour_end + ":" + minute_end; 
		 				        display_appointment(date_start, date_end, "zebra", "#3F51B5", 123, infos);
	 						
	 					} while (Cursor.moveToNext());
	 					
 				}
 		}

    }
    
    private void previousCalendarDate()
    {
        mLayout.removeViewAt(eventIndex - 1);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        DisplayDate.setText(displayDateInString(cal.getTime()));
        displayDailyEvents();
    }
    
    private void nextCalendarDate()
    {
        mLayout.removeViewAt(eventIndex - 1);
        cal.add(Calendar.DAY_OF_MONTH, 1);
        DisplayDate.setText(displayDateInString(cal.getTime()));
        displayDailyEvents();
    }
    
    private String displayDateInString(Date mDate)
    {
        SimpleDateFormat formatter = new SimpleDateFormat("d MMMM, yyyy", Locale.ENGLISH);
        return formatter.format(mDate);
    }
    
    
    private void display_appointment(Date start, Date end, String message, String color, int indexxx, String info)
    {
    	 SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
      	
    	 int height = getEventTimeFrame(start, end);
	       
	     String displayValue = timeFormatter.format(start);
	     String[]hourMinutes = displayValue.split(":");
	     int hours = Integer.parseInt(hourMinutes[0]);
	     int minutes = Integer.parseInt(hourMinutes[1]);
	     int topViewMargin = (hours * 60) +  minutes; //     ((minutes * 60) / 100);
	     TextView mEventView = new TextView(agenda.this);
	     //mEventView.setId(100);
	        
	     RelativeLayout.LayoutParams lParam = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
	     lParam.addRule(RelativeLayout.ALIGN_PARENT_TOP);
	     lParam.topMargin = topViewMargin * 2;
	     lParam.leftMargin = 24;
	     mEventView.setLayoutParams(lParam);
	     mEventView.setPadding(24, 0, 24, 0);
	     mEventView.setPadding(120, 0, 120, 0);
	        
	     mEventView.setHeight(height * 2);
	     mEventView.setGravity(0x11);
	     mEventView.setTextColor(Color.parseColor("#ffffff"));
	  
	     mEventView.setText(message);
	     mEventView.setBackgroundColor(Color.parseColor(color));
	        
	     mLayout.addView(mEventView, eventIndex - 1);
	     
	     TextView tv = new TextView(this);
	     tv.setX(20);
	     tv.setX(20);
	     tv.setText(info);
	     tv.setTextColor(Color.parseColor("#ffffff"));
	     tv.setLayoutParams(lParam);
	     mLayout.addView(tv);
	        
	     final int index = indexxx;
	     mEventView.setOnClickListener(new OnClickListener() 
		 {
				public void onClick(View v)
				{
	               Log.i("TAG", "index :" + index);
	               Toast.makeText(getApplicationContext(), "Clicked Button Index :" + index, Toast.LENGTH_LONG).show();
	               Intent myIntent = new Intent(agenda.this, infos_event.class);
				   startActivity(myIntent);
				}
		 });
    	
    }
    
    
    private void aaaaaa(String start, String end, String message, String color, int indexxx)
    {
    	
         SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
    	
    	 ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
		 
		 ArrayList<Object> dataList = new ArrayList<Object>();
		 
		 String[] parts = start.split("-");
		 int year  = Integer.parseInt(parts[0]);  // year
		 int mois = Integer.parseInt(parts[1]); // mois
		 int day = Integer.parseInt(parts[2]); // jour
		 int hour = Integer.parseInt(parts[3]); // hour
		 int min = Integer.parseInt(parts[4]); // min
		 cal_start.set(year,mois,day,hour,min);
	     Date eventDate =  cal_start.getTime();
         
	     parts = end.split("-");
		 year  = Integer.parseInt(parts[0]);  // year
		 mois = Integer.parseInt(parts[1]); // mois
		 day = Integer.parseInt(parts[2]); // jour
		 hour = Integer.parseInt(parts[3]); // hour
		 min = Integer.parseInt(parts[4]); // min	     
		 cal_end.set(year,mois,day,hour,min);
         Date endDate =  cal_end.getTime();

	     int height = getEventTimeFrame(eventDate, endDate);
	       
	     String displayValue = timeFormatter.format(eventDate);
	     String[]hourMinutes = displayValue.split(":");
	     int hours = Integer.parseInt(hourMinutes[0]);
	     int minutes = Integer.parseInt(hourMinutes[1]);
	     int topViewMargin = (hours * 60) +  minutes; //     ((minutes * 60) / 100);
	     TextView mEventView = new TextView(agenda.this);
	     //mEventView.setId(100);
	        
	     RelativeLayout.LayoutParams lParam = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
	     lParam.addRule(RelativeLayout.ALIGN_PARENT_TOP);
	     lParam.topMargin = topViewMargin * 2;
	     lParam.leftMargin = 24;
	     mEventView.setLayoutParams(lParam);
	     mEventView.setPadding(24, 0, 24, 0);
	     mEventView.setPadding(120, 0, 120, 0);
	        
	     mEventView.setHeight(height * 2);
	     mEventView.setGravity(0x11);
	     mEventView.setTextColor(Color.parseColor("#ffffff"));
	  
	     mEventView.setText(message);
	     mEventView.setBackgroundColor(Color.parseColor(color));
	        
	     mLayout.addView(mEventView, eventIndex - 1);
	        
	     final int index = indexxx;
	     mEventView.setOnClickListener(new OnClickListener() 
		 {
				public void onClick(View v)
				{
	               Log.i("TAG", "index :" + index);
	               Toast.makeText(getApplicationContext(), "Clicked Button Index :" + index, Toast.LENGTH_LONG).show();
	               Intent myIntent = new Intent(agenda.this, infos_event.class);
				   startActivity(myIntent);
				}
		 });
	     
		 
	
    }
    
    
    private void boucle()
    {
    	
        Date calendarDate = cal.getTime();
        
        cal_start.set(2017,0,7,15,54);
        Date eventDate =  cal_start.getTime();
        
        cal_end.set(2017,0,7,18,02);
        Date endDate =  cal_end.getTime();
        
        String message = "Plouf";
        int height = getEventTimeFrame(eventDate, endDate);
        String color = "#3F51B5";
        
        SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
        String displayValue = timeFormatter.format(eventDate);
        String[]hourMinutes = displayValue.split(":");
        int hours = Integer.parseInt(hourMinutes[0]);
        int minutes = Integer.parseInt(hourMinutes[1]);
        Log.d(TAG, "Hour value " + hours);
        Log.d(TAG, "Minutes value " + minutes);
        int topViewMargin = (hours * 60) +  minutes; //     ((minutes * 60) / 100);
        Log.d(TAG, "Margin top " + topViewMargin);
//        createEventView(topViewMargin, height, message, color);
        
        TextView mEventView = new TextView(agenda.this);
        mEventView.setId(100);

        
        RelativeLayout.LayoutParams lParam = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lParam.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        lParam.topMargin = topViewMargin * 2;
        lParam.leftMargin = 24;
        mEventView.setLayoutParams(lParam);
        mEventView.setPadding(24, 0, 24, 0);
        mEventView.setPadding(120, 0, 120, 0);
        
        mEventView.setHeight(height * 2);
        mEventView.setGravity(0x11);
        mEventView.setTextColor(Color.parseColor("#ffffff"));
  
        mEventView.setText(message);
        //mEventView.setBackgroundColor(Color.parseColor("#3F51B5"));
        mEventView.setBackgroundColor(Color.parseColor(color));
        
        mLayout.addView(mEventView, eventIndex - 1);
        
        final int index = 55;
		// Set click listener for button
        mEventView.setOnClickListener(new OnClickListener() 
		{
			public void onClick(View v)
			{
               Log.i("TAG", "index :" + index);
               Toast.makeText(getApplicationContext(), "Clicked Button Index :" + index, Toast.LENGTH_LONG).show();
               Intent myIntent = new Intent(agenda.this, infos_event.class);
				startActivity(myIntent);
			}
		});
        
        
     
        calendarDate = cal.getTime();
        
        cal_start.set(2017,0,7,18,54);
        eventDate =  cal_start.getTime();
        
        cal_end.set(2017,0,7,19,22);
        endDate =  cal_end.getTime();
        
        message = "Sport";
        height = getEventTimeFrame(eventDate, endDate);
        color = "#FFE1B5";
        
        displayValue = timeFormatter.format(eventDate);
        hourMinutes = displayValue.split(":");
        hours = Integer.parseInt(hourMinutes[0]);
        minutes = Integer.parseInt(hourMinutes[1]);
        Log.d(TAG, "Hour value " + hours);
        Log.d(TAG, "Minutes value " + minutes);
        topViewMargin = (hours * 60) +  minutes; //     ((minutes * 60) / 100);
        Log.d(TAG, "Margin top " + topViewMargin);
//        createEventView(topViewMargin, height, message, color);
        
        mEventView = new TextView(agenda.this);
        mEventView.setId(200);
		
        
        lParam = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lParam.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        lParam.topMargin = topViewMargin * 2;
        lParam.leftMargin = 24;
        mEventView.setLayoutParams(lParam);
        mEventView.setPadding(24, 0, 24, 0);
        mEventView.setPadding(120, 0, 120, 0);
        
        mEventView.setHeight(height * 2);
        mEventView.setGravity(0x11);
        mEventView.setTextColor(Color.parseColor("#ffffff"));
  
        //mEventView.setText(message);
        //mEventView.setBackgroundColor(Color.parseColor("#3F51B5"));
        mEventView.setBackgroundColor(Color.parseColor(color));
        
        mLayout.addView(mEventView, eventIndex - 1);
  
        final int aaindex = 128;
        
        
      
        
       
		// Set click listener for button
        mEventView.setOnClickListener(new OnClickListener() 
		{
			public void onClick(View v)
			{
               Log.i("TAG", "index :" + index);
               Toast.makeText(getApplicationContext(), "Clicked Button Index :" + aaindex, Toast.LENGTH_LONG).show();
			}
		});

        
        TextView tv = new TextView(this);
        tv.setX(20);
        tv.setX(20);
        
        tv.setText("TextView");
        tv.setLayoutParams(lParam);
        mLayout.addView(tv);
    	
    }
    
    
    
    
    private void displayDailyEvents()
    {
        Date calendarDate = cal.getTime();
        List<EventObjects> dailyEvent = mQuery.getAllFutureEvents(calendarDate);
        
        cal_start.set(2017,0,7,15,54);
        Date eventDate =  cal_start.getTime();
        
        cal_end.set(2017,0,7,18,02);
        Date endDate =  cal_end.getTime();
        
        String eventMessage = "Plouf";
        int eventBlockHeight = getEventTimeFrame(eventDate, endDate);
        Log.d(TAG, "Height " + eventBlockHeight);
        String color = "#3F51B5";
        displayEventSection(eventDate, eventBlockHeight, eventMessage, color);

        
        
        cal_start.set(2017,0,7,13,30);
        eventDate =  cal_start.getTime();
        
        cal_end.set(2017,0,7,16,14);
        endDate =  cal_end.getTime();
        
        eventMessage = "Docteur";
        eventBlockHeight = getEventTimeFrame(eventDate, endDate);
        Log.d(TAG, "Height " + eventBlockHeight);
        color = "#35DC0C";
        displayEventSection(eventDate, eventBlockHeight, eventMessage, color);
        
        
       /* 
        cal_start.set(2017,0,7,15,54);
        eventDate =  cal_start.getTime();
        
        cal_end.set(2017,0,7,18,02);
        endDate =  cal_end.getTime();
        
        eventMessage = "Plouf";
        eventBlockHeight = getEventTimeFrame(eventDate, endDate);
        Log.d(TAG, "Height " + eventBlockHeight);
        displayEventSection(eventDate, eventBlockHeight, eventMessage);
        */		
        /*
        for(EventObjects eObject : dailyEvent)
        {
            Date eventDate = eObject.getDate();
            Date endDate = eObject.getEnd();
            String eventMessage = eObject.getMessage();
            int eventBlockHeight = getEventTimeFrame(eventDate, endDate);
            Log.d(TAG, "Height " + eventBlockHeight);
            displayEventSection(eventDate, eventBlockHeight, eventMessage);
        }
        */

    }
    
    private int getEventTimeFrame(Date start, Date end)
    {
        long timeDifference = end.getTime() - start.getTime();
        Calendar mCal = Calendar.getInstance();
        mCal.setTimeInMillis(timeDifference);
        
        long tmp = timeDifference / 60000;
        int nbre_minutes = (int) tmp;
        return nbre_minutes;
        
        //int hours = mCal.get(Calendar.HOUR);
        //int minutes = mCal.get(Calendar.MINUTE);
        //return (hours * 60) + ((minutes * 60) / 100);
    }
    
    
    private void displayEventSection(Date eventDate, int height, String message, String color)
    {
        SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
        String displayValue = timeFormatter.format(eventDate);
        String[]hourMinutes = displayValue.split(":");
        int hours = Integer.parseInt(hourMinutes[0]);
        int minutes = Integer.parseInt(hourMinutes[1]);
        Log.d(TAG, "Hour value " + hours);
        Log.d(TAG, "Minutes value " + minutes);
        int topViewMargin = (hours * 60) +  minutes; //     ((minutes * 60) / 100);
        Log.d(TAG, "Margin top " + topViewMargin);
        createEventView(topViewMargin, height, message, color);
    }
    
    private void createEventView(int topMargin, int height, String message, String color)
    {
        TextView mEventView = new TextView(agenda.this);
        RelativeLayout.LayoutParams lParam = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lParam.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        lParam.topMargin = topMargin * 2;
        lParam.leftMargin = 24;
        mEventView.setLayoutParams(lParam);
        mEventView.setPadding(24, 0, 24, 0);
        mEventView.setPadding(120, 0, 120, 0);
        
        //height = 50;
        mEventView.setHeight(height * 2);
        mEventView.setGravity(0x11);
        mEventView.setTextColor(Color.parseColor("#ffffff"));
        
        
  //      cal_start.set(2017,0,7,15,42,30);
  //      cal_end.set(2017,0,7,18,12,30);
        
        // 15H 900
        // 16H 960
        //15 * 60
        //16 * 60
        
        //8280001     
        
        // 1heure = 60s  * 60min * 1000
        //int minutes = (int) ((milliseconds / 1000) / 60);
        
        mEventView.setText(message);
        //mEventView.setBackgroundColor(Color.parseColor("#3F51B5"));
        mEventView.setBackgroundColor(Color.parseColor(color));
        
        
        mLayout.addView(mEventView, eventIndex - 1);
        
        
        mEventView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
            	Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();
            	
            	//Intent intent = new Intent(MainActivity.this, add_event.class);
                //startActivity(intent);
            }
        });
        
    }
}