package com.example.mysmartlab

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.mysmartlab.Model.ApiHttp
import com.example.mysmartlab.databinding.ActivityMailBinding
import com.example.mysmartlab.databinding.ActivityRegBinding
import retrofit2.Call
import retrofit2.Response
import java.util.*

class ActivityMail : AppCompatActivity() {
    lateinit var binding: ActivityMailBinding
    var codech = ""
    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 = ActivityMailBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.editNum1.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.editNum1.text.toString().length == 1){
                    codech += s
                    binding.editNum2.requestFocus()
                }
            }
        })
        binding.editNum2.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.editNum2.text.toString().length == 1){
                    codech += s
                    binding.editNum3.requestFocus()
                }
            }
        })
        binding.editNum3.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.editNum3.text.toString().length == 1){
                    codech += s
                    binding.editNum4.requestFocus()
                }
            }
        })
        binding.editNum4.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.editNum4.text.toString().length == 1){
                    codech += s
                }
                if(codech.length ==4){
                    SingIn()
                }
            }
        })
        Timer()
    }

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

            override fun onFinish() {
                binding.sekondCounter.setText("Код отправлен повторно");
                SingIn()
                Timer()
            }
        }
        timer.start()
    }
    private fun SingIn() {
        ApiHttp.retrifitApi.SignIn(ApiHttp.User_mail, codech)
            .enqueue(object : retrofit2.Callback<String> {
                override fun onFailure(call: Call<String>, t: Throwable) {
                    Toast.makeText(this@ActivityMail, 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@ActivityMail, ActivityAnalise::class.java)
                        startActivity(intent)

                    } else {
                        Toast.makeText(this@ActivityMail, "Неверный код", Toast.LENGTH_SHORT).show()
                        codech = ""
                        binding.editNum1.setText("")
                        binding.editNum2.setText("")
                        binding.editNum3.setText("")
                        binding.editNum4.setText("")
                        binding.editNum1.requestFocus()
                    }
                }
            })
    }
}