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 java.util.ArrayList;
import java.util.List;

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

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

    // Contacts table name
    private static final String TABLE_CATEGORIES = "categories";

    // Contacts Table Columns names
    private static final String KEY_DESCRIPTION = "description";
    private static final String KEY_TYPE = "type";
    private static final String KEY_PRIORITY = "priority";

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

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_CATEGORIES + "("
                + KEY_DESCRIPTION + " TEXT PRIMARY KEY," + KEY_PRIORITY +
                 " TEXT," +KEY_TYPE+ " TEXT)";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

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

        // Create tables again
        onCreate(db);
    }

    // Adding new Categorie
    public void addContact(Categorie contact) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_DESCRIPTION, contact.getDescription());
        values.put(KEY_PRIORITY, contact.getPriority());
        values.put(KEY_TYPE, contact.getType());

        // Inserting Row
        db.insert(TABLE_CATEGORIES, null, values);
        db.close(); // Closing database connection
    }

    // Getting single contact
    public Categorie getContact(String id) {
        SQLiteDatabase db = this.getReadableDatabase();
        Categorie contact=new Categorie();

        Cursor cursor = db.query(TABLE_CATEGORIES, new String[] { KEY_DESCRIPTION,
                        KEY_PRIORITY ,KEY_TYPE}, KEY_DESCRIPTION + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor.moveToFirst())

        contact = new Categorie(
                cursor.getString(0), Integer.parseInt(cursor.getString(1)),cursor.getString(2));
         return contact;
    }

    // Getting All Contacts
    public List<Categorie> getAllCategories(String type) {
        List<Categorie> contactList = new ArrayList<Categorie>();
        SQLiteDatabase db = this.getWritableDatabase();
        // Select All Query
        Cursor cursor = db.query(TABLE_CATEGORIES, new String[]{KEY_DESCRIPTION,
                        KEY_PRIORITY, KEY_TYPE}, KEY_TYPE + "=?",
                new String[]{type}, null, null, null, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Categorie contact = new Categorie();

                contact.setDescription(cursor.getString(0));
                contact.setPriority(Integer.parseInt(cursor.getString(1)));
                contact.setType(type);
                // Adding contact to list
                contactList.add(contact);
            } while (cursor.moveToNext());
        }

        return contactList;
    }

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

        return cursor.getCount();
    }
    // Updating single Categorie
  public int updateCategorie(Categorie cat,String Description) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_DESCRIPTION, cat.getDescription());
        values.put(KEY_PRIORITY, cat.getPriority());
         values.put(KEY_TYPE, cat.getType());

        // updating row
        return db.update(TABLE_CATEGORIES, values, KEY_DESCRIPTION + " = ?",
                new String[]{String.valueOf(Description)});
    }

    public void deleteCategorie(Categorie cat) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_CATEGORIES, KEY_DESCRIPTION + " = ?",
                new String[]{String.valueOf(cat.getDescription())});
        db.close();
    }

    public void createdb() {
        this.onCreate(getWritableDatabase());
    }
}
