package com.example.smartlab

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.GestureDetector
import android.view.MotionEvent
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.smartlab.databinding.WelcomeWindowBinding
import android.content.ContentValues
import android.content.SharedPreferences
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.widget.Toast
import com.example.smartlab.Api.ApiHttp
import com.example.smartlab.Api.ApiRequest
import com.google.gson.GsonBuilder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.awaitResponse
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory

class WeclomeActivity : AppCompatActivity() {
    @SuppressLint("MissingInflatedId")
    var binding: WelcomeWindowBinding? = null
    lateinit var editor : SharedPreferences.Editor
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = WelcomeWindowBinding.inflate(layoutInflater)
        setContentView(binding!!.root)
        binding!!.next.isEnabled = false
        editor = this.getSharedPreferences("hasVisitedMail", MODE_PRIVATE).edit()
        listener_correct()
    }
    private fun listener_correct() {
        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) {
                binding!!.next.isEnabled =
                    Regex("[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" + "\\@" + "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" + "\\." + "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}").containsMatchIn(
                        binding!!.EditText1.text.toString()
                    )
            }
        })
    }

    fun SendEmail(view: View) {
        ApiHttp.retrifitApi.SendCode(binding!!.EditText1.text.toString())
            .enqueue(object : retrofit2.Callback<String> {
                override fun onFailure(call: Call<String>, t: Throwable) {
                    Toast.makeText(this@WeclomeActivity, t.message, Toast.LENGTH_LONG).show()
                }
                override fun onResponse(call: Call<String>, response: Response<String>) {
                    if (response.code() == 200) {
                        ApiHttp.User_mail = binding!!.EditText1.text.toString()
                        editor.putBoolean("hasVisitedMail",true).apply()
                        var intent = Intent(this@WeclomeActivity, CodeCheckActivity::class.java)
                        startActivity(intent)
                    }
                }
            })
    }
    }