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 android.widget.Toast;

/**
 * A class that handles the database methods that are necessary
 */

public class MyDatabaseHelper extends SQLiteOpenHelper {

    private Context context;
    private static final String DATABASE_NAME = "Shride.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "ride_list";
    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 MyDatabaseHelper(@Nullable Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_NAME + " ("
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + 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(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    }

    void addTrip(String whereFrom, String whereTo, String pickup, String seats,
                 String space, String car, String preferences, String environment) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();

        cv.put(KEY_WHEREFROM, whereFrom);
        cv.put(KEY_WHERETO, whereTo);
        cv.put(KEY_PICKUP, pickup);
        cv.put(KEY_SEATS, seats);
        cv.put(KEY_SPACE, space);
        cv.put(KEY_CAR, car);
        cv.put(KEY_PREFERENCES, preferences);
        cv.put(KEY_ENVIRONMENT, environment);

       db.insert(TABLE_NAME, null, cv);

    }

    Cursor readAllData() {
        String query = "SELECT  * FROM " + TABLE_NAME;
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = null;
        if (db != null) {
            cursor = db.rawQuery(query, null);
        }
        return cursor;
    }


}
