package com.tanya.careerguidanceproject

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

//import java.util.*

class DBHandler(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) {

    override fun onCreate(db: SQLiteDatabase) {

        val queryUser = ("CREATE TABLE " + TABLE_NAME_USER + " ("
                + USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + USER_NAME + " TEXT,"
                + USER_EMAIL_ID + " TEXT,"
                + USER_MOBILE_NUMBER + " NUMBER,"
                + USER_PASSWORD + " TEXT)")

        db.execSQL(queryUser)
    }

    fun addNewUser(
        UserName: String?,
        UserEmailId: String?,
        UserMobNo: String?,
        UserPassword: String?
    ) {

        val db = this.writableDatabase

        val values = ContentValues()

        values.put(USER_NAME, UserName)
        values.put(USER_EMAIL_ID, UserEmailId)
        values.put(USER_MOBILE_NUMBER, UserMobNo)
        values.put(USER_PASSWORD, UserPassword)

        db.insert(TABLE_NAME_USER, null, values)

        db.close()

    }

    fun updatePassword(email : String, password : String){

        val db = this.writableDatabase

        val values = ContentValues()

        values.put(USER_PASSWORD, password)

        db.update(TABLE_NAME_USER,values, "$USER_EMAIL_ID= ?", arrayOf(email))

        db.close()

    }

    fun checkUser(email: String, password: String): Boolean {
        // array of columns to fetch
        val columns = arrayOf(
            USER_ID
        )
        val db = this.readableDatabase
        // selection criteria
        val selection =
            "$USER_EMAIL_ID = ? AND $USER_PASSWORD = ?"
        // selection arguments
        val selectionArgs = arrayOf(email, password)
        // query user table with conditions
        /**
         * Here query function is used to fetch records from user table this function works like we use sql query.
         * SQL query equivalent to this query function is
         * SELECT user_id FROM user WHERE user_email = 'jack@androidtutorialshub.com' AND user_password = 'qwerty';
         */
        val cursor = db.query(
            TABLE_NAME_USER,  //Table to query
            columns,  //columns to return
            selection,  //columns for the WHERE clause
            selectionArgs,  //The values for the WHERE clause
            null,  //group the rows
            null,  //filter by row groups
            null
        ) //The sort order
        val cursorCount = cursor.count
        cursor.close()
        db.close()
        return cursorCount > 0
    }

    override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
        // this method is called to check if the table exists already.
        db.execSQL("DROP TABLE IF EXISTS $TABLE_NAME_USER")
        onCreate(db)
    }

    companion object{
        // creating a constant variables for our database.
        // below variable is for our database name.
        private const val DB_NAME = "UserRecord"

        // below int is our database version
        private const val DB_VERSION = 1

        // below variable is for our table name.
        private const val TABLE_NAME_USER = "User_record"

        // below variable is for our id column.
        private const val USER_ID = "User_Id"

        // below variable is for our course name column
        private const val USER_NAME = "User_Name"

        // below variable id for our course duration column.
        private const val USER_EMAIL_ID = "User_EmailId"

        // below variable for our course description column.
        private const val USER_MOBILE_NUMBER = "User_MobNo"

        // below variable is for our course tracks column.
        private const val USER_PASSWORD = "User_Password"

    }
}

