package com.example.offlinestore1

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.Switch
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity

class SettingsActivity : AppCompatActivity() {

    private lateinit var btnManagePassword: Button
    private lateinit var switchGrid: Switch
    private lateinit var prefs: PreferencesHelper

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

        prefs = PreferencesHelper(this)

        btnManagePassword = findViewById(R.id.btnManagePassword)
        switchGrid = findViewById(R.id.switchGrid)

        updateUi()

        switchGrid.isChecked = prefs.isGridMode()
        switchGrid.setOnCheckedChangeListener { _, isChecked ->
            prefs.saveViewMode(isChecked)
        }

        btnManagePassword.setOnClickListener {
            showManagePasswordDialog()
        }
    }

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

    override fun onStop() {
        super.onStop()
        LoginActivity.runningActivities--
        if (LoginActivity.runningActivities <= 0) {
            LoginActivity.isAuthenticated = false
        }
    }

    private fun updateUi() {
        if (prefs.hasPassword()) {
            btnManagePassword.text = "לשינוי הסיסמה"
        } else {
            btnManagePassword.text = "הוספת סיסמה"
        }
    }

    private fun showManagePasswordDialog() {
        val dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_manage_password, null)
        val dialog = AlertDialog.Builder(this)
            .setView(dialogView)
            .create()

        val txtTitle = dialogView.findViewById<TextView>(R.id.txtDialogTitle)
        val edtOld = dialogView.findViewById<EditText>(R.id.edtOldPassword)
        val edtNew = dialogView.findViewById<EditText>(R.id.edtNewPassword)
        val btnSave = dialogView.findViewById<Button>(R.id.btnSave)
        val btnRemove = dialogView.findViewById<Button>(R.id.btnRemove)

        val hasPassword = prefs.hasPassword()
        
        if (hasPassword) {
            txtTitle.text = "לשינוי הסיסמה"
            edtOld.visibility = View.VISIBLE
            btnRemove.visibility = View.VISIBLE
        } else {
            txtTitle.text = "הוספת סיסמה"
            edtOld.visibility = View.GONE
            btnRemove.visibility = View.GONE
        }

        btnSave.setOnClickListener {
            val newPwd = edtNew.text.toString().trim()
            if (newPwd.isEmpty()) {
                Toast.makeText(this, "יש להזין סיסמה חדשה", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            if (hasPassword) {
                val oldPwd = edtOld.text.toString().trim()
                if (prefs.checkPassword(oldPwd)) {
                    prefs.savePassword(newPwd)
                    Toast.makeText(this, "הסיסמה שונתה בהצלחה", Toast.LENGTH_SHORT).show()
                    dialog.dismiss()
                    updateUi()
                } else {
                    Toast.makeText(this, "סיסמה נוכחית שגויה", Toast.LENGTH_SHORT).show()
                }
            } else {
                prefs.savePassword(newPwd)
                Toast.makeText(this, "הסיסמה הוגדרה בהצלחה", Toast.LENGTH_SHORT).show()
                dialog.dismiss()
                updateUi()
            }
        }

        btnRemove.setOnClickListener {
            val oldPwd = edtOld.text.toString().trim()
            if (prefs.checkPassword(oldPwd)) {
                prefs.clearPassword()
                Toast.makeText(this, "הסיסמה הוסרה", Toast.LENGTH_SHORT).show()
                dialog.dismiss()
                updateUi()
            } else {
                Toast.makeText(this, "יש להזין סיסמה נוכחית כדי להסיר", Toast.LENGTH_SHORT).show()
            }
        }

        dialog.show()
    }
}
