package org.deke.risk.riskahead.helper;

import java.util.HashMap;

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;

public class SQLiteHandler extends SQLiteOpenHelper {

    private static final String TAG = SQLiteHandler.class.getSimpleName();

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

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

    // Login table name
    private static final String TABLE_LOGIN = "login";

    // Login Table Columns names
    private static final String KEY_UID = "uid";
    private static final String KEY_USERNAME = "username";
    private static final String KEY_NAME = "name";
    private static final String KEY_SURNAME = "surname";
    private static final String KEY_EMAIL = "email";
    private static final String KEY_STATUS = "status";
    private static final String PROVIDER_TYPE = "providerType";
    private static final String KEY_CREATED_AT = "created_at";
    private static final String KEY_UPDATED_AT = "updated_at";
    private static final String TOKEN = "token";

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

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
                + KEY_UID + " INTEGER PRIMARY KEY,"
                + KEY_USERNAME + " TEXT,"
                + KEY_NAME + " TEXT,"
                + KEY_SURNAME + " TEXT,"
                + KEY_EMAIL + " TEXT UNIQUE,"
                + KEY_STATUS + " TEXT,"
                + PROVIDER_TYPE + " TEXT,"
                + KEY_UPDATED_AT + " TEXT,"
                + KEY_CREATED_AT + " TEXT,"
                + TOKEN + " TEXT"+ ")";
        db.execSQL(CREATE_LOGIN_TABLE);

        Log.d(TAG, "Database tables created");
    }

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

    /**
     * Storing user details in database
     * *//*
    public void addUser(String uid, String username, String name, String surname, String email, String status, String provider_type, String created_at, String updated_at, String token) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_UID, uid); //UID
        values.put(KEY_USERNAME, username); // Username
        values.put(KEY_NAME, name); // Name
        values.put(KEY_SURNAME, surname); // Surame
        values.put(KEY_EMAIL, email); // Email
        values.put(KEY_STATUS, status); // Status
        values.put(PROVIDER_TYPE, provider_type); // ProviderType
        values.put(KEY_CREATED_AT, created_at); // Created At
        values.put(KEY_UPDATED_AT, updated_at); // Updated At
        values.put(TOKEN, token); // TOKEN

        // Inserting Row
        long id = db.insert(TABLE_LOGIN, null, values);
        db.close(); // Closing database connection

        Log.d(TAG, "New user inserted into sqlite: " + id);
    }

    public void updateUser(String uid, String username, String name, String surname, String email, String status, String provider_type, String created_at, String updated_at, String token) {
        deleteUsers();
        addUser(uid,username,name,surname,email,status,provider_type,created_at,updated_at,token);
    }

    *//**
     * Getting user data from database
     * *//*
    public HashMap<String, String> getUserDetails() {
        HashMap<String, String> user = new HashMap<String, String>();
        String selectQuery = "SELECT  * FROM " + TABLE_LOGIN;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        // Move to first row
        cursor.moveToFirst();
        if (cursor.getCount() > 0) {
            user.put("uid", cursor.getString(0));
            user.put("username", cursor.getString(1));
            user.put("name", cursor.getString(2));
            user.put("surname", cursor.getString(3));
            user.put("email", cursor.getString(4));
            user.put("status", cursor.getString(5));
            user.put("providerType", cursor.getString(6));
            user.put("created_at", cursor.getString(7));
            user.put("updated_at", cursor.getString(8));
            user.put("token", cursor.getString(9));
        }
        cursor.close();
        db.close();
        // return user
        Log.d(TAG, "Fetching user from Sqlite: " + user.toString());

        return user;
    }

    *//**
     * Getting user login status return true if rows are there in table
     * *//*
    public int getRowCount() {
        String countQuery = "SELECT  * FROM " + TABLE_LOGIN;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        int rowCount = cursor.getCount();
        db.close();
        cursor.close();

        // return row count
        return rowCount;
    }

    *//**
     * Re crate database Delete all tables and create them again
     * *//*
    public void deleteUsers() {
        SQLiteDatabase db = this.getWritableDatabase();
        // Delete All Rows
        db.delete(TABLE_LOGIN, null, null);
        db.close();

        Log.d(TAG, "Deleted all user info from sqlite");
    }
    */


}
