package com.example.cmknowledgegraph;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.PersonalCenter.Login;

import Schema.OperaSchema;

public class LoginActivity extends AppCompatActivity {
    public static String username;
    public static int usersuccess = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        final EditText tel_edit=findViewById(R.id.id_edit);
        final EditText password_edit=findViewById(R.id.password_edit);

        /**
         * 登录按钮，传递参数：手机号(int)、密码(string)
         */
        Button login_btn=findViewById(R.id.login_btn);
        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(tel_edit==null||tel_edit.getText().toString().equals("")){
                    AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                    builder.setTitle("登录失败");// 设置标题
                    builder.setMessage("请输入电话号码");// 为对话框设置内容
                    builder.create().show();// 使用show()方法显示对话框
                }else if(password_edit==null||password_edit.getText().toString().equals("")){
                    AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                    builder.setTitle("登录失败");// 设置标题
                    builder.setMessage("请输入密码");// 为对话框设置内容
                    builder.create().show();// 使用show()方法显示对话框
                }else {
                    //获取手机号和密码
                    String tel = tel_edit.getText().toString().trim();
                    String password = password_edit.getText().toString();
                    //调用数据库类中的方法进行登录
//                boolean isLogin= OperaSchema.longin(tel,password);//原来的数据库
                    Login login = new Login(tel, password);

                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            LoginActivity.username = login.connUser();
                            LoginActivity.usersuccess = 1;
                            Log.i("登录", "登录有没有问题" + username + "oooo");

                        }
                    }).start();
/**
 * 登陆成功，返回上一个界面，即个人中心界面，刷新个人中心界面
 * 即，改变登录按钮为欢迎按钮
 */
                    while (LoginActivity.usersuccess == 0) {
                        Log.i("suersuer======", "dkie" + LoginActivity.usersuccess);
                    }

                    if (!LoginActivity.username.equals("")) {
                        finish();//返回个人中心Activity
                        //刷新个人中心Activity
                        Log.i("成功", "成功了");

                    } else {//登陆失败提醒
                        AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                        builder.setTitle("登陆提醒");// 设置标题
                        builder.setMessage("登陆失败");// 为对话框设置内容
                        builder.create().show();// 使用show()方法显示对话框
                        Log.i("失败", "失败了");
                    }
                    LoginActivity.username = "";
                    LoginActivity.usersuccess = 0;
                }

            }
        });

        //TextView注册跳转
        TextView register_text=findViewById(R.id.register_text);
        register_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到注册页面
                Intent intent=new Intent();
                intent.setClass(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
    }


}
