package com.androidbegin.buttonclicktutorial;

import android.content.ContentValues;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

// http://stackoverflow.com/questions/12001143/custom-listview-with-checkbox-single-selection

// http://www.thaicreate.com/mobile/android-listview-checkbox.html

// http://windrealm.org/tutorials/android/listview-with-checkboxes.php#downloads

// http://wptrafficanalyzer.in/blog/event-listener-for-checkboxes-in-listview/

// http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html

// http://www.devexchanges.info/2015/11/listview-with-checkbox-single-row.html

//http://windrealm.org/tutorials/android/listview-with-checkboxes.php#activity

public class RepeatCustomListView extends BaseAdapter
{

    private LayoutInflater lInflater;
    private List<RepeatObject> listStorage;

    ViewHolder listViewHolder;
    
    private int selectedPosition = -1;

	DB_alarm db;
    
    public RepeatCustomListView(Context context, List<RepeatObject> customizedListView) 
    {
        lInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        listStorage = customizedListView;
    }

    @Override
    public int getCount() {
        return listStorage.size();
    }

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    {

	        if(convertView == null)
	        {
	            listViewHolder = new ViewHolder();
	            convertView = lInflater.inflate(R.layout.debug_listview05, parent, false);
	
	            listViewHolder.indice = (TextView)convertView.findViewById(R.id.textView);
	            listViewHolder.titre = (TextView)convertView.findViewById(R.id.titre); // added
	            listViewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkBox);
	            
	            //listViewHolder.checkBox.setClickable(false);
	            //theCheckBox.setClickable(false);
	            
	            convertView.setTag(listViewHolder);
	        }
	        
	        else
	        {
	            listViewHolder = (ViewHolder)convertView.getTag();
	        }
	        
	        listViewHolder.indice.setText(listStorage.get(position).getIndice());
	        
	        //ItemObject s = listStorage.get(position);
	        //listViewHolder.titre.setText("titi");
	        
	        
	        //listViewItems.get(position).SetStatus("y");
	        
	        
	        
	        listViewHolder.titre.setText(listStorage.get(position).getTitre());
	        String val = listStorage.get(position).getStatus();
	        
	        if(val.equals("active"))
       		{
	        	 listViewHolder.checkBox.setChecked(true);
       		}
	        else
	        {	
		        listViewHolder.checkBox.setChecked(false);
	        }

	        /************************************/
	        
	        listViewHolder.checkBox.setOnClickListener(new View.OnClickListener() 
	        {
	            @Override
	            public void onClick(View v) 
	            {
	            	//RepeatObject aaa = listStorage.get(position);
	            	//aaa.getIndice();
	            	
	            	Toast.makeText(v.getContext(), "Petit clic", Toast.LENGTH_SHORT).show();	
	            }
            
	        });
/*	        
	        listViewHolder.checkBox.setOnClickListener(new View.OnClickListener() 
	        {
	            @Override
	            public void onClick(View v) 
	            {
	            	//Context context = null;
	            	Toast.makeText(v.getContext(), "Clicked Laugh Vote", Toast.LENGTH_SHORT).show();
	            	
					//Toast.makeText(context, "Sauve" , Toast.LENGTH_LONG).show();

	            	
	                if (listViewHolder.checkBox.isChecked())
	                {
	                    listViewHolder.checkBox.setChecked(false);
	                }else{
	                    listViewHolder.checkBox.setChecked(true);
	                }
	            }
	        });
*/	
	        
	        
	        listViewHolder.checkBox.setOnClickListener(onStateChangedListener(listViewHolder.checkBox, position));
	        
	        return convertView;
    }
 
   

	private OnClickListener onStateChangedListener(final CheckBox checkBox, final int position)
	{
		
		
		return new View.OnClickListener() 
		{
            @Override
            public void onClick(View v) 
            {
            	//Toast.makeText(v.getContext(), "clic" + String.valueOf(position), Toast.LENGTH_SHORT).show();	
            	
            	RepeatObject aaa = listStorage.get(position);
            	String indice = aaa.getIndice();
            	
                if (checkBox.isChecked()) 
                {
                	repeat_list03.listViewItems.get(position).SetStatus("active");
                	
                    selectedPosition = position;
                    //aaa.SetStatus("y");
                } 
                else 
                {
                    selectedPosition = -1;
                    //aaa.SetStatus("n");
                	repeat_list03.listViewItems.get(position).SetStatus("noactive");
                }
                
                // on viens mettre a jour la database
                repeat_list03.update();
                
               // notifyDataSetChanged();
            }
        };
		
		
	}



	/* 
    private View.OnClickListener onStateChangedListener(final CheckBox checkBox, final int position)
    {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBox.isChecked()) {
                    selectedPosition = position;
                } else {
                    selectedPosition = -1;
                }
                notifyDataSetChanged();
            }
        };
*/
    static class ViewHolder
    {
        TextView indice;
        TextView titre;
        CheckBox checkBox;
    }
    
  /*  
    private void update()
    {
    		db = new DB_alarm(getApplicationContext());
    	
	    	int size = repeat_list03.listViewItems.size();
			
			for(int x = 0; x < size; x = x + 1) 
			{
				String str_status = repeat_list03.listViewItems.get(x).getStatus();
				String str_indice = repeat_list03.listViewItems.get(x).getIndice();
				
				long now = System.currentTimeMillis();
				now = System.currentTimeMillis();
				ContentValues contentValues = new ContentValues();
				contentValues.put(DB_alarm.COLUMN_REPEAT_ACTIVE, str_status);
				contentValues.put(DB_alarm.COL_MODIFIEDTIME, now);
	    
				// mise a jour de la database
				
				//db.update(TABLE_REPEAT, contentValues, KEY_ID + " = ?", new String[]{str_indice});
				
	           	db.update_repeat(str_indice, contentValues);
	           	
	           	//db.execSQL("UPDATE DB_TABLE SET YOUR_COLUMN='newValue' WHERE id=6 ");
	           	
			}
		
    }
*/
    /*
	private Context getApplicationContext()
	{
		// TODO Auto-generated method stub
		return null;
	}
    
   */
}