package com.example.shridev1;

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

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

public class DatabaseHandler extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "shrideManager";
    private static final String TABLE_SHRIDE = "shride";
    private static final String KEY_ID = "id";
    private static final String KEY_WHEREFROM = "whereFrom";
    private static final String KEY_WHERETO = "whereTo";
    private static final String KEY_PICKUP = "pickup";
    private static final String KEY_SEATS = "seats";
    private static final String KEY_SPACE = "space";
    private static final String KEY_CAR = "car";
    private static final String KEY_PREFERENCES = "preferences";
    private static final String KEY_ENVIRONMENT = "environment";



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


    // Creating tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_SHRIDE_TABLE = "CREATE TABLE " + TABLE_SHRIDE + "("
            + KEY_ID + "INTEGER PRIMARY KEY," + KEY_WHEREFROM + "TEXT, "
            + KEY_WHERETO + " TEXT, " + KEY_PICKUP + " TEXT, " + KEY_SEATS + " TEXT, "
            + KEY_SPACE + " TEXT, " + KEY_CAR + " TEXT, " + KEY_PREFERENCES + " TEXT, "
            + KEY_ENVIRONMENT + " TEXT" + ")";
        db.execSQL(CREATE_SHRIDE_TABLE);
    }

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

        // Create tables again
        onCreate(db);
    }

    void addTrip(Shride shride) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_WHEREFROM, shride.getWhereFrom());
        values.put(KEY_WHERETO, shride.getWhereTo());
        values.put(KEY_PICKUP, shride.getPickup());
        values.put(KEY_SEATS, shride.getSeats());
        values.put(KEY_SPACE, shride.getSpace());
        values.put(KEY_CAR, shride.getCar());
        values.put(KEY_PREFERENCES, shride.getPreferences());
        values.put(KEY_ENVIRONMENT, shride.getEnvironment());

        // Inserting row
        db.insert(TABLE_SHRIDE, null, values);

        db.close(); // Closing database connection
    }

    // code to get the single trip
    Shride getTrip(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_SHRIDE, new String[] { KEY_ID, KEY_WHEREFROM, KEY_WHERETO, KEY_PICKUP,
    KEY_SEATS, KEY_SPACE, KEY_CAR, KEY_PREFERENCES, KEY_ENVIRONMENT}, KEY_ID + "=?",
            new String[] { String.valueOf(id)}, null, null, null, null);

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

        Shride shride = new Shride(Integer.parseInt(cursor.getString(0)),
        cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4),
                cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8));

        return shride;
    }

    // Code to get all items in a list view

    public List getAllTrips() {
        List trips = new ArrayList();

        // Select all query
        String selectQuery = "SELECT * FROM " + TABLE_SHRIDE;

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

        // Looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Shride shride = new Shride();
                shride.setId(Integer.parseInt(cursor.getString(0)));
                shride.setWhereFrom(cursor.getString(1));
                shride.setWhereTo(cursor.getString(2));
                shride.setPickup(cursor.getString(3));
                shride.setSeats(cursor.getString(4));
                shride.setSpace(cursor.getString(5));
                shride.setCar(cursor.getString(6));
                shride.setPreferences(cursor.getString(7));
                shride.setEnvironment(cursor.getString(8));

                // Adding trip to list
                trips.add(shride);
            } while (cursor.moveToNext());
        }
        return trips;
    }

    // code to update single trip
    public int updateTrip(Shride shride) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_WHEREFROM, shride.getWhereFrom());
        values.put(KEY_WHERETO, shride.getWhereTo());
        values.put(KEY_PICKUP, shride.getPickup());
        values.put(KEY_SEATS, shride.getSeats());
        values.put(KEY_SPACE, shride.getSpace());
        values.put(KEY_CAR, shride.getCar());
        values.put(KEY_ENVIRONMENT, shride.getEnvironment());
        values.put(KEY_PREFERENCES, shride.getPreferences());

        // Updating row
        return db.update(TABLE_SHRIDE, values, KEY_ID + " = ?",
                new String[] { String.valueOf(shride.getID()) });
    }

    // Deleting single trip
    public void deleteTrip(Shride shride) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_SHRIDE, KEY_ID + " = ?",
                new String[] { String.valueOf(shride.getID()) } );
        db.close();
    }

    // Getting trip count
    public int getTripCount() {
        String countQuery = "SELECT * FROM " + TABLE_SHRIDE;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

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

}
