package com.example.GPS_DB01;

import android.os.Bundle;

import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity 
{
	Button button;
	EditText textReport;
    DAOtextReport daOtextReport;
  
    
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_main);
	
			daOtextReport = new DAOtextReport(this);
			
			textReport = (EditText) findViewById(R.id.report_edit_text);
			button = (Button) findViewById(R.id.MyButton);
			
			// Capture button clicks
			/*button.setOnClickListener(new OnClickListener() 
			{
				public void onClick(View arg0) {
	
					// Start NewActivity.class
					Intent myIntent = new Intent(MainActivity.this,	NewActivity.class);
					startActivity(myIntent);
				}
			});
			*/
			
			//remplir_db();
			
			
			TextReport id = new TextReport("not_defined", 50.01 , 60.10, 128.0, 758.85);
			//long result = daOtextReport.addReport02(id);
			
			id = new TextReport("plouf", 150.01 , 55.10, 4.0, 5);
			//result = daOtextReport.addReport02(id);
			
			String run_name = "plouf";
			
			int aid = 2;
			
			daOtextReport.zi();
	/*		
			Cursor cursor = query("table_gps", new String[]
					{ "name" ,  "latitude" , "longitude", "altitude", "vitesse" }, "_id" + "=?",  
	                new String[] { String.valueOf(aid) }, null, null, null, null);  
	*/		
			
		// https://www.javatpoint.com/android-sqlite-tutorial
		    
			
		//	String requete = "SELECT * FROM table_gps WHERE name = plouf"; //  WHERE name = " + run_name ;
		 	
		//	Cursor Cursor = daOtextReport.DB_query(requete);
		//	int val = Cursor.getCount();
			
			//  String requete = "SELECT * FROM table_appointment WHERE date = " + stimestamp ;
			
			
			
			
	}
	
	public void textReportOnClicked(View view) 
	{
			//Toast.makeText(getApplicationContext(), "Added to DB", Toast.LENGTH_SHORT).show();

			Random_generator msr = new Random_generator();
		
			String report = msr.generateRandomString();
			//String report = textReport.getText().toString();
			
        	long result = daOtextReport.addReport(new TextReport("junjunguo.com", report, 63.4187362, 10.4387621, 1234, 90));

        	if (result >= 0)
        	{
        		Toast.makeText(getApplicationContext(), "succsefully add report to row " + result, Toast.LENGTH_SHORT).show();
        		textReport.getText().clear();
        	} 
        	else 
        	{
        		Toast.makeText(getApplicationContext(), "not succeed ! please try again !", Toast.LENGTH_SHORT).show();
        	}
        	
    }
	
	
	
	public void remplir_db()
	{
	//	long result = daOtextReport.addReport(new TextReport("junjunguo.com", "report", 63.4187362, 10.4387621));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
			getMenuInflater().inflate(R.menu.activity_main, menu);
			return true;
	}
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) 
	{
       
        switch (item.getItemId())
        {
        case R.id.menu_remind:
  			Toast.makeText(getApplicationContext(),"Remind",Toast.LENGTH_SHORT).show();
  			Intent Intent04 = new Intent(MainActivity.this, NewActivity.class);
  			startActivity(Intent04);
	        return true;
        
  		case R.id.menu_repeat:
  			Toast.makeText(getApplicationContext(),"Repeat",Toast.LENGTH_SHORT).show();
  			Intent Intent05 = new Intent(MainActivity.this, Control_Service.class);
  			startActivity(Intent05);
	        return true;

  		case R.id.menu_list_run:
  			Toast.makeText(getApplicationContext(),"Repeat",Toast.LENGTH_SHORT).show();
  			Intent Intent08 = new Intent(MainActivity.this, Listrun.class);
  			startActivity(Intent08);
	        return true;    
	        
  		case R.id.menu_about:
  			Toast.makeText(getApplicationContext(),"CAbout",Toast.LENGTH_SHORT).show();
  			Intent Intent06 = new Intent(MainActivity.this, GPS_tracker_maps.class);
  			startActivity(Intent06);
	        return true;

  		case R.id.menu_quit:
  			Toast.makeText(getApplicationContext(),"Salut on ferme la boutique...",Toast.LENGTH_SHORT).show();
  			finish();
	        return true;    
          
  		default:
          return super.onOptionsItemSelected(item);
        
        }
    }
	
	
	
}