package com.androidbegin.buttonclicktutorial;

import java.util.Calendar;

import android.app.DialogFragment;
import android.app.ActionBar.LayoutParams;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toast;


public class dialog_fragment extends DialogFragment 
{
	
	 Button button1 , button2, button3 , button4, button5 , button6 , button7;
	 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;
	 
	 String days_list_name = "";
	 String days_list_num = "";


	 int array_days_config_valeur[] = new int[7];
	 String array_days_config_name[] = new String[7];

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,	Bundle savedInstanceState)
	{
		View rootView = inflater.inflate(R.layout.dialog_fragment_layout, container, false);
		
		// titre non actif
		getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		
		//getDialog().setTitle("DialogFragment titre");		
		
		button1 = (Button) rootView.findViewById(R.id.btn_day_lun);
		button2 = (Button) rootView.findViewById(R.id.btn_day_mar);
		button3 = (Button) rootView.findViewById(R.id.btn_day_mer);
		button4 = (Button) rootView.findViewById(R.id.btn_day_jeu);
		button5 = (Button) rootView.findViewById(R.id.btn_day_ven);
		button6 = (Button) rootView.findViewById(R.id.btn_day_sam);
		button7 = (Button) rootView.findViewById(R.id.btn_day_dim);
		
		
		initialisation();
		
		listner(rootView);

		Button btn_apply = (Button) rootView.findViewById(R.id.btn_exit);
		btn_apply.setOnClickListener(new View.OnClickListener() 
		{
			    @Override
			    public void onClick(View v)
			    {
			    	sauver_configuration();
			    }
		});
		
		Button btn_exit = (Button) rootView.findViewById(R.id.btn_exit);
		btn_exit.setOnClickListener(new View.OnClickListener() 
		{
			    @Override
			    public void onClick(View v)
			    {
			    	 dismiss();
			    }
		});
		
		
		return rootView;
	}
	
	
	private void listner(View vue)
	{
			 button1 = (Button) vue.findViewById(R.id.btn_day_lun);
			 button1.setOnClickListener(new View.OnClickListener() 
			 {
				    @Override
				    public void onClick(View v)
				    {
				    	if (activeButton1 != null) 
		        		{
		                    button1.setSelected(false);
		                    activeButton1 = null;
		                }
		                else
		                {	
		                	activeButton1 = button1;
		                	button1.setSelected(true);
		                }
				    }
			 });
			 
			 button2 = (Button) vue.findViewById(R.id.btn_day_mar);
			 button2.setOnClickListener(new View.OnClickListener() 
			 {
				    @Override
				    public void onClick(View v)
				    {
				    	if (activeButton2 != null) 
		        		{
		                    button2.setSelected(false);
		                    activeButton2 = null;
		                }
		                else
		                {	
		                	activeButton2 = button2;
		                	button2.setSelected(true);
		                }
				    }
			 });
		 		 
			 button3 = (Button) vue.findViewById(R.id.btn_day_mer);
			 button3.setOnClickListener(new View.OnClickListener() 
			 {
				    @Override
				    public void onClick(View v)
				    {
				    	if (activeButton3 != null) 
		        		{
		                    button3.setSelected(false);
		                    activeButton3 = null;
		                }
		                else
		                {	
		                	activeButton3 = button3;
		                	button3.setSelected(true);
		                }
				    }
			 });
			 
			 button4 = (Button) vue.findViewById(R.id.btn_day_jeu);
			 button4.setOnClickListener(new View.OnClickListener() 
			 {
				    @Override
				    public void onClick(View v)
				    {
				    	if (activeButton4 != null) 
		        		{
		                    button4.setSelected(false);
		                    activeButton4 = null;
		                }
		                else
		                {	
		                	activeButton4 = button4;
		                	button4.setSelected(true);
		                }
				    }
			 });
			 
			 button5 = (Button) vue.findViewById(R.id.btn_day_ven);
			 button5.setOnClickListener(new View.OnClickListener() 
			 {
				    @Override
				    public void onClick(View v)
				    {
				    	if (activeButton5 != null) 
		        		{
		                    button5.setSelected(false);
		                    activeButton5 = null;
		                }
		                else
		                {	
		                	activeButton5 = button5;
		                	button5.setSelected(true);
		                }
				    }
			 });
			 
			 button6 = (Button) vue.findViewById(R.id.btn_day_sam);
			 button6.setOnClickListener(new View.OnClickListener() 
			 {
				    @Override
				    public void onClick(View v)
				    {
				    	if (activeButton6 != null) 
		        		{
		                    button6.setSelected(false);
		                    activeButton6 = null;
		                }
		                else
		                {	
		                	activeButton6 = button6;
		                	button6.setSelected(true);
		                }
				    }
			 });

			 
			 button7 = (Button) vue.findViewById(R.id.btn_day_dim);
			 button7.setOnClickListener(new View.OnClickListener() 
			 {
				    @Override
				    public void onClick(View v)
				    {
				    	if (activeButton7 != null) 
		        		{
		                    button7.setSelected(false);
		                    activeButton7 = null;
		                }
		                else
		                {	
		                	activeButton7 = button7;
		                	button7.setSelected(true);
		                }
				    }
			});

		 
		 
	}
	
	private void initialisation()
	{
		  Calendar calendar = Calendar.getInstance();
	      int current_day = calendar.get(Calendar.DAY_OF_WEEK);
	      
	      switch(current_day)
	    	{
	        	case 1:
	                	activeButton7 = button7; // dim
	                	button7.setSelected(true);
	                	break;
	        	case 2:
	                	activeButton1 = button1;  // lun
	                	button1.setSelected(true);
	                	break;
	        	case 3:
	                	activeButton2 = button2; // mar
	                	button2.setSelected(true);
	                	break;	
	        	case 4:
	                	activeButton3 = button3; // mer
	                	button3.setSelected(true);
	                	break;	
	        	case 5:
	                	activeButton4 = button4; // jeu
	                	button4.setSelected(true);
	                	break;
	        	case 6:
	                	activeButton5 = button5; // ven
	                	button5.setSelected(true);
	                	break;
	        	case 7:
	                	activeButton6 = button6; // sam
	                	button6.setSelected(true);
	                	break;	
	    	}
	      
	      
	}
	
	private void sauver_configuration()
    {
    	// lire l'état des boutons
    	
    	if (activeButton1 != null)
 		{
    		days_list_name = days_list_name + "LUN "; // 2
    		days_list_num = days_list_num + "2 "; 	
    		array_days_config_valeur[0]= 2 ;
    		array_days_config_name [0]= "LUN";
 		}
    	
    	if (activeButton2 != null)
 		{
    		days_list_name = days_list_name + "MAR "; // 3
    		days_list_num = days_list_num + "3 "; 	
    		array_days_config_valeur[1]= 3 ;
    		array_days_config_name [1]= "MAR";
 		}
    	
    	if (activeButton3 != null)
 		{
    		days_list_name = days_list_name + "MER "; // 4
    		days_list_num = days_list_num + "4 "; 	
    		array_days_config_valeur[2]= 4 ;
    		array_days_config_name [2]= "MER";
 		}
    	
    	if (activeButton4 != null)
 		{
    		days_list_name = days_list_name + "JEU ";  // 5
    		days_list_num = days_list_num + "5 "; 	
    		array_days_config_valeur[3]= 5 ;
    		array_days_config_name [3]= "JEU";
 		}
    
    	if (activeButton5 != null)
 		{
    		days_list_name = days_list_name + "VEN ";  // 6
    		days_list_num = days_list_num + "6 "; 	
    		array_days_config_valeur[4]= 6 ;
    		array_days_config_name [4]= "VEN";
 		}
    	
    	if (activeButton6 != null)
 		{
    		days_list_name = days_list_name + "SAM ";   // 7
    		days_list_num = days_list_num + "7 "; 	
    		array_days_config_valeur[5]= 7 ;
    		array_days_config_name [5]= "SAM";
 		}
    	
    	if (activeButton7 != null)
 		{
    		days_list_name = days_list_name + "DIM";  // 1
    		days_list_num = days_list_num + "1"; 	
    		array_days_config_valeur[6]= 1 ;
    		array_days_config_name [6]= "DIM";
 		}
  
    	// supprimer les espaces left et right
    	days_list_name = days_list_name.trim();
	    
    //	Toast.makeText(dialog_fragment.this, "Sauvegarde de l'alarme repeat" , Toast.LENGTH_SHORT).show();

    }

	
	
}