package com.revoapp.calendar.database;

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

import com.revoapp.api.database.calendar.pojo.Events;

import java.util.ArrayList;

public class DatabaseHandler extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 3;

    private static final String DATABASE_NAME = "calendar_events";

    private static final String TABLE_EVENTS = "events";

    private static final String COL_ID = "id";
    private static final String COL_TITLE = "title";
    private static final String COL_MESSAGE = "message";
    private static final String COL_DATE = "date";
    private static final String COL_REMINDER = "isreminder";
    private static final String COL_ADDED_REMOTELY = "added_remotely";

    private SQLiteDatabase db;

    private void createDbObject() {
        db = this.getWritableDatabase();
    }

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_EVENTS_TABLE = "CREATE TABLE " + TABLE_EVENTS + " ("
                + COL_ID + " INTEGER PRIMARY KEY,"
                + COL_TITLE + " TEXT,"
                + COL_MESSAGE + " TEXT,"
                + COL_DATE + " INTEGER,"
                + COL_REMINDER + " INTEGER,"
                + COL_ADDED_REMOTELY + " INTEGER"
                + ")";

        db.execSQL(CREATE_EVENTS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_EVENTS);
        onCreate(db);
    }

    public void dropDatabase() {
        if (db == null || !db.isOpen())
            createDbObject();
        db.execSQL("DROP TABLE " + TABLE_EVENTS);

        onCreate(db);
    }

    public void addEvent(Events event) {
        if (db == null || !db.isOpen())
            createDbObject();

        ContentValues values = new ContentValues();
        values.put(COL_TITLE, event.getTitle());
        values.put(COL_MESSAGE, event.getMessage());
        values.put(COL_DATE, event.getDate());
        values.put(COL_REMINDER, event.getIsReminding());
        values.put(COL_ADDED_REMOTELY, event.isAddedRemotely());

        db.insert(TABLE_EVENTS, null, values);
        closeDB();
    }

    public ArrayList<Events> getAllEvents() {
        ArrayList<Events> eventsList = new ArrayList<Events>();

        String selectQuery = "SELECT  * FROM " + TABLE_EVENTS + " ORDER BY " + COL_DATE;

        if (db == null || !db.isOpen())
            createDbObject();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                Events event = new Events();
                if (cursor.getString(0) != null)
                    event.setId(Integer.parseInt(cursor.getString(0)));
                event.setTitle(cursor.getString(1));
                event.setMessage(cursor.getString(2));
                event.setDate(cursor.getLong(3));
                if (cursor.getString(5) != null)
                    event.setIsReminding(Integer.parseInt(cursor.getString(5)));

                eventsList.add(event);
            } while (cursor.moveToNext());
        }

        cursor.close();
        closeDB();
        return eventsList;
    }

    public void deleteEvent(Events event) {
        if (db == null || !db.isOpen())
            createDbObject();
        db.delete(TABLE_EVENTS, COL_ID + " = ?",
                new String[]{String.valueOf(event.getId())});
        closeDB();
    }

    public void deleteAllEvents() {
        if (db == null || !db.isOpen())
            createDbObject();
        db.delete(TABLE_EVENTS, COL_ADDED_REMOTELY + " = ?",
                new String[]{"1"});
        closeDB();
    }

    public void addEvents(ArrayList<Events> events) {
        if (events == null || events.isEmpty())
            return;

        if (db == null || !db.isOpen())
            createDbObject();

        for (Events event : events) {
            addEvent(event);
        }
        closeDB();
    }

    public void closeDB() {
        if (db != null)
            db.close();
        this.close();
    }
}