package com.example.appli_calendar05;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.example.appli_calendar05.EventObjects;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class DatabaseQuery extends DatabaseObject
{
    private static final String TAG = Database.class.getSimpleName();
    
   
    
    public DatabaseQuery(Context context) 
    {
        super(context);
    }
    
    
    public List<EventObjects> getAllFutureEvents()
    {
        Date dateToday = new Date();
        List<EventObjects> events = new ArrayList<>();
        String query = "select * from events";
        Cursor cursor = this.getDbConnection().rawQuery(query, null);
        if(cursor.moveToFirst()){
            do{
                int id = cursor.getInt(0);
                String message = cursor.getString(cursor.getColumnIndexOrThrow("message"));
                String startDate = cursor.getString(cursor.getColumnIndexOrThrow("start_date"));
                //convert start date to date object
                Date reminderDate = convertStringToDate(startDate);
                if(reminderDate.after(dateToday) || reminderDate.equals(dateToday)){
                    events.add(new EventObjects(id, message, reminderDate));
                }
            }while (cursor.moveToNext());
        }
        cursor.close();
        return events;
    }
    
    private Date convertStringToDate(String dateInString){
        DateFormat format = new SimpleDateFormat("d-MM-yyyy", Locale.ENGLISH);
        Date date = null;
        try {
            date = format.parse(dateInString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    
    
    public static void creation_alarm()
   	{
          
   		/*String dateInString = "28-11-2016 22:00";
   		long  time_millis = Tools.convertir_date_timestamp(dateInString);
   	    long now = System.currentTimeMillis(); 
   	    String NAME = "28-11-2016";
   	    String FROMDATE =  "un_a";
   	    String TODATE =  "un_b";
   	    long DATETIME =  time_millis;
   	    String STATUS = "active";
   	    long CREATEDTIME =  now ;
   	    long MODIFIEDTIME =  now ;*/
    	
    	/*
    	    	SQLiteDatabase db = this.getWritableDatabase();
    	    	
    	    	ContentValues vals = new ContentValues();
    			long now = System.currentTimeMillis();
        		
    			vals.put(COL_NAME,  id.name);
    			vals.put(COL_FROMDATE, id.fromDate);
        		vals.put(COL_TODATE, id.toDate);
        		vals.put(COL_DATETIME, id.timestamp );
        		vals.put(COL_STATUS, "active");
        		vals.put(COL_CREATEDTIME, now);
        		vals.put(COL_MODIFIEDTIME, now);
        		vals.put(COL_RULE, id.rule);
        		vals.put(COL_INTERVAL, id.interval);
        		vals.put(COL_SOUND, id.sound ? 1 : 0);
        		vals.put(COL_INFO, id.info);
        		vals.put(COL_CREATED_AT, "");
        		
        		db.insert(TABLE, null, vals);*/
        	
        
    
    	ContentValues vals = new ContentValues();
    	vals.put("message",  "Salut");
    	vals.put("reminder",  "plouf");
    	vals.put("end",  "cretin");
    	
    	//getDbConnection().insert("events", null, vals);
    	
   	   
   	    
   	    //db.insert_alarm(NAME, FROMDATE, TODATE, DATETIME, STATUS, CREATEDTIME, MODIFIEDTIME);
   	}
    
}