package com.tanya.careerguidanceproject.activities

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.tanya.careerguidanceproject.DBHandler
import com.tanya.careerguidanceproject.R
import com.tanya.careerguidanceproject.UserModal

var res : Boolean  = false

class LoginActivity : AppCompatActivity() {

    private lateinit var etEmail : EditText
    private lateinit var etPassword : EditText
    private lateinit var btnLogin : Button
    private lateinit var btnForgotPassword : Button
    private lateinit var btnRegister : Button

    private var userModalArrayList: ArrayList<UserModal>? = null
    private var dbHandler: DBHandler? = null

    //private val validMobileNumber = "9876543210"
    //private val validPassword = arrayOf("qwe","asd","zxc")

    private lateinit var sharedPreferences: SharedPreferences

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        sharedPreferences = getSharedPreferences(getString(R.string.preference_file_name), Context.MODE_PRIVATE)

        val isLoggedIn = sharedPreferences.getBoolean("isLoggedIn",false)
        setContentView(R.layout.activity_login)

        if(isLoggedIn) {
            val intent = Intent(this@LoginActivity, HomeActivity::class.java)
            startActivity(intent)
            finish()
        }

        title = "Log In"

        etEmail = findViewById(R.id.etEmailLogin)
        etPassword = findViewById(R.id.etPasswordLogin)
        btnLogin = findViewById(R.id.btnLogIn)
        btnForgotPassword = findViewById(R.id.btnForgotPasswordLogin)
        btnRegister = findViewById(R.id.btnRegisterLogin)

        userModalArrayList = java.util.ArrayList()
        dbHandler = DBHandler(this@LoginActivity)

        btnLogin.setOnClickListener {

            //val mobileNumber = etMobileNumber.text.toString()
            //val password = etPassword.text.toString()

            res = true

            //if((validMobileNumber == mobileNumber) && (validPassword.contains(password))) {
            //if (dbHandler!!.verifyUserRecords(mobileNumber,password)) {

            if (dbHandler!!.checkUser(etEmail.text.toString().trim()
                    , etPassword.text.toString().trim())) {
                /*Intent accountsIntent = new Intent(activity, UsersListActivity.class);
                accountsIntent.putExtra("EMAIL", textInputEditTextEmail.getText().toString().trim());
                emptyInputEditText();
                startActivity(accountsIntent);
            */
                savePreferences()
                val intent = Intent(this@LoginActivity, HomeActivity::class.java)
                intent.putExtra("Email",etEmail.text.toString().trim())
                startActivity(intent)

            }else {
                Toast.makeText(
                    this@LoginActivity,
                    "Incorrect username or password",
                    Toast.LENGTH_LONG
                ).show()
            }

        }

        btnForgotPassword.setOnClickListener {

            Toast.makeText(this@LoginActivity,
                "Forgot Password button clicked",
                Toast.LENGTH_LONG
            ).show()

            val intent = Intent(this@LoginActivity, ForgotPasswordActivity::class.java)
            startActivity(intent)
        }

        btnRegister.setOnClickListener {

            Toast.makeText(this@LoginActivity,
                "Sign Up button clicked",
                Toast.LENGTH_LONG
            ).show()

            val intent = Intent(this@LoginActivity, RegistrationActivity::class.java)
            startActivity(intent)
        }
    }

    override fun onPause() {
        super.onPause()
        finish()
    }

    private fun savePreferences(){
        sharedPreferences.edit().putBoolean("loggedIn",true).apply()
        res = true
    }
}