package com.example.Maps013;

import android.os.Bundle;
import android.os.Environment;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class ListRUNDatas extends Activity 
{

	DB_GPS db;
	private SQLiteDatabase dataBase;

	String run_nom;
	
	TextView tv_nom;
	TextView tv_date;
	TextView tv_start;
	TextView tv_stop;
	TextView tv_distance;
	TextView tv_duration;
	Button btn_email;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
			super.onCreate(savedInstanceState);
			// Get the view from new_activity.xml
			setContentView(R.layout.listview_run_layout);
			
			db = new DB_GPS(getApplicationContext());
			
			ActionBar actionBar = getActionBar();
	        getActionBar().setHomeButtonEnabled(true);
	        getActionBar().setDisplayHomeAsUpEnabled(true);
	        
	        Bundle extras = getIntent().getExtras();
			run_nom = extras.getString("run_nom");
			
	        setTitle("Infos RUN: " + run_nom);

	     
			tv_nom = (TextView) this.findViewById(R.id.tv_nom);
			tv_date = (TextView) this.findViewById(R.id.tv_date); 
			tv_start = (TextView) this.findViewById(R.id.tv_start);
			tv_stop = (TextView) this.findViewById(R.id.tv_stop); 
			tv_distance = (TextView) this.findViewById(R.id.tv_distance);
			tv_duration = (TextView) this.findViewById(R.id.tv_duration); 
			btn_email = (Button) this.findViewById(R.id.btn_email); 
			
	        
	        ArrayList<PIPO> id =  db.zorro(run_nom); //db.Infos_RUN(run_nom);
	        
	        //databasePath = context.getDatabasePath("wl.db").getPath();
	        
	        String path = db.getDBpath();
	        
/*	        
	        File fileToCopy = new File(path);
	        File destinationFile = new File(Environment.getExternalStorageDirectory(),"filename");

	        FileInputStream fis = null;
			try {
				fis = new FileInputStream(fileToCopy);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        FileOutputStream fos = null;
			try {
				fos = new FileOutputStream(destinationFile);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

	        byte[] b = new byte[1024];
	        int noOfBytesRead;

	        try {
				while((noOfBytesRead = fis.read(b)) != -1)
					try {
						fos.write(b,0,noOfBytesRead);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	        try {
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        try {
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	*/        
	        
	      	        
	       // Uri uri = null; 
	    //   Uri uri = Uri.parse("file://" + "filename");
	        
	     //   uri = Uri.parse("file://" + "filename");
	        
	    //    Environment.getExternalStorageDirectory()
/*	        
	        File externalFile = new File(Environment.getExternalStorageDirectory(), "filename");
	        Uri uri = Uri.fromFile(externalFile);
	       
	        String email = "patrice.delpy@neuf.fr";
	        String subject = "Salut";
	        String message = "je vais bien tot va bien !";
	        
	        final Intent emailIntent = new Intent(  android.content.Intent.ACTION_SEND);
	        emailIntent.setType("plain/text");
	        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { email });
	        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
      
	        if (uri != null)
	        {
             emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
	        }
	        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
      
	        this.startActivity(Intent.createChooser(emailIntent,  "Sending email..."));
*/	        
	        
	        
	        
	        String nom = id.get(0).nom;
	        Long date = id.get(0).date;
	        Long start = id.get(0).start;
	        Long stop = id.get(0).stop;
	        Long steps = id.get(0).steps;
	        String mode = id.get(0).mode;
	        Double calories = id.get(0).calories;
	        Double vitesse = id.get(0).vitesse;
	        Double distance = id.get(0).distance;
	        //Double duration = id.get(0).duration;
	        
	        tv_nom.setText(nom);
	        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy - HH:mm:ss");
	        String time = sdf.format(date);
	        //tv_date.setText(time);
	        
	        String time_start = sdf.format(start);
	        tv_start.setText(time_start);

	        String time_stop = sdf.format(stop);
	        tv_stop.setText(time_stop);

	        
	         //in milliseconds
			long diff = stop - start;

			long diffSeconds = diff / 1000 % 60;
			long diffMinutes = diff / (60 * 1000) % 60;
			long diffHours = diff / (60 * 60 * 1000) % 24;
			long diffDays = diff / (24 * 60 * 60 * 1000);
			
			
			String s1 = String.valueOf(diffDays); 
   		    String s2 = String.valueOf(diffHours); 
   		    String s3 = String.valueOf(diffMinutes); 
   		    String s4 = String.valueOf(diffSeconds);
   		    
   		    if(!s1.equals("0") )
   		    {
   		    	s1 = s1 + " day ";
   		    }
   		    else s1 = "";
			
   		    if(!s2.equals("0") )
		    {
   		    	s2 = s2 + " H ";
		    }
   		    else s2 = "";
   		    
   		    if(!s3.equals("0") )
		    {
   		    	s3 = s3 + " Min ";
		    }
   		    else s3 = "";

   		    
   		    if(!s4.equals("0") )
		    {
   		    	s4 = s4 + " Sec ";
		    }
   		    else s4 = "";

	        
   		    //String duration = String.valueOf(diffDays) + " " + String.valueOf(diffHours) + " " + String.valueOf(diffMinutes) + " " + String.valueOf(diffSeconds);
			
   		    
			String duration = s1 + " " + s2 + " " + s3 + " " + s4;
			
			tv_duration.setText(duration);
			
			
			DecimalFormat format = new DecimalFormat("#.000");
		       
	        // number to string
			//String strDouble = String.valueOf(toBeString);
			String ttt = format.format(distance) +  " Km";
			
	       // System.out.println("$ : "+format.format(143.5));
			
			
			
			tv_distance.setText(String.valueOf(ttt));
			
			btn_email.setOnClickListener(new OnClickListener() 
			{

					@Override
					public void onClick(View arg0) 
					{
//				        SendEmail aaa = new SendEmail();
//				        aaa.toto();
						traitement_email();

						Toast.makeText(getApplicationContext(), "Email send", Toast.LENGTH_LONG).show();
					}

			});
			
			
	}
	
	
	public void traitement_email()
	{
		
		String path = db.getDBpath();
        
        
        File fileToCopy = new File(path);
        File destinationFile = new File(Environment.getExternalStorageDirectory(),"filename");

        FileInputStream fis = null;
		try 
		{
			fis = new FileInputStream(fileToCopy);
		} 
		catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
        FileOutputStream fos = null;
		try 
		{
			fos = new FileOutputStream(destinationFile);
		} 
		catch (FileNotFoundException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        byte[] b = new byte[1024];
        int noOfBytesRead;

        try 
        {
			while((noOfBytesRead = fis.read(b)) != -1)
				try
				{
					fos.write(b,0,noOfBytesRead);
				} 
				catch (IOException e) 
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		} 
        catch (IOException e1)
        {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        
        try 
        {
			fis.close();
		} 
        catch (IOException e) 
        {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        try 
        {
			fos.close();
		} 
        catch (IOException e)
        {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
          	        
        File externalFile = new File(Environment.getExternalStorageDirectory(), "filename");
        Uri uri = Uri.fromFile(externalFile);
       
        String email = "patrice.delpy@neuf.fr";
        String subject = "Salut";
        String message = "je vais bien tot va bien !";
        
        final Intent emailIntent = new Intent(  android.content.Intent.ACTION_SEND);
        emailIntent.setType("plain/text");
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { email });
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
  
        if (uri != null)
        {
         emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
        }
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
  
        this.startActivity(Intent.createChooser(emailIntent,  "Sending email..."));
        
		
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
	      switch (item.getItemId())
	      {
	      		case android.R.id.home:
	      			this.finish();
	      			return true;
	      
	      		

	      		default:
	      			return super.onOptionsItemSelected(item);
		 }
	}

   
   
    
   
	
}