package com.example.mysmartlab

import android.content.Intent
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import com.example.mysmartlab.databinding.ActivityPacientCartBinding
import com.example.mysmartlab.databinding.ActivityPasswordBinding

class ActivityPassword : AppCompatActivity() {
    lateinit var binding: ActivityPasswordBinding
    private var pas: String = ""
    private var btn: Button? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityPasswordBinding.inflate(layoutInflater)
        setContentView(binding.root)
        init()
    }
    private fun init() {
        val prefs = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
        binding.btnNum0.setOnClickListener { onClickNum(0) }
        binding.btnNum1.setOnClickListener { onClickNum(1) }
        binding.btnNum2.setOnClickListener { onClickNum(2) }
        binding.btnNum3.setOnClickListener { onClickNum(3) }
        binding.btnNum4.setOnClickListener { onClickNum(4) }
        binding.btnNum5.setOnClickListener { onClickNum(5) }
        binding.btnNum6.setOnClickListener { onClickNum(6) }
        binding.btnNum7.setOnClickListener { onClickNum(7) }
        binding.btnNum8.setOnClickListener { onClickNum(8) }
        binding.btnNum9.setOnClickListener { onClickNum(9) }
        binding.btnClear.setOnClickListener { onClickDel() }
        binding.btnSkip1.setOnClickListener {
            if (prefs.getBoolean("isCreateCard", true)) {
                val editor: SharedPreferences.Editor =
                    getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
                editor.putBoolean("isCreateCard", false).commit()
                val intent: Intent =
                    Intent(this@ActivityPassword, ActivityPacientCart::class.java)
                startActivity(intent)
            } else {
                val intent: Intent =
                    Intent(this@ActivityPassword, MainActivity::class.java)
                startActivity(intent)
            }
        }
        btn = binding.btnSkip1
        if (!prefs.getBoolean("isCreateCard", true)) {
            btn!!.visibility = View.INVISIBLE
        }
        if (!prefs.getBoolean("isPass", true)) {
            binding.txtCreate.text = "Введите пароль"
        }

    }

    private fun onClickNum(s: Int) {
        if (pas.length < 4) {
            pas += s.toString()

            when (pas.length) {
                1 -> binding.imgNum1.setImageResource(R.drawable.blue_krug)
                2 -> binding.imgNum2.setImageResource(R.drawable.blue_krug)
                3 -> binding.imgNum3.setImageResource(R.drawable.blue_krug)
                4 -> binding.imgNum4.setImageResource(R.drawable.blue_krug)
            }
        }
        if (pas.length == 4) {
            val prefs = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
            if (prefs.getBoolean("isPass", true)) {
                val editor: SharedPreferences.Editor =
                    getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
                editor.putBoolean("isPass", false).commit()
                editor.putInt("Pass", pas.hashCode())
                editor.putBoolean("isCreateCard", false)
                editor.apply()
                val intent: Intent =
                    Intent(this@ActivityPassword, MainActivity::class.java)
                startActivity(intent)
            } else {
                if (pas.hashCode() == prefs.getInt("Pass", 0)) {
                    val intent: Intent =
                        Intent(this@ActivityPassword, MainActivity::class.java)
                    startActivity(intent)
                }
            }
        }

    }

    private fun onClickDel() {

        if (pas.length > 0) {
            pas = pas.substring(0, pas.length - 1)

            when (pas.length) {
                0 -> binding.imgNum1.setImageResource(R.drawable.blue_pust_krug)
                1 -> binding.imgNum2.setImageResource(R.drawable.blue_pust_krug)
                2 -> binding.imgNum3.setImageResource(R.drawable.blue_pust_krug)
                3 -> binding.imgNum4.setImageResource(R.drawable.blue_pust_krug)
            }
        }
    }

}
