package com.example.smartlab

import android.content.ContentValues
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.RequiresApi
import com.example.smartlab.Model.ApiData
import com.example.smartlab.Model.APIUse
import com.example.smartlab.databinding.ActivityRegestrationBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.awaitResponse
import retrofit2.converter.gson.GsonConverterFactory

class RegestrationActivity : AppCompatActivity() {
    lateinit var binding: ActivityRegestrationBinding
    private lateinit var email: TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityRegestrationBinding.inflate(layoutInflater)
        setContentView(binding.root)

        email = binding.editRegEmail
        init()
    }

    private fun init() {
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
        binding.editRegEmail.requestFocus()
        binding.editRegEmail.addTextChangedListener(object : TextWatcher {
            @RequiresApi(Build.VERSION_CODES.M)
            override fun afterTextChanged(s: Editable) {
                valid()
            }

            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            }
        })

    }

    private fun valid() {
        if (email.text.isNotEmpty() && isValidString(email.text.toString())) {
            binding.btnNext.isEnabled = true
        } else {
            binding.btnNext.isEnabled = false
        }
    }

    fun isValidString(str: String): Boolean {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(str).matches()
    }

    fun onClickCode(view: View) {

        val intent: Intent = Intent(this, EmailCodeActivity::class.java)
        //записывает значение и передает в следующую активность
        ApiData.email = binding.editRegEmail.text.toString()

        APIUse.apiService.SendCode(binding.editRegEmail.text.toString())
            .enqueue(object : Callback<String> {
                override fun onResponse(call: Call<String>, response: Response<String>) {
                    if (response.code() == 200) {
                        if (response.body() != null) {
                            startActivity(intent)
                        }
                    }
                }

                override fun onFailure(call: Call<String>, t: Throwable) {
                    Toast.makeText(
                        this@RegestrationActivity,
                        "Что-то пошло не так",
                        Toast.LENGTH_SHORT
                    ).show()
                }
            })

    }
}