package com.example.cmknowledgegraph;

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

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

import com.example.PersonalCenter.Register;


public class RegisterActivity extends AppCompatActivity {
    public static int regester = 0;
    public static boolean isRegister;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        final EditText tel_edit=findViewById(R.id.tel_register_edit);
        final EditText password_edit=findViewById(R.id.password_register_edit);
        final EditText nickname_edit=findViewById(R.id.nickname_register_edit);

        /**
         * 注册按钮，传递参数：手机号（int），密码(string)，昵称(string)
         */
        Button register =findViewById(R.id.register_btn);
        register.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(RegisterActivity.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(RegisterActivity.this);
                    builder.setTitle("注册失败");// 设置标题
                    builder.setMessage("请输入密码");// 为对话框设置内容
                    builder.create().show();// 使用show()方法显示对话框
                }else if(nickname_edit==null||nickname_edit.getText().toString().equals("")){
                    AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                    builder.setTitle("注册失败");// 设置标题
                    builder.setMessage("请输入用户名");// 为对话框设置内容
                    builder.create().show();// 使用show()方法显示对话框
                }else {
                    //获取手机号，密码，昵称
                    String tel = tel_edit.getText().toString().trim();
                    String password = password_edit.getText().toString();
                    String NickName = nickname_edit.getText().toString();
                    //调用数据库类方法进行注册
//                boolean isRegister= OperaSchema.register(tel,NickName,password);//原来的数据连接
                    final Register register = new Register(tel, NickName, password);

                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            RegisterActivity.isRegister = register.regist();
                            Log.i("hahaaha", "注册成功了吗" + isRegister);
                            RegisterActivity.regester = 1;
                        }
                    }).start();
                    while (RegisterActivity.regester == 0) {
                        Log.i("rererer======", "dsd" + RegisterActivity.regester);
                    }
                    if (RegisterActivity.isRegister) {
                        //注册成功，对话框提醒
                        AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                        builder.setTitle("注册提醒");// 设置标题
                        // builder.setIcon(R.drawable.ic_launcher);//设置图标
                        builder.setMessage("注册成功");// 为对话框设置内容
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {
                                // TODO Auto-generated method stub
                                //返回登录页面
                                finish();
                            }
                        });
                        builder.create().show();// 使用show()方法显示对话框
                    } else {//注册失败
                        //注册失败提醒
                        Log.i("zh==-----==--==", "kidoosloe");
                        AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                        builder.setTitle("注册提醒");// 设置标题
                        // builder.setIcon(R.drawable.ic_launcher);//设置图标
                        builder.setMessage("注册失败");// 为对话框设置内容
                        builder.create().show();
                    }
                    RegisterActivity.regester = 0;
                    RegisterActivity.isRegister = false;
                }

            }
        });
    }
}
