package com.example.button01;


import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import java.util.Calendar;
import android.app.ActionBar.LayoutParams;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Space;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity implements View.OnClickListener 
{

    private ViewGroup buttonsContainer, buttonsContainer2;
    private Button activeButton = null;
    
    private Button activeButton1 = null;
    private Button activeButton2 = null;
    private Button activeButton3 = null;
    private Button activeButton4 = null;
    private Button activeButton5 = null;
    private Button activeButton6 = null;
    private Button activeButton7 = null;
    
    
    private final int MAX_BUTTONS = 3;

    boolean isPressed = false;

    Button button1 , button2, button3 , button4, button5 , button6 , button7;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        gui();
        alarm01();
/*
        this.buttonsContainer = (ViewGroup) findViewById(R.id.buttonsContainer);

        int buttonsSpacing = (int) getResources().getDimension(R.dimen.activity_horizontal_margin);
        int buttonSize = (int) getResources().getDimension(R.dimen.button_size);

        for (int i = 0; i < MAX_BUTTONS; i++) {
            Button button = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, buttonsContainer, false);
            button.setText("Test " + i);
            button.setOnClickListener(this);
            buttonsContainer.addView(button);

            //Add margin between buttons manually
            if (i != MAX_BUTTONS - 1) {
                buttonsContainer.addView(new Space(this), new ViewGroup.LayoutParams(buttonsSpacing, buttonSize));
            }
        }
        selectButton((Button) buttonsContainer.getChildAt(0));
 */       

/*******************************************/
 /*       
        int buttonsSpacing = (int) getResources().getDimension(R.dimen.activity_horizontal_margin);
        int buttonSize = (int) getResources().getDimension(R.dimen.button_size);
        
        
        this.buttonsContainer = (ViewGroup) findViewById(R.id.buttonsContainer);
        
        button1 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, buttonsContainer, false);
        button1.setId(1);
        button1.setText("LUN.");
        button1.setOnClickListener(this);
        buttonsContainer.addView(button1);

        buttonsContainer.addView(new Space(this), new ViewGroup.LayoutParams(buttonsSpacing, buttonSize));

        button2 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, buttonsContainer, false);
        button2.setId(2);
        button2.setText("MAR.");
        button2.setOnClickListener(this);
        buttonsContainer.addView(button2);

        buttonsContainer.addView(new Space(this), new ViewGroup.LayoutParams(buttonsSpacing, buttonSize));
        
        button3 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, buttonsContainer, false);
        button3.setId(3);
        button3.setText("MER.");
        button3.setOnClickListener(this);
        buttonsContainer.addView(button3);
        
        buttonsContainer.addView(new Space(this), new ViewGroup.LayoutParams(buttonsSpacing, buttonSize));

        button4 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, buttonsContainer, false);
        button4.setId(4);
        button4.setText("JEU.");
        button4.setOnClickListener(this);
        buttonsContainer.addView(button4);
        
                
        this.buttonsContainer2 = (ViewGroup) findViewById(R.id.buttonsContainer2);
        
        
        button5 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, buttonsContainer2, false);
        button5.setId(1);
        button5.setText("XXX.");
        button5.setOnClickListener(this);
        buttonsContainer2.addView(button5);

*/        
/*******************************************/

        
/*        
        this.buttonsContainer2 = (ViewGroup) findViewById(R.id.buttonsContainer2);
        
        button5 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, buttonsContainer2, false);
        button5.setId(5);
        button5.setText("VEN.");
       // button.setTextSize(40);
        button5.setOnClickListener(this);
        buttonsContainer2.addView(button5);

        buttonsContainer2.addView(new Space(this), new ViewGroup.LayoutParams(buttonsSpacing, buttonSize));

        button6 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, buttonsContainer2, false);
        button6.setId(6);
        button6.setText("SAM.");
        button6.setOnClickListener(this);
        buttonsContainer2.addView(button6);

*/
        
      
//        LinearLayout layMain = createLayout(LinearLayout.VERTICAL);  	
// 		layMain.setPadding(8, 8, 8, 8);
 /*		
 		LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        
        //button5 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, buttonsContainer, false);
        button5 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, null);
        button5.setId(5);
        button5.setText("VEN.");
        button5.setOnClickListener(this);
        layMain.addView(ll);



 		ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);

        button6 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, null);

        button6.setId(6);
        button6.setText("SAM.");
        button6.setOnClickListener(this);
        ll.addView(button6);
        layMain.addView(ll);

        
        setContentView(layMain);
*/
 		

        
    }
    
    // http://blog.mikesir87.io/2013/04/android-creating-an-alarm-with-alarmmanager/
    
    private void gui()
    {
    	
        int buttonsSpacing = (int) getResources().getDimension(R.dimen.activity_horizontal_margin);
        int buttonSize = (int) getResources().getDimension(R.dimen.button_size);
    	
    	LinearLayout layMain = createLayout(LinearLayout.VERTICAL);  	
 		layMain.setPadding(58, 8, 8, 8);
 		
 		LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        TextView tv = new TextView(this);
        tv.setText("Evenement");
        tv.setPadding(10, 40, 40, 40);
 	    ll.addView(tv);
 	    layMain.addView(ll);

 	    ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        tv = new TextView(this);
        tv.setText("sfffsq");
        tv.setPadding(10, 40, 40, 40);
 	    ll.addView(tv);
 	    layMain.addView(ll);


 	    ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        tv = new TextView(this);
        tv.setText("zero");
        tv.setPadding(10, 40, 40, 40);
 	    ll.addView(tv);
 	    layMain.addView(ll);

 	    /******* ligne 1 **********/ 
 	    
        ll = new LinearLayout(this);
       	ll.setOrientation(LinearLayout.HORIZONTAL);
        button1 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, ll ,false);
        button1.setId(1);
        button1.setText("LUN.");
        button1.setOnClickListener(this);
        ll.addView(button1);
        
        ll.addView(new Space(this), new ViewGroup.LayoutParams(buttonsSpacing, buttonSize));
        
        button2 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, ll ,false);
        button2.setId(2);
        button2.setText("MAR.");
        button2.setOnClickListener(this);
        ll.addView(button2);
	    
	    ll.addView(new Space(this), new ViewGroup.LayoutParams(buttonsSpacing, buttonSize));
        
        button3 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, ll ,false);
        button3.setId(3);
        button3.setText("MER.");
        button3.setOnClickListener(this);
        ll.addView(button3);
	    
	    ll.addView(new Space(this), new ViewGroup.LayoutParams(buttonsSpacing, buttonSize));
        
        button4 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, ll ,false);
        button4.setId(4);
        button4.setText("JEU.");
        button4.setOnClickListener(this);
        ll.addView(button4);
	    layMain.addView(ll);
	    
	    /************* SAUT ****************/
        ll = new LinearLayout(this);
       	ll.setOrientation(LinearLayout.HORIZONTAL);
 		ImageView imageView = new ImageView(this);
 		LayoutParams layoutParams=new LayoutParams(20, 20);
  	    // layoutParams.setMargins(10,10,10,100);
 	    imageView.setLayoutParams(layoutParams);
 	    ll.addView(imageView);
 	    layMain.addView(ll);
        
	    /******* ligne 2 **********/ 
	    
        ll = new LinearLayout(this);
       	ll.setOrientation(LinearLayout.HORIZONTAL);
        button5 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, ll ,false);
        button5.setId(5);
        button5.setText("VEN.");
        button5.setOnClickListener(this);
        ll.addView(button5);
        
        ll.addView(new Space(this), new ViewGroup.LayoutParams(buttonsSpacing, buttonSize));
        
        button6 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, ll ,false);
        button6.setId(6);
        button6.setText("SAM.");
        button6.setOnClickListener(this);
        ll.addView(button6);
        
        
        
        ll.addView(new Space(this), new ViewGroup.LayoutParams(buttonsSpacing, buttonSize));
        
        button7 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, ll ,false);
        button7.setId(7);
        button7.setText("DIM.");
        button7.setOnClickListener(this);
        ll.addView(button7);
	    layMain.addView(ll);

	    
        
        
        
      // 	final View childView2 = getLayoutInflater().inflate(R.layout.circularbutton_layout, null);
      //  ll.addView(childView2);
 	  //  layMain.addView(ll);

 	    
 	    
     
 	    
 	    
