package com.quantumics;

/**
 * Created by macbookair on 3/7/19.
 */

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 DBHelper extends SQLiteOpenHelper {

    public static class WifiData{
        public static String TABLE_NAME="WIFI";
        public static String COLUMN_WIFI="WIFI_DATA";
        public static String COLUMN_ID="ID";
        public static String COLUMN_TIMESTAMP="TIMESTAMP";
        public static final String CREATE_TABLE =
                "CREATE TABLE " + TABLE_NAME + "("
                        + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                        + COLUMN_WIFI + " TEXT,"
                        + COLUMN_TIMESTAMP + " DATETIME DEFAULT CURRENT_TIMESTAMP"
                        + ")";

        private int id;
        private String wifiData;
        private String timestamp;

        public WifiData(){

        }

        public WifiData(int id, String wifiData, String timestamp){
            this.id = id;
            this.wifiData = wifiData;
            this.timestamp = timestamp;
        }

        public void setId(int id){
            this.id = id;
        }

        public void setWifiData(String wifiData){
            this.wifiData =wifiData;
        }

        public void setTimestamp(String timestamp){
            this.timestamp = timestamp;
        }

        public int getId(){
            return this.id;
        }

        public String getWifiData(){
            return this.wifiData;
        }
    }

    public static class WifiP2PData{
        public static String TABLE_NAME="WIFIP2P";
        public static String COLUMN_WIFIP2P="WIFIP2P_DATA";
        public static String COLUMN_ID="ID";
        public static String COLUMN_TIMESTAMP="TIMESTAMP";
        public static final String CREATE_TABLE =
                "CREATE TABLE " + TABLE_NAME + "("
                        + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                        + COLUMN_WIFIP2P + " TEXT,"
                        + COLUMN_TIMESTAMP + " DATETIME DEFAULT CURRENT_TIMESTAMP"
                        + ")";


        private int id;
        private String wifiP2PData;
        private String timestamp;

        public WifiP2PData(){

        }


        public WifiP2PData(int id, String wifiP2PData, String timestamp){
            this.id = id;
            this.wifiP2PData = wifiP2PData;
            this.timestamp = timestamp;
        }

        public void setId(int id){
            this.id = id;
        }

        public void setWifiP2PData(String wifiP2PData){
            this.wifiP2PData =wifiP2PData;
        }

        public void setTimestamp(String timestamp){
            this.timestamp = timestamp;
        }

        public int getId(){
            return this.id;
        }

        public String getWifiP2PData(){
            return this.wifiP2PData;
        }
    }


    public static class GeolocationData{
        public static String TABLE_NAME="GEOLOCATION";
        public static String COLUMN_GEOLOCATION="GEOLOCATION_DATA";
        public static String COLUMN_ID="ID";
        public static String COLUMN_TIMESTAMP="TIMESTAMP";
        public static final String CREATE_TABLE =
                "CREATE TABLE " + TABLE_NAME + "("
                        + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                        + COLUMN_GEOLOCATION + " TEXT,"
                        + COLUMN_TIMESTAMP + " DATETIME DEFAULT CURRENT_TIMESTAMP"
                        + ")";


        private int id;
        private String geolocationData;
        private String timestamp;

        public GeolocationData(){

        }

        public GeolocationData(int id, String geolocationData, String timestamp){
            this.id = id;
            this.geolocationData = geolocationData;
            this.timestamp = timestamp;
        }

        public void setId(int id){
            this.id = id;
        }

        public void setGeolocationData(String geolocationData){
            this.geolocationData =geolocationData;
        }

        public void setTimestamp(String timestamp){
            this.timestamp = timestamp;
        }

        public int getId(){
            return this.id;
        }

        public String getGeolocationData(){
            return this.geolocationData;
        }
    }


    // DBHelper Version
    private static final int DATABASE_VERSION = 1;

    // DBHelper Name
    private static final String DATABASE_NAME = "proximity";


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



    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {

        // create notes table
        db.execSQL(WifiData.CREATE_TABLE);
        db.execSQL(WifiP2PData.CREATE_TABLE);
        db.execSQL(GeolocationData.CREATE_TABLE);
    }

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

        // Create tables again
        onCreate(db);
    }


    public long insertWifiData(String wifiData) {
        // get writable database as we want to write data
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        // `id` and `timestamp` will be inserted automatically.
        // no need to add them
        values.put(WifiData.COLUMN_WIFI, wifiData);

        // insert row
        long id = db.insert(WifiData.TABLE_NAME, null, values);

        // close db connection
        db.close();

        // return newly inserted row id
        return id;
    }



    public long insertWifiP2PData(String wifip2pData) {
        // get writable database as we want to write data
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        // `id` and `timestamp` will be inserted automatically.
        // no need to add them
        values.put(WifiP2PData.COLUMN_WIFIP2P, wifip2pData);

        // insert row
        long id = db.insert(WifiP2PData.TABLE_NAME, null, values);

        // close db connection
        db.close();

        // return newly inserted row id
        return id;
    }





    public long insertGeolocation(String geolocationData) {
        // get writable database as we want to write data
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        // `id` and `timestamp` will be inserted automatically.
        // no need to add them
        values.put(GeolocationData.COLUMN_GEOLOCATION, geolocationData);

        // insert row
        long id = db.insert(GeolocationData.TABLE_NAME, null, values);

        // close db connection
        db.close();

        // return newly inserted row id
        return id;
    }




    public WifiData getWifiData(long id) {
        // get readable database as we are not inserting anything
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(WifiData.TABLE_NAME,
                new String[]{WifiData.COLUMN_ID, WifiData.COLUMN_WIFI, WifiData.COLUMN_TIMESTAMP},
                WifiData.COLUMN_ID + "=?",
                new String[]{String.valueOf(id)}, null, null, null, null);

        if (cursor != null)
            cursor.moveToFirst();

        // prepare note object
        WifiData wifidata = new WifiData(
                cursor.getInt(cursor.getColumnIndex(WifiData.COLUMN_ID)),
                cursor.getString(cursor.getColumnIndex(WifiData.COLUMN_WIFI)),
                cursor.getString(cursor.getColumnIndex(WifiData.COLUMN_TIMESTAMP)));

        // close the db connection
        cursor.close();

        return wifidata;
    }





    public WifiP2PData getWifiP2PData(long id) {
        // get readable database as we are not inserting anything
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(WifiP2PData.TABLE_NAME,
                new String[]{WifiP2PData.COLUMN_ID, WifiP2PData.COLUMN_WIFIP2P, WifiP2PData.COLUMN_TIMESTAMP},
                WifiP2PData.COLUMN_ID + "=?",
                new String[]{String.valueOf(id)}, null, null, null, null);

        if (cursor != null)
            cursor.moveToFirst();

        // prepare note object
        WifiP2PData wifiP2Pdata = new WifiP2PData(
                cursor.getInt(cursor.getColumnIndex(WifiP2PData.COLUMN_ID)),
                cursor.getString(cursor.getColumnIndex(WifiP2PData.COLUMN_WIFIP2P)),
                cursor.getString(cursor.getColumnIndex(WifiP2PData.COLUMN_TIMESTAMP)));

        // close the db connection
        cursor.close();

        return wifiP2Pdata;
    }




    public GeolocationData getGeolocationData(long id) {
        // get readable database as we are not inserting anything
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(GeolocationData.TABLE_NAME,
                new String[]{GeolocationData.COLUMN_ID, GeolocationData.COLUMN_GEOLOCATION, GeolocationData.COLUMN_TIMESTAMP},
                GeolocationData.COLUMN_ID + "=?",
                new String[]{String.valueOf(id)}, null, null, null, null);

        if (cursor != null)
            cursor.moveToFirst();

        // prepare note object
        GeolocationData geolocationdata = new GeolocationData(
                cursor.getInt(cursor.getColumnIndex(GeolocationData.COLUMN_ID)),
                cursor.getString(cursor.getColumnIndex(GeolocationData.COLUMN_GEOLOCATION)),
                cursor.getString(cursor.getColumnIndex(GeolocationData.COLUMN_TIMESTAMP)));

        // close the db connection
        cursor.close();

        return geolocationdata;
    }




    public List<WifiData> getAllWifiData() {
        List<WifiData> wifiDatas = new ArrayList<>();

        // Select All Query
        String selectQuery = "SELECT  * FROM " + WifiData.TABLE_NAME + " ORDER BY " +
                WifiData.COLUMN_TIMESTAMP + " DESC";

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

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                WifiData wifiData = new WifiData();
                wifiData.setId(cursor.getInt(cursor.getColumnIndex(WifiData.COLUMN_ID)));
                wifiData.setWifiData(cursor.getString(cursor.getColumnIndex(WifiData.COLUMN_WIFI)));
                wifiData.setTimestamp(cursor.getString(cursor.getColumnIndex(WifiData.COLUMN_TIMESTAMP)));

                wifiDatas.add(wifiData);
            } while (cursor.moveToNext());
        }

        // close db connection
        db.close();

        // return notes list
        return wifiDatas;
    }






    public List<WifiP2PData> getAllWifiP2PData() {
        List<WifiP2PData> wifiP2PDatas= new ArrayList<>();

        // Select All Query
        String selectQuery = "SELECT  * FROM " + WifiP2PData.TABLE_NAME + " ORDER BY " +
                WifiData.COLUMN_TIMESTAMP + " DESC";

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

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                WifiP2PData wifiP2PData = new WifiP2PData();
                wifiP2PData.setId(cursor.getInt(cursor.getColumnIndex(WifiP2PData.COLUMN_ID)));
                wifiP2PData.setWifiP2PData(cursor.getString(cursor.getColumnIndex(WifiP2PData.COLUMN_WIFIP2P)));
                wifiP2PData.setTimestamp(cursor.getString(cursor.getColumnIndex(WifiP2PData.COLUMN_TIMESTAMP)));

                wifiP2PDatas.add(wifiP2PData);
            } while (cursor.moveToNext());
        }

        // close db connection
        db.close();

        // return notes list
        return wifiP2PDatas;
    }




    public List<GeolocationData> getAllGeolocationData() {
        List<GeolocationData> geolocationDatas= new ArrayList<>();

        // Select All Query
        String selectQuery = "SELECT  * FROM " + GeolocationData.TABLE_NAME + " ORDER BY " +
                WifiData.COLUMN_TIMESTAMP + " DESC";

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

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                GeolocationData geolocationData = new GeolocationData();
                geolocationData.setId(cursor.getInt(cursor.getColumnIndex(GeolocationData.COLUMN_ID)));
                geolocationData.setGeolocationData(cursor.getString(cursor.getColumnIndex(GeolocationData.COLUMN_GEOLOCATION)));
                geolocationData.setTimestamp(cursor.getString(cursor.getColumnIndex(GeolocationData.COLUMN_TIMESTAMP)));

                geolocationDatas.add(geolocationData);
            } while (cursor.moveToNext());
        }

        // close db connection
        db.close();

        // return notes list
        return geolocationDatas;
    }







    public int getWifiCount() {
        String countQuery = "SELECT  * FROM " + WifiData.TABLE_NAME;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);

        int count = cursor.getCount();
        cursor.close();


        // return count
        return count;
    }



    public int getWifiP2PCount() {
        String countQuery = "SELECT  * FROM " + WifiP2PData.TABLE_NAME;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);

        int count = cursor.getCount();
        cursor.close();


        // return count
        return count;
    }




    public int getGeolocationCount() {
        String countQuery = "SELECT  * FROM " + GeolocationData.TABLE_NAME;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);

        int count = cursor.getCount();
        cursor.close();


        // return count
        return count;
    }







    public int updateWifiData(WifiData wifiData) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(WifiData.COLUMN_WIFI, wifiData.getWifiData());

        // updating row
        return db.update(WifiData.TABLE_NAME, values, WifiData.COLUMN_ID + " = ?",
                new String[]{String.valueOf(wifiData.getId())});
    }







    public int updateWifiP2PData(WifiP2PData wifiP2PData) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(WifiP2PData.COLUMN_WIFIP2P, wifiP2PData.getWifiP2PData());

        // updating row
        return db.update(WifiP2PData.TABLE_NAME, values, WifiP2PData.COLUMN_ID + " = ?",
                new String[]{String.valueOf(wifiP2PData.getId())});
    }





    public int updateGeolocationData(GeolocationData geolocationData) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(GeolocationData.COLUMN_GEOLOCATION, geolocationData.getGeolocationData());

        // updating row
        return db.update(GeolocationData.TABLE_NAME, values, GeolocationData.COLUMN_ID + " = ?",
                new String[]{String.valueOf(geolocationData.getId())});
    }





    public void deleteWifiData(WifiData wifiData) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(WifiData.TABLE_NAME, WifiData.COLUMN_ID + " = ?",
                new String[]{String.valueOf(wifiData.getId())});
        db.close();
    }





    public void deleteWifiP2PData(WifiP2PData wifiP2PData) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(WifiP2PData.TABLE_NAME, WifiData.COLUMN_ID + " = ?",
                new String[]{String.valueOf(wifiP2PData.getId())});
        db.close();
    }



    public void deleteGeolocationData(GeolocationData geolocationData) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(GeolocationData.TABLE_NAME, WifiData.COLUMN_ID + " = ?",
                new String[]{String.valueOf(geolocationData.getId())});
        db.close();
    }



    public void deleteAllWifiData() {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(WifiData.TABLE_NAME, null, null);
        db.close();
    }


    public void deleteAllWifiP2PData() {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(WifiP2PData.TABLE_NAME, null, null);
        db.close();
    }


    public void deleteAllGeolocationData() {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(GeolocationData.TABLE_NAME, null, null);
        db.close();
    }

}