package com.example.GPS_DB02;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.List;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;

public class NewActivity extends Activity 
{

	DB_GPS db;
	private SQLiteDatabase dataBase;

	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
			super.onCreate(savedInstanceState);
			// Get the view from new_activity.xml
			setContentView(R.layout.listview_layout);
	}
	
	
	@Override protected void onResume() 
	{
	        super.onResume();
	        
	        db = new DB_GPS(getApplicationContext());
	    		
	        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getList());
	        ListView listView = (ListView) findViewById(R.id.my_listview);
	        listView.setAdapter(adapter);
	        addItemClickListener(listView);
    }

   
    public String[] getList() 
    {
	        //daOtextReport = new DAOtextReport(getApplicationContext());
    	
    	
    	    List<GPS> alist  = db.getAllRecords();
    	
    	    SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy  HH:mm:ss");
    	  
    	    
	        //List<TextReport> alist = daOtextReport.getAllRecord();
	        
	        String aaa = alist.get(0).toString();
	        
	        String[] list = new String[alist.size()];
	        for (int i = 0; i < alist.size(); i++) 
	        {
	        	
	        	//Double lat =  alist.get(i).latitude;
	      	    //Double lng =  alist.get(i).longitude;
	      	    //Double alt =  alist.get(i).altitude;
	      	    //Double vit = alist.get(i).vitesse;
	        	
	        	long time = alist.get(i).time;
	        	
	        	String str_time = df.format(time); 
	        	
	      	    String lat =  Double.toString(alist.get(i).latitude);
	      	    String lng =  Double.toString(alist.get(i).longitude);
	      	    String alt =  Double.toString(alist.get(i).altitude);
	      	    String speed =  Double.toString(alist.get(i).vitesse);
	      	   
	      	   
	        	//String s = " a " + " b "  + " c " + " d " ; 
	            String s = i + "     " + str_time + "\n " + lat + "       " + lng + "\n " + alt + "       "  + speed ;
	      	    list[i] = s;

//	            list[i] = alist.get(i).toString();
	        }
	        
	        //daOtextReport.close();
	        return list;
    }
    
    @Override 
    public String toString() 
    {
    	SimpleDateFormat df = new SimpleDateFormat("dd-M-yyyy HH:mm:ss");
    	
    	
    	return "můmmůůlůmůmům";
    	/*
    	
        return "runid='" + runid +
                ",      isreported=" + isreported +
                "\nreport='" + report +
                "\ndatetime=" + df.format(datetime.getTime()) +
                "\nlatitude=" + latitude +
                ", longitude=" + longitude +
                "\naltitude=" + altitude +
                ", speed=" + speed    ;
                */
    }
    
    
    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();
	            }
        });
    }
	
}