package com.example.Maps013;


import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

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

// https://github.com/krrishnaaaa/CustomListViewDemo
//http://android.pcsalt.com/create-list-using-listview-android/


// https://www.mikeplate.com/2010/01/21/show-a-context-menu-for-long-clicks-in-an-android-listview/

// https://github.com/mikeplate/ListViewDemo

public class Listing_run extends Activity 
{

	DB_GPS db;
	private SQLiteDatabase dataBase;
	
    ListView lvDetail;
    Context context = Listing_run.this;
    
    ArrayList<RUN> myList = new ArrayList<RUN>();
   
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listing_run_layout);
        
    	ActionBar actionBar = getActionBar();
        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        setTitle("Liste des RUNS:");
        
    	db = new DB_GPS(getApplicationContext());

        lvDetail = (ListView) findViewById(R.id.lvCustomList);
       
        getDataInList();
        lvDetail.setAdapter(new ListBaseAdapter(context, myList));
        registerForContextMenu(lvDetail);  
        
        //  clicks on item
        lvDetail.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
        	public void onItemClick(AdapterView<?> parentAdapter, View view, int position, long id) 
        	{
        		Toast.makeText(Listing_run.this, " Long click to open contect menu",   Toast.LENGTH_SHORT).show();
            }
        });
        
        
        
    }

    private void getDataInList() 
    {
        myList  = db.RUNgetAllRecords();
    }

    
    @Override   
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)  
    {  
            super.onCreateContextMenu(menu, v, menuInfo);  
            menu.setHeaderTitle("Select actions :");    
            menu.add(0, v.getId(), 0, "View RUN Infos");
            menu.add(0, v.getId(), 0, "View GPS Datas");   
            menu.add(0, v.getId(), 0, "View MAP");   
    }   
    
    @Override    
    public boolean onContextItemSelected(MenuItem item)
    {    
    		Intent Intent;
    		
    		  AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    	         int listPosition = info.position;
    	         
    	            	         
    	         //AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    	         //Object obj = lvDetail.getItemAtPosition(info.position);
    	         //String title = obj.toString();
    	         //String items = (String) lvDetail.getSelectedItem();
    	         
    	         
    	         View childView = lvDetail.getChildAt(info.position);
    	         TextView editText = (TextView) childView.findViewById(R.id.tvTitle);
    	         String run_nom = editText.getText().toString();
    	         
    	       //  TableRow tableRow = (TableRow) (lvDetail.getItemAtPosition(info.position));

    	       //  String project = ((TextView) tableRow.findViewById(R.id.tvTitle)).getText().toString();
    	         
    	      //   HashMap objs = (HashMap) lvDetail.getItemAtPosition(info.position);
    	      //   String idRecup = (String) objs.get("id");
    	         
    	     //    myList.get(listPosition).getTitle();//list item title
    	         
    	      //   adapter.getItem(listPosition);//list item title
    	
            if(item.getTitle()=="View RUN Infos")
            {  
                Intent = new Intent(Listing_run.this, ListRUNDatas.class);
            	Intent.putExtra("run_nom", run_nom);
      			startActivity(Intent);
            }    
            
            if(item.getTitle()=="View GPS Datas")
            {  
      			Intent = new Intent(Listing_run.this, ListGPSDatas.class);
      			Intent.putExtra("run_nom", run_nom);
      			startActivity(Intent);
            }
         
            if(item.getTitle()=="View MAP")
            {  
      			Intent = new Intent(Listing_run.this, GPS_tracker_maps.class);
      	        Intent.putExtra("run_nom", run_nom);
      			startActivity(Intent);
            }
            
            return true;    
      }    
   
    @Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
	      switch (item.getItemId())
	      {
	      		case android.R.id.home:
	      			this.finish();
	      			return true;
	      
	      		

	      		default:
	      			return super.onOptionsItemSelected(item);
		 }
	}
    
    
    
    
    
    protected void onListItemClick(ListView l, View v, int position, long id)
	{
		//String keyword = (String)this.getListAdapter().getItem(position);
		
		//Object o = this.getListAdapter().getItem(position);
		//String keyword = o.toString();
		// Object toto = l.getItemAtPosition(position);
		// String value = toto.toString();
		 
		 
		//Cursor c = (Cursor)l.getItemAtPosition(position);
		//String run_name = c.getString(c.getColumnIndex(db.COL_NOM_RUN ));
        //Intent intent = new Intent(Listrun.this, Display_datas_run.class);
        //intent.putExtra("run_name", run_name);
        //startActivity(intent);
		
    	 Toast.makeText(getApplicationContext(), "Position: " + position , Toast.LENGTH_SHORT).show();
    	
	}
    
/*    
    private void addItemClickListener(final ListView listView)
    {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
        {
	            @Override 
	            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
	            {
	                String itemValue = (String) listView.getItemAtPosition(position);
	                Toast.makeText(getApplicationContext(), "Position: " + position + " item: " + itemValue, Toast.LENGTH_SHORT).show();
	            }
        });
    }
*/    
    
    
    
}