package com.example.accelerometre01;



import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

// http://stackoverflow.com/questions/15570542/determining-the-speed-of-a-vehicle-using-gps-in-android

public class MainActivity extends Activity
{
	
	Button btn_start, btn_stop;
	TextView infos;
	
	Thread thread;  
	
	Double distance = 0.0;
	Double calories = 0.0;
	Double velocity = 0.0;

	int step_length = 0;  
	int weight = 0;       
	int total_step = 0;   
	
	long timer = 0;
	long startTimer = 0;
	long tempTime = 0;

	
	private Handler handler = new Handler()
	{
	    @Override
	    public void handleMessage(Message msg) 
	    {

	    	super.handleMessage(msg);
	    	
	    	String reponse = msg.getData().getString("message");
	    	 
	    	//Toast.makeText(getApplicationContext(), reponse , Toast.LENGTH_SHORT).show();
	    	
	    	countStep();  
	    	countDistance();    
	    	

			if (timer != 0 && distance != 0.0)
			{
				calories = weight * distance * 0.001;
				velocity = distance * 1000 / timer;
			} 
			else 
			{
				calories = 0.0;
				velocity = 0.0;
			}
	    	
	    }
	};
	  
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_main);
			
			btn_start = (Button) findViewById(R.id.button1);    
			btn_stop = (Button) findViewById(R.id.button2);    
			infos = (TextView) findViewById(R.id.infos);   
			
			enable_buttons();
			
			// http://www.jiahaoliuliu.com/2011/05/android-sending-and-receiving-messages.html
			//	Thread With Handlers
		

			thread = new Thread() 
			{
				
				@Override
				public void run()
				{
					super.run();
					int temp = 0;
					while (true)
					{
						try
						{
							Thread.sleep(5000);
						}
						catch (InterruptedException e) 
						{
							e.printStackTrace();
						}
						
					
						if (temp != service02.CURRENT_SETP) 
						{
							temp = service02.CURRENT_SETP;
						}
						
						if (startTimer != System.currentTimeMillis()) 
						{
							timer = tempTime + System.currentTimeMillis() - startTimer;
						}
						
						
						String msg = "alpha";
						
						Message msgObj = handler.obtainMessage();
                        Bundle b = new Bundle();
                        b.putString("message", msg);
                        msgObj.setData(b);
                        handler.sendMessage(msgObj);
/*                        
                        Message myMessage;
                        Bundle messageBundle=new Bundle();
                     // Envoyer le message au Handler (la méthode handler.obtainMessage est plus efficace
                        // que créer un message à partir de rien, optimisation du pool de message du Handler)
                        //Instanciation du message (la bonne méthode):
                        myMessage=handler.obtainMessage();    
                        //Ajouter des données à transmettre au Handler via le Bundle
                        messageBundle.putInt("coco", 1);
                       //Ajouter le Bundle au message
                        myMessage.setData(messageBundle);
                        //Envoyer le message
                        handler.sendMessage(myMessage);
*/						
                     
						
					}
				}
			};
			
			thread.start();
	}

	

    @Override
    protected void onResume()
    {
        super.onResume();
        
        init();
    }    
	
	private void init() 
	{
		Intent service = new Intent(this, service02.class);
		stopService(service);
		service02.CURRENT_SETP = 0;
		tempTime = timer = 0;
		handler.removeCallbacks(thread);
		
	}
	
	private void countDistance() 
	{
		if (service02.CURRENT_SETP % 2 == 0) 
		{
			distance = (service02.CURRENT_SETP / 2) * 3 * step_length * 0.01;
		} 
		else 
		{
			distance = ((service02.CURRENT_SETP / 2) * 3 + 1) * step_length * 0.01;
		}
	}
	
	
	private void countStep() 
	{
		if (service02.CURRENT_SETP % 2 == 0) 
		{
			total_step = service02.CURRENT_SETP;
		} 
		else 
		{
			total_step = service02.CURRENT_SETP +1;
		}

		total_step = service02.CURRENT_SETP;
	}
	
	
	
	
	@SuppressWarnings("deprecation")
	protected void onDestroy()
	{
		//thread.interrupt();
		super.onDestroy();
		thread.destroy();
		//thread.interrupt();  
		thread=null;
	}
	
	@Override
	public void onBackPressed()
	{
		// TODO Auto-generated method stub
		super.onBackPressed();
		finish();
	}
	
	private void enable_buttons()
	{
		btn_start.setOnClickListener(new OnClickListener() 
		{
				@Override
				public void onClick(View arg0) 
				{
					Intent i = new Intent(getApplicationContext(), service.class);
					startService(i);
					Toast.makeText(getApplicationContext(), "Start Podomtre service", Toast.LENGTH_LONG).show();
				}
		});
		
		btn_stop.setOnClickListener(new OnClickListener() 
		{
				@Override
				public void onClick(View arg0) 
				{
					Intent i = new Intent(getApplicationContext(), service.class);
					stopService(i);
					Toast.makeText(getApplicationContext(), "Stop Podometre service : down", Toast.LENGTH_LONG).show();
				}

		});
	}
	
}
