package com.androidbegin.buttonclicktutorial;

import java.util.ArrayList;
import java.util.List;


import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

// http://wptrafficanalyzer.in/blog/enabling-multi-selection-mode-in-listview-by-adding-togglebutton-using-custom-layout-in-android/ 

// http://mobileapptechnology.blogspot.fr/2014/05/multiselection-mode-enabled-in-listview.html

//http://pragmaticjoe.blogspot.fr/2015/01/creating-textview-and-togglebutton-list.html

// https://danielme.com/2013/10/09/diseno-android-listview-con-checkbox/


public class repeat_list03 extends Activity
{

	
    static final List<RepeatObject> listViewItems = new ArrayList<RepeatObject>();
	
	static DB_alarm db;
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
			super.onCreate(savedInstanceState);
			setContentView(R.layout.repeat_display_list);
			
	        ActionBar actionBar = getActionBar();
	        getActionBar().setHomeButtonEnabled(true);
	        getActionBar().setDisplayHomeAsUpEnabled(true);
	        setTitle("Listing des repeats");
			
			db = new DB_alarm(getApplicationContext());
			
			/************************************************************/
/*	
			ListView repeatList = (ListView)findViewById(R.id.List);
			
			repeatList.setOnItemClickListener(new OnItemClickListener()
			{
						public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
						{

							Intent i = new Intent(getApplicationContext(), infos_repeat.class);
							i.putExtra("stafid", stafid.get(arg2));
							startActivity(i);
						}
			});
*/			
			
			
		    //List<RepeatObject> listViewItems = new ArrayList<RepeatObject>();
		    //listViewItems.clear();
			
		    
		    //listViewItems.add(new RepeatObject("1","aaaNigeria","y"));
		    //listViewItems.add(new RepeatObject("20","Ghana","n"));
		    //listViewItems.add(new RepeatObject("1","Senegal","y"));
		    //listViewItems.add(new RepeatObject("1","Togo","n"));
	
		    //listViewWithCheckBox.setAdapter(new RepeatCustomListView(this, listViewItems));
		    //listViewWithCheckBox.setOnItemClickListener(new AdapterView.OnItemClickListener() 
		    //{
		    //        @Override
		    //        public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
		    //        {
		    //            Toast.makeText(getApplicationContext(), "Position " + position, Toast.LENGTH_LONG).show();
		    //            // passe en mode edition
		    //        }
		    //});
		
	}	
	
	
	@Override
	protected void onResume() 
	{
		super.onResume();
		displayData();
		/////// disadpt.notifyDataSetChanged();
	}
	
	private void displayData() 
	{
		ListView listViewWithCheckBox = (ListView)findViewById(R.id.List);
		
	    listViewItems.clear();
		
		String requete = "SELECT * FROM table_repeat";
		
		Cursor mCursor = db.DB_query(requete);
		int val = mCursor.getCount();
	    Log.i("**************** => ", String.valueOf(val));
	
		
		//fetch each record
		if (mCursor.moveToFirst()) 
		{
			do {
					String indice = mCursor.getString(mCursor.getColumnIndex(DB_alarm.COLUMN_REPEAT_ID));
					String titre = mCursor.getString(mCursor.getColumnIndex(DB_alarm.COLUMN_REPEAT_NAME));
					String repeat_active = mCursor.getString(mCursor.getColumnIndex(DB_alarm.COLUMN_REPEAT_ACTIVE));  // "active" ou "noactive"
					
 				    listViewItems.add(new RepeatObject(indice, titre, repeat_active));
					
				
			} while (mCursor.moveToNext());
			
			
			listViewWithCheckBox.setAdapter(new RepeatCustomListView(this, listViewItems));
		    listViewWithCheckBox.setOnItemClickListener(new AdapterView.OnItemClickListener() 
		    {
		            @Override
		            public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
		            {
		                //Toast.makeText(getApplicationContext(), "Position " + position, Toast.LENGTH_LONG).show();
		                Intent i = new Intent(getApplicationContext(), infos_repeat03.class);
						i.putExtra("indice", position);
						startActivity(i);
		                
		            }
		    });

			
			
			mCursor.close();

		}
		
		/*
		public static final String TABLE_REPEAT = "table_repeat";
		public static final String COLUMN_REPEAT_ID = "_id";
		public static final String COLUMN_REPEAT_NAME = "repeat_name";	 
		public static final String COLUMN_REPEAT_TIME = "repeat_time";
		public static final String COLUMN_REPEAT_DAYS = "repeat_days";
		public static final String COLUMN_REPEAT_INTERVAL = "repeat_interval";
		public static final String COLUMN_REPEAT_ACTIVE = "alarm_active";	
		public static final String COLUMN_REPEAT_VIBRATE = "option_vibrate";
		public static final String COLUMN_REPEAT_RINGTONE = "option_ringtone";
		public static final String COLUMN_REPEAT_CREATED_TIME = "created_time";
		public static final String COLUMN_REPEAT_MODIFIED_TIME = "modified_time";
		*/
		
		
	}
	
	
	/**************************************************/
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.remind_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
	      switch (item.getItemId())
	      {

	      		case android.R.id.home:
	      			this.finish();
	      			return true;
	      			
	      		case R.id.add_event:
	      			Toast.makeText(getApplicationContext(),"Add event",Toast.LENGTH_SHORT).show();
	      			Intent Intent02 = new Intent(repeat_list03.this, AddRepeat02.class);
	      			startActivity(Intent02);
	      			return true;
		     
	      		case R.id.preferences:
	      			Toast.makeText(getApplicationContext(),"Not defined",Toast.LENGTH_SHORT).show();
	      			//Intent Intent01 = new Intent(repeat_list.this, SettingsActivity.class);
	      			//startActivity(Intent01);
	      			return true;
		    	
		    
	      		default:
		          return super.onOptionsItemSelected(item);
		 }
	}

/*	
	@Override
    public void onBackPressed() 
	{

		//String a, b, c, d;
		
		//a = listViewItems.get(0).getStatus();
		//b = listViewItems.get(1).getStatus();
		//c = listViewItems.get(2).getStatus();
		//d = listViewItems.get(3).getStatus();
		
		
		int size = listViewItems.size();
		
		for(int x = 0; x < size; x = x + 1) 
		{
			String str_status = listViewItems.get(x).getStatus();
			String str_indice = 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);
    
           	db.update_repeat(str_indice, contentValues);
		}
		
         
		Toast.makeText(getApplicationContext(),"Salut",Toast.LENGTH_SHORT).show();
        super.onBackPressed();

    }
*/	
	
	
	protected static void update()
	{
		int size = listViewItems.size();
		
		for(int x = 0; x < size; x = x + 1) 
		{
			String str_status = listViewItems.get(x).getStatus();
			String str_indice = 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);
    
           	db.update_repeat(str_indice, contentValues);
		}
	}
	
}
