package com.example.GPS_DB01;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;


public class DAOtextReport 
{
    private SQLiteDatabase database;
    private DBHelper dbHelper;

    public DAOtextReport(Context context)
    {
        dbHelper = new DBHelper(context);
        database = dbHelper.getWritableDatabase();
    }

    public void close() 
    {
        dbHelper.close();
    }
    
    public long addReport(TextReport textReport) 
    {
        ContentValues cv = new ContentValues();
        //cv.put(DBtables.TextReport.COLUMN_ID, textReport.getRunid());
        cv.put(DBtables.TextReport.COLUMN_REPORT, textReport.getReport());
        cv.put(DBtables.TextReport.COLUMN_ISREPOETED, textReport.isIsreported());
        cv.put(DBtables.TextReport.COLUMN_LONGITUDE, textReport.getLongitude());
        cv.put(DBtables.TextReport.COLUMN_LATITUDE, textReport.getLatitude());
        cv.put(DBtables.TextReport.COLUMN_ALTITUDE, textReport.getAltitude());
        cv.put(DBtables.TextReport.COLUMN_SPEED, textReport.getSpeed());
        cv.put(DBtables.TextReport.COLUMN_DATETIME, System.currentTimeMillis());

        return database.insert(DBtables.TextReport.TABLE_NAME, null, cv);
    }

    
    public long addReport02(TextReport textReport) 
    {
        ContentValues cv = new ContentValues();
    
        cv.put(DBHelper.COLUMN_NAME, textReport.getName());
        cv.put(DBHelper.COLUMN_LATITUDE, textReport.getLatitude());
        cv.put(DBHelper.COLUMN_LONGITUDE, textReport.getLongitude());
        cv.put(DBHelper.COLUMN_ALTITUDE, textReport.getAltitude());
        cv.put(DBHelper.COLUMN_VITESSE, textReport.getSpeed());
    
        return database.insert(DBHelper.TABLE_GPS, null, cv);
    }
    
    
    public long add_run(String name, long date, long start , long stop, long steps, String mode, double calories, double vitesse_moyenne, double distance, long duration ) 
    {
    	
        ContentValues cv = new ContentValues();
        cv.put( dbHelper.COLUMN_NAME_RUN , name);
        cv.put( dbHelper.COLUMN_DATE_RUN , date);
        cv.put( dbHelper.COLUMN_START_RUN , start);
        cv.put( dbHelper.COLUMN_STOP_RUN , stop);
        cv.put( dbHelper.COLUMN_STEPS_COUNT_RUN , steps);
        cv.put( dbHelper.COLUMN_MODE_RUN, mode);
        cv.put( dbHelper.COLUMN_CALORIES_RUN , calories);
        cv.put( dbHelper.COLUMN_VITESSE_MOYENNE_RUN , vitesse_moyenne);
        cv.put( dbHelper.COLUMN_DISTANCE_RUN , distance);
        cv.put( dbHelper.COLUMN_DURATION_RUN , duration);

        return database.insert(dbHelper.TABLE_RUN_SUMMARY, null, cv);
        
    }
    

    public long updateIsReported(TextReport textReport) 
    {
        ContentValues cv = new ContentValues();
        cv.put(DBtables.TextReport.COLUMN_ISREPOETED, textReport.isIsreported());

        String where = DBtables.TextReport.COLUMN_ID + "=" + textReport.getRunid() + "AND" +
                	   DBtables.TextReport.COLUMN_REPORT + "=" + textReport.getReport() + "AND" +
                	   DBtables.TextReport.COLUMN_DATETIME + "=" +
                	   textReport.getDatetime().getTimeInMillis();
        
        return database.update(DBtables.TextReport.TABLE_NAME, cv, where, null);
    }

    public List<TextReport> getAllTextReports()
    {
        List<TextReport> textReports = new ArrayList<TextReport>();

        Cursor cursor =  database.query(DBtables.TextReport.TABLE_NAME, DBtables.TextReport.ALL_COLUMNS, null, null, null, null, DBtables.TextReport.COLUMN_DATETIME + " DESC");

        cursor.moveToFirst();
        
        while (!cursor.isAfterLast()) 
        {
            TextReport textReport = cursorToTextReport(cursor);
            textReports.add(textReport);
            cursor.moveToNext();
        }
        cursor.close();
        return textReports;
    }


   
    public int getRowCount()
    {
        String countQuery = "SELECT  * FROM " + DBtables.TextReport.TABLE_NAME;
        Cursor cursor = database.rawQuery(countQuery, null);
        int count = cursor.getCount();
        cursor.close();
        return count;
    }

  
    private TextReport cursorToTextReport(Cursor cursor) 
    {
        TextReport tr = new TextReport();

        tr.setRunid(cursor.getString(cursor.getColumnIndex(DBtables.TextReport.COLUMN_ID)));
        tr.setReport(cursor.getString(cursor.getColumnIndex(DBtables.TextReport.COLUMN_REPORT)));
        tr.setDatetime(cursor.getLong(cursor.getColumnIndex(DBtables.TextReport.COLUMN_DATETIME)));
        tr.setLongitude(cursor.getDouble(cursor.getColumnIndex(DBtables.TextReport.COLUMN_LONGITUDE)));
        tr.setLatitude(cursor.getDouble(cursor.getColumnIndex(DBtables.TextReport.COLUMN_LATITUDE)));
        
        tr.setAltitude(cursor.getDouble(cursor.getColumnIndex(DBtables.TextReport.COLUMN_ALTITUDE)));
        tr.setSpeed(cursor.getDouble(cursor.getColumnIndex(DBtables.TextReport.COLUMN_SPEED)));
        // here we convert int to boolean
        
        tr.setIsreported(cursor.getInt(cursor.getColumnIndex(DBtables.TextReport.COLUMN_ISREPOETED)) > 0);
        return tr;
    }
    
    
    public Cursor DB_query(String query) 
    {
    	    	
	   	 	Cursor cursor = database.rawQuery(query, null);
	   	 	if (cursor != null)
	   	 	{
	   	 		cursor.moveToFirst();
	   	 	}
	   	 	return cursor;
    }
    
    
    public void zi()
    {
    		
    	int id = 2;
    	
		Cursor cursor = database.query("table_gps", new String[]
				{ "name" ,  "latitude" , "longitude", "altitude", "vitesse" }, "_id" + "=?",  
                new String[] { String.valueOf(id) }, null, null, null, null);  
		
    	
		 if (cursor != null)  
		 {	 
			 int aaa = cursor.getColumnIndex(DBHelper.COLUMN_ALTITUDE);
			 String z = cursor.getString(0);
			 
				double string_end_timestamp = cursor.getDouble( cursor.getColumnIndex(DBHelper.COLUMN_ALTITUDE) ) ;
			 
			 
			 
		 }	 
    }
}
