package com.example.smartlab

import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.Window
import android.view.WindowManager
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.smartlab.Api.ApiHttp
import com.example.smartlab.databinding.CodecheckPanelBinding
import com.example.smartlab.databinding.WelcomeWindowBinding
import retrofit2.Call
import retrofit2.Response
import java.util.*



class CodeCheckActivity : AppCompatActivity() {
lateinit var binding: CodecheckPanelBinding
var codecheck = ""
    lateinit var timer: CountDownTimer
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        window.requestFeature(Window.FEATURE_NO_TITLE)
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
        binding = CodecheckPanelBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.EditText1.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
            }
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if(binding.EditText1.text.toString().length == 1){
                    codecheck += s
                    binding.EditText2.requestFocus()
                }
            }
        })
        binding.EditText2.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
            }
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if(binding.EditText2.text.toString().length == 1){
                    codecheck += s
                    binding.EditText3.requestFocus()
                }
            }
        })
        binding.EditText3.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
            }
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if(binding.EditText3.text.toString().length == 1){
                    codecheck += s
                    binding.EditText4.requestFocus()
                }
            }
        })
        binding.EditText4.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
            }
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if(binding.EditText4.text.toString().length == 1){
                    codecheck += s
                }
                if(codecheck.length ==4){
                    SingIn()
                }
            }
        })
        Timer()
        }

    private fun Timer()
    {
        timer = object : CountDownTimer(60000, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                binding.Timer.setText("Отправить код повторно можно\nбудет через " + millisUntilFinished / 1000 + " секунд");
            }

            override fun onFinish() {
                binding.Timer.setText("Код отправлен повторно");
                SingIn()
                Timer()
            }
        }
        timer.start()
    }
    private fun SingIn() {
        ApiHttp.retrifitApi.CheckCode(ApiHttp.User_mail, codecheck)
            .enqueue(object : retrofit2.Callback<String> {
                override fun onFailure(call: Call<String>, t: Throwable) {
                    Toast.makeText(this@CodeCheckActivity, t.message, Toast.LENGTH_SHORT).show()
                }

                override fun onResponse(call: Call<String>, response: Response<String>) {
                    if (response.code() == 200) {
                        timer.cancel()
                        var intent = Intent(this@CodeCheckActivity, PacientCardActivity::class.java)
                        startActivity(intent)

                    } else {
                        Toast.makeText(this@CodeCheckActivity, "Неверный код", Toast.LENGTH_SHORT).show()
                        codecheck = ""
                        binding.EditText1.setText("")
                        binding.EditText2.setText("")
                        binding.EditText3.setText("")
                        binding.EditText4.setText("")
                        binding.EditText1.requestFocus()
                    }
                }
            })

    }
    }

