package com.example.hamburger;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.view.View;
import android.widget.Toast;
import android.widget.Button;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import com.example.hamburger.dao.UserDao;

public class MainActivity extends AppCompatActivity {

    //这个类主要用来进行，账号密码的验证，以及监听事件的编写。
    // 所以首先需要将一开始设计的输入文本框id name 和passwd获取。
    EditText name;//创建账号
    EditText passwd;//创建密码

    private static final String TAG="mysql-hamburger-MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        name=findViewById(R.id.name);//获取输入的账号
        passwd=findViewById(R.id.passwd);//获取输入的密码


        //这是能够登录的账号密码
        String Usename = "admin";
        String Upwd = "12345";

        //创建两个String类，储存从输入文本框获取到的内容
        String user = name.getText().toString().trim();
        String pwd = passwd.getText().toString().trim();

        //获取按钮
        Button button = (Button) findViewById(R.id.button);
        Button button_1 = (Button) findViewById(R.id.register);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick (View v){
                //监听按钮，如果点击，就跳转
                Intent intent = new Intent();
                //前一个（MainActivity.this）是目前页面，后面一个是要跳转的下一个页面
                intent.setClass(MainActivity.this, MainActivity2.class);
                startActivity(intent);
                //Toast.makeText(MainActivity.this, "欢迎你", Toast.LENGTH_SHORT).show();
            }
        });

        /*//进行判断，如果两个内容都相等，就显现第一条语句，反之，第二条。
        if(user.equals(Usename) & pwd.equals(Upwd)){
            //按钮进行监听

            }*/
        //else{
          //  Toast.makeText(MainActivity.this, "身份验证错误，禁止访问", Toast.LENGTH_SHORT).show();
        //}
        //按钮进行监听
        button_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //监听按钮，如果点击，就跳转
                Intent intent = new Intent();
                //前一个（MainActivity.this）是目前页面，后面一个是要跳转的下一个页面
                intent.setClass(MainActivity.this, register.class);
                startActivity(intent);
            }
        });
    }

    public void login(View view){

        EditText EditTextAccount = findViewById(R.id.name);
        EditText EditTextPassword = findViewById(R.id.passwd);

        new Thread(){
            @Override
            public void run() {
                UserDao userDao = new UserDao();
                int msg = userDao.login(EditTextAccount.getText().toString(),EditTextPassword.getText().toString());
                hand1.sendEmptyMessage(msg);
            }
        }.start();

    }

    public void reg(View view){
        startActivity(new Intent(getApplicationContext(),register.class));
    }

    @SuppressLint("HandlerLeak")
    final Handler hand1 = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0){
                Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_LONG).show();
            } else if (msg.what == 1) {
                Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_LONG).show();
            } else if (msg.what == 2){
                Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_LONG).show();
            } else if (msg.what == 3){
                Toast.makeText(getApplicationContext(), "账号不存在", Toast.LENGTH_LONG).show();
            }
        }
    };

}