package com.example.watch.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.watch.R
import java.util.regex.Pattern

class LoginActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.login_layout)
//        隐藏验证码部分
        val edit_security_part = findViewById<LinearLayout>(R.id.edit_security_part)
        edit_security_part.setVisibility(View.GONE); // 隐藏
//        获取验证码
        getVoild()
    }

    //    获取验证码
    fun getVoild() {
        val ButtonGoOn = findViewById<Button>(R.id.go_on)
        ButtonGoOn.setOnClickListener { v ->
            val text_number = findViewById<EditText>(R.id.account)
        val number: String = text_number.getText().toString()
        if (number.length == 0) {
             Toast.makeText(this, "手机号码不能为空", Toast.LENGTH_LONG).show()
            return@setOnClickListener
        }
        if(!checkPhone(number)){
            Toast.makeText(this, "手机号码格式不正确", Toast.LENGTH_LONG).show()
            return@setOnClickListener
        }
            hideLayout()
            volidLogin()
        }
    }

    //  隐藏手机输入部分
    fun hideLayout() {
        val account_layout = findViewById<LinearLayout>(R.id.account_layout)
        account_layout.setVisibility(View.GONE); // 隐藏
        val go_on_layout = findViewById<LinearLayout>(R.id.go_on_layout)
        go_on_layout.setVisibility(View.GONE); // 隐藏
        val edit_security_part = findViewById<LinearLayout>(R.id.edit_security_part)
        edit_security_part.setVisibility(View.VISIBLE); // 显示
//        volid 获取焦点
        val item01 = findViewById<View>(R.id.item_code_iv1)
        item01.requestFocus()

    }

    fun volidLogin() {
        val ButtonLogin = findViewById<Button>(R.id.login_btn)
        ButtonLogin.setOnClickListener { v ->
            startActivity(Intent(this, MainActivity::class.java))
        }

    }

}

//检查手机号格式
fun checkPhone(number: String): Boolean {
    val regExp = "^((13[0-9])|(15[^4])|(18[0-9])|(17[0-8])|(14[5-9])|(166)|(19[8,9])|)\\d{8}$"
    val p = Pattern.compile(regExp)
    val m = p.matcher(number)
    return m.matches()
}