/*
        ll = new LinearLayout(this);
       	ll.setOrientation(LinearLayout.HORIZONTAL);
       	ll.setTag(9); 
       	final View childView3 = getLayoutInflater().inflate(R.layout.circularbutton_layout, null);
        ll.addView(childView3);
 	    layMain.addView(ll);
*/

 //       ll = new LinearLayout(this);
 //      	ll.setOrientation(LinearLayout.HORIZONTAL);
       	//final View childView = getLayoutInflater().inflate(R.layout.circularbutton_layout, null);
 //       button3 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, ll ,false);
 //       button1.setId(1);
 //       button1.setText("LUN.");
 //       button1.setOnClickListener(this);
 //       ll.addView(button1);

 	    
        setContentView(layMain);

    	
    }
    
    
    
    private LinearLayout createLayout(int iOrientation)
	  {
			LinearLayout lay = new LinearLayout(this);		
			lay.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			lay.setOrientation(iOrientation);
			return lay;
	  }
	

    private void selectButton(Button button) 
    {
        if (activeButton != null) {
            //activeButton.setSelected(false);
            activeButton = null;
        }

        else
        {	
        activeButton = button;
        button.setSelected(true);
        }
        
        
    }

    @Override
    public void onClick(View view) 
    {
    	int element = view.getId();
    	
    	switch(element)
    	{
        	case 1:
        		if (activeButton1 != null) 
        		{
                    button1.setSelected(false);
                    activeButton1 = null;
                }
                else
                {	
                	activeButton1 = button1;
                	button1.setSelected(true);
                }
        		break;

        	case 2:
        		if (activeButton2 != null)
        		{
                    button2.setSelected(false);
                    activeButton2 = null;
                }
                else
                {	
                	activeButton2 = button2;
                	button2.setSelected(true);
                }
        		break;
        	case 3:
        		if (activeButton3 != null)
        		{
                    button3.setSelected(false);
                    activeButton3 = null;
                }
                else
                {	
                	activeButton3 = button3;
                	button3.setSelected(true);
                }
        		break;	
        	case 4:
        		if (activeButton4 != null)
        		{
                    button4.setSelected(false);
                    activeButton4 = null;
                }
                else
                {	
                	activeButton4 = button4;
                	button4.setSelected(true);
                }
        		break;	
        	case 5:
        		if (activeButton5 != null)
        		{
                    button5.setSelected(false);
                    activeButton5 = null;
                }
                else
                {	
                	activeButton5 = button5;
                	button5.setSelected(true);
                }
        		break;
        	case 6:
        		if (activeButton6 != null)
        		{
                    button6.setSelected(false);
                    activeButton6 = null;
                }
                else
                {	
                	activeButton6 = button6;
                	button6.setSelected(true);
                }
        		break;
        	case 7:
        		if (activeButton7 != null)
        		{
                    button7.setSelected(false);
                    activeButton7 = null;
                }
                else
                {	
                	activeButton7 = button7;
                	button7.setSelected(true);
                }
        		break;	
    }
    	
    	
       // selectButton((Button) view);
    }
    
    //setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
    //alarmMan.setRepeating(AlarmManager.ELAPSED_REALTIME, triggerAtTime, 86400000, pendingIntent);
    
    //http://android-er.blogspot.fr/2012/05/to-schedule-repeating-alarm-call.html
    
    //http://stackoverflow.com/questions/36550991/repeating-alarm-for-specific-days-of-week-android
    
    
    //https://developer.android.com/training/scheduling/alarms.html
    
    
    
   // http://stackoverflow.com/questions/14272295/how-can-i-get-the-repeat-alarm-for-week-days-using-alarm-manager
    
    void alarm01()
    {
    
    	AlarmManager alarmMgr;
    	PendingIntent alarmIntent;

    	alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    	
    	Intent intent = new Intent(this, receiver.class);
    	alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    	
    	// Set the alarm to start at 8:30 a.m.
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTimeInMillis(System.currentTimeMillis());
    	calendar.set(Calendar.HOUR_OF_DAY, 17);
    	calendar.set(Calendar.MINUTE, 18);

    	int min = 1; // 2 minites
    	// setRepeating() lets you specify a precise custom interval--in this case,  // 20 minutes.
    	alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),   1000 * 60 * min, alarmIntent);
    
    }
    
    
    void alarm02()
    {
    	
    	setAlarm(2); //set the alarm for this day of the week

           	
    }
    
    public void setAlarm(int dayOfWeek) 
    {
    	
    	AlarmManager alarmMgr;
    	PendingIntent alarmIntent;
    	
    	alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    	Intent intent = new Intent(this, receiver.class);
    	alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    	
        // Add this day of the week line to your existing code
    	Calendar alarmCalendar = Calendar.getInstance();
    	
        alarmCalendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);

        int hour = 20;
        int min = 10;
        
        alarmCalendar.set(Calendar.HOUR, hour);
        alarmCalendar.set(Calendar.MINUTE, min);
        alarmCalendar.set(Calendar.SECOND, 0);
        
        Long alarmTime = alarmCalendar.getTimeInMillis();
        //Also change the time to 24 hours.
        alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 24 * 60 * 60 * 1000 , alarmIntent); 
    }

    public void alarm03()
    {
    	//http://stackoverflow.com/questions/12476118/how-to-repeat-alerm-specific-day-of-week-in-android
    	
    	Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        String today = null;
        if (day == 2 || day == 3 || day == 4 || day == 5
                || day == 6) {

            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                    calSet.getTimeInMillis(), 24 * 60 * 60 * 1000,
                    pendingIntent);
        }
    }
    
    
    //http://www.allappsdevelopers.com/TopicDetail.aspx?TopicID=a2dcfbc6-adf1-40b6-8884-3b886c5fb7b1 ++++++++++++++++++++++++++++++++++
    
    
    //http://stackoverflow.com/questions/27942339/open-android-notifications-on-watch-and-phone-on-repeating-time-and-week-day-fro
    
    //http://androidexample.com/Create_Repeating_Alarm_Start_After_Each_2_Minutes/index.php?view=article_discription&aid=93
    
    //https://www.javacodegeeks.com/2012/09/android-alarmmanager-tutorial.html
    
    
 /*   
    alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY,
            AlarmManager.INTERVAL_DAY, intent);
  */
    
   /* 
    
    tous les jours
    
    Intent myIntent = new Intent(ThisActivity.this , NotifyService.class);     
    AlarmManager alarmManager = (AlarmManager) Context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getService(ThisActivity.this, 0, myIntent, 0);
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 12);
    calendar.set(Calendar.MINUTE, 00);
    calendar.set(Calendar.SECOND, 00);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pendingIntent); //Repeat every 24 hours
   */
    
    
    
 /*   
    Calendar calendar = Calendar.getInstance();

    int day = calendar.get(Calendar.DAY_OF_WEEK);

   alarmManager.set(AlarmManager.RTC_WAKEUP, startTimmer.getTimeInMillis(),day * 7, pendingIntent1);
   */
    
    
    
    //http://code4reference.com/2012/07/tutorial-on-android-alarmmanager/
    
    //everyday except the weekends
    
  // http://stackoverflow.com/questions/19834722/android-set-alarm-to-fire-everyday-except-the-weekends?rq=1  
    
 //   http://stackoverflow.com/questions/40376899/android-alarm-manager-setting-up-multiple-alarms?rq=1
    
/*  http://stackoverflow.com/questions/36624351/android-alarm-manager-every-week?rq=1
 *   
    public void addReminder(Context context) 
    {

    	  Intent alarmIntent = new Intent(context, WeeklyReminderReceiver.class);

    	  PendingIntent  pendingIntent = PendingIntent.getBroadcast(context, 0, 
    	            alarmIntent, PendingIntent.FLAG_ONE_SHOT);    

    	  AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    	  Calendar calendar = Calendar.getInstance();

    	  calendar.set(Calendar.DAY_OF_WEEK, 2);
    	  calendar.set(Calendar.HOUR_OF_DAY, 9);
    	  calendar.set(Calendar.MINUTE, 0);
    	  calendar.set(Calendar.SECOND, 0);
    	  calendar.set(Calendar.MILLISECOND, 0);   

    	  alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);     

    }
*/    
    
    
/*    
    if (chk_monday.isChecked()) {
        forday(2);
    } else if (chk_tuesday.isChecked()) {
        forday(3);
    } else if (chk_wednesday.isChecked()) {
        forday(4);
    } else if (chk_thursday.isChecked()) {
        forday(5);
    } else if (chk_friday.isChecked()) {
        forday(6);
    } else if (chk_sat.isChecked()) {
        forday(7);
    } else if (chk_sunday.isChecked()) {
        forday(1);
    }

public void forday(int week) {

calSet.set(Calendar.DAY_OF_WEEK, week);
calSet.set(Calendar.HOUR_OF_DAY, hour);
calSet.set(Calendar.MINUTE, minuts);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calSet.getTimeInMillis(), 1 * 60 * 60 * 1000, pendingIntent);
}
    
    */
    
}