package com.example.moslah_hamza.statsdemo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class DataBaseConnection extends SQLiteOpenHelper {
    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 2;

    // Database Name
    private static final String DATABASE_NAME = "MoneyTracker";

    private SQLiteDatabase db;

    // Contacts table name
    private static final String TABLE_Name = "Limite";

    // Contacts Table Columns names
    private static final String KEY_ID = "limitId";
    private static final String KEY_MONTANT = "montant";
    private static final String KEY_Mode = "mode";
    private static final String KEY_DATE_Deb = "dateDebut";
    private static final String KEY_DATE_Fin = "dateFin";
    private static final String KEY_CATEGORIE = "categorie";

    public DataBaseConnection(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d("db", "created");
        String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_Name + " ("
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_MONTANT + " REAL," + KEY_Mode + " TEXT, "
                + KEY_DATE_Deb + " TEXT," + KEY_DATE_Fin + " TEXT, " + KEY_CATEGORIE + " TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    public void createDataBase() {
        this.db = getWritableDatabase();
        this.onCreate(db);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_Name);

        // Create tables again
        onCreate(db);
    }

    // Adding new contact
    public void addLimite(Limite limite) {
        SQLiteDatabase db = this.getWritableDatabase();
        Limite limite1=this.getLimite(limite.getCat());

        if(limite1.getMontant()==0){
            ContentValues values = new ContentValues();
            values.put(KEY_CATEGORIE, limite.getCat());
            values.put(KEY_Mode, limite.getMode());
            values.put(KEY_DATE_Deb, String.valueOf(limite.getDateDebut()));
            values.put(KEY_DATE_Fin, String.valueOf(limite.getDateFin()));
            values.put(KEY_MONTANT, limite.getMontant());

            // Inserting Row
            db.insert(TABLE_Name, null, values);
            db.close(); // Closing database connection
        }
        else {
            limite.setLimitId(limite1.getLimitId());
            this.updateLimite(limite);
        }
    }

    // Getting single contact
    public Limite getLimite(String id) {
        SQLiteDatabase db = this.getReadableDatabase();
        Limite limite=new Limite();

        Cursor cursor = db.query(TABLE_Name, new String[]{KEY_ID,KEY_Mode,
                        KEY_CATEGORIE, KEY_MONTANT, KEY_DATE_Deb,KEY_DATE_Fin}, KEY_CATEGORIE + "=?",
                new String[]{id}, null, null, null, null);
        if (cursor.moveToFirst()){

            limite= new Limite(cursor.getString(0), cursor.getDouble(3), cursor.getString(4),
                    cursor.getString(5), cursor.getString(2), cursor.getString(1));
        }

        // return contact
        return limite;
    }

    // Getting All Contacts
    public List<Limite> getAllLimits() {
        List<Limite> contactList = new ArrayList<Limite>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_Name;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Limite lmt = new Limite();
                lmt.setLimitId(cursor.getString(0));
                lmt.setMontant(cursor.getFloat(1));
                lmt.setDateDebut(cursor.getString(3));
                lmt.setDateFin(cursor.getString(4));
                lmt.setCat(cursor.getString(5));
                lmt.setMode(cursor.getString(2));
                // Adding contact to list
                contactList.add(lmt);
            } while (cursor.moveToNext());
        }

        // return contact list
        return contactList;
    }

    // Getting contacts Count
    public int getLimitsCount() {
        String countQuery = "SELECT  * FROM " + TABLE_Name;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }

    // Updating single contact
    public int updateLimite(Limite limite) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_CATEGORIE, limite.getCat());
        values.put(KEY_Mode, limite.getMode());
        values.put(KEY_DATE_Deb, String.valueOf(limite.getDateDebut()));
        values.put(KEY_DATE_Fin, String.valueOf(limite.getDateFin()));
        values.put(KEY_MONTANT, limite.getMontant());

        // updating row
        return db.update(TABLE_Name, values, KEY_ID + " = ?",
                new String[]{String.valueOf(limite.getLimitId())});
    }

    // Deleting single contact
    public void deleteLimite(Limite contact) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_Name, KEY_ID + " = ?",
                new String[]{String.valueOf(contact.getLimitId())});
        db.close();
    }
}
