package com.example.offlinestore1

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class LoginActivity : AppCompatActivity() {

    companion object {
        var isAuthenticated = false
        var runningActivities = 0
    }

    private lateinit var edtPassword: EditText
    private lateinit var btnLogin: Button
    private lateinit var prefs: PreferencesHelper

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        prefs = PreferencesHelper(this)

        // אם אין סיסמה בכלל, נכנסים ישר
        if (!prefs.hasPassword()) {
            isAuthenticated = true
            startActivity(Intent(this, MainActivity::class.java))
            finish()
            return
        }

        setContentView(R.layout.activity_login)

        edtPassword = findViewById(R.id.edtPassword)
        btnLogin = findViewById(R.id.btnLogin)

        btnLogin.setOnClickListener {
            val input = edtPassword.text.toString()
            if (prefs.checkPassword(input)) {
                isAuthenticated = true
                startActivity(Intent(this, MainActivity::class.java))
                finish()
            } else {
                Toast.makeText(this, "סיסמה שגויה", Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun onStart() {
        super.onStart()
        runningActivities++
    }

    override fun onStop() {
        super.onStop()
        runningActivities--
    }
}