package com.example.smartlab

import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.core.widget.doAfterTextChanged
import com.example.smartlab.databinding.ActivityCodeEmailBinding

class RegisterUser : AppCompatActivity() {
          override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
                 setContentView(R.layout.activity_register_user)
        checkEmail()
    }

    private fun regexMatch(str:String): Boolean
    {
        val regex = "^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$".toRegex();
        return regex.matches(str)
    }

    private fun checkEmail()
    {
        var editTest:EditText=findViewById(R.id.TextSizeCheck)
        var btn:Button = findViewById(R.id.button);
        var messege = findViewById<TextView>(R.id.vxodPoEmail)


        editTest.doAfterTextChanged {
            if(regexMatch(editTest.text.toString()))
            {
                messege.text = "Вход по Email"
                messege.setTextColor(getColor(R.color.grey2))
                editTest.setBackgroundResource(R.drawable.unactive_button)
                btn.setBackgroundResource(R.drawable.active_button)
                btn.setOnClickListener()
                {
                    val intent = Intent(this,CodeEmail::class.java )
                    startActivity(intent)
                }
            }
            else
            {
                messege.text = "Неверный формат почты"
                messege.setTextColor(getColor(R.color.grey1))
                editTest.setBackgroundResource(R.drawable.unactive_button)
                btn.setBackgroundResource(R.drawable.unactive_button)
            }

        }
    }





}