package com.example.defenseapplication.view

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import com.example.defenseapplication.R
import com.example.defenseapplication.databinding.DialogPasswordInputBinding

class PasswordInputDialog(context: Context) : Dialog(context) {

    private lateinit var binding: DialogPasswordInputBinding
    private var onConfirmListener: ((String) -> Unit)? = null
    private var onDismissListener: (() -> Unit)? = null

    init {
        initView()
    }

    private fun initView() {
        binding = DialogPasswordInputBinding.inflate(LayoutInflater.from(context))
        setContentView(binding.root)

        window?.apply {
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            val params = attributes
            params.gravity = Gravity.CENTER
            params.width = WindowManager.LayoutParams.MATCH_PARENT
            params.height = WindowManager.LayoutParams.WRAP_CONTENT
            params.horizontalMargin = 40f
            attributes = params
        }

        setupPasswordInput()

        binding.btnCancel.setOnClickListener {
            dismiss()
        }

        binding.btnConfirm.setOnClickListener {
            val password = getPassword()
            if (password.length == 6) {
                onConfirmListener?.invoke(password)
                dismiss()
            }
        }
    }

    private fun setupPasswordInput() {
        val editTexts = listOf(
            binding.etPassword1,
            binding.etPassword2,
            binding.etPassword3,
            binding.etPassword4,
            binding.etPassword5,
            binding.etPassword6
        )

        editTexts.forEachIndexed { index, editText ->
            editText.setOnKeyListener { _, _, _ ->
                if (editText.text.isNotEmpty()) {
                    if (index < editTexts.size - 1) {
                        editTexts[index + 1].requestFocus()
                    } else {
                        editText.clearFocus()
                    }
                }
                false
            }

            editText.setOnFocusChangeListener { v, hasFocus ->
                if (hasFocus) {
                    (v as EditText).setSelection((v.text?.length ?: 0))
                }
            }
        }
    }

    private fun getPassword(): String {
        return binding.etPassword1.text.toString() +
                binding.etPassword2.text.toString() +
                binding.etPassword3.text.toString() +
                binding.etPassword4.text.toString() +
                binding.etPassword5.text.toString() +
                binding.etPassword6.text.toString()
    }

    fun setOnConfirmListener(listener: (String) -> Unit): PasswordInputDialog {
        this.onConfirmListener = listener
        return this
    }

    fun setOnDismissListener(listener: () -> Unit): PasswordInputDialog {
        this.onDismissListener = listener
        return this
    }

    fun showError(message: String) {
        binding.tvError.text = message
        binding.tvError.visibility = View.VISIBLE
    }

    fun clearError() {
        binding.tvError.visibility = View.GONE
        binding.tvError.text = ""
    }

    fun clearPassword() {
        binding.etPassword1.text?.clear()
        binding.etPassword2.text?.clear()
        binding.etPassword3.text?.clear()
        binding.etPassword4.text?.clear()
        binding.etPassword5.text?.clear()
        binding.etPassword6.text?.clear()
        binding.etPassword1.requestFocus()
    }

    override fun dismiss() {
        super.dismiss()
        onDismissListener?.invoke()
    }
}