package hunnu.sj.raise_money;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;

import hunnu.sj.raise_money.DataBase.UserService;

//用户注册
public class RegiteActivity extends AppCompatActivity {
    EditText user_name;
    EditText pass_word;
    EditText repass_word;
    RadioButton radio1;
    RadioButton radio2;
    String role;
    Handler rHandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);//用户在主线程中处理子线程发送回来的消息，更改控件内容
            String txt = (String) msg.obj;
            switch (msg.what){
                case 0:
                    user_name.setError(txt);
                    break;
                case 1:
                    pass_word.setError(txt);
                    break;
                case 2:
                    repass_word.setText("");
                    repass_word.setError(txt);
                    break;
                case 3:
                    user_name.setError(txt);
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registe);
    }

    public void onRegister(View view){
        user_name = findViewById(R.id.user_name);
        pass_word = findViewById(R.id.pass_word);
        repass_word = findViewById(R.id.repass_word);
        role = "默认角色";
        radio1 = findViewById(R.id.ratio1);
        radio2 = findViewById(R.id.radio2);
        if(radio1.isChecked()){
            role = "爱心人士";
        }else if(radio2.isChecked()){
            role = "贫困学生";
        }
        //获取注册界面的控件
        new RegiteThread().start();
    }

    class RegiteThread extends Thread{
        @Override
        public void run() {
            String username = user_name.getText().toString();
            String password = pass_word.getText().toString();
            String repassword = repass_word.getText().toString();//从控件获取字符
            UserService uService = new UserService();
            Message message = rHandler.obtainMessage();//使用handler来处理信息
            if(username.trim().equals("")){//一系列条件来判断是否可注册
                message.what = 0;
                message.obj = "请输入用户名";
                rHandler.sendMessage(message);
            }else if(password.trim().equals("")){
                message.what = 1;
                message.obj = "请输入密码";
                rHandler.sendMessage(message);
            }else if(!password.equals(repassword)){
                message.what = 2;
                message.obj = "密码验证错误";
                rHandler.sendMessage(message);
            }else if(uService.login(username,password)!=0){
                message.what = 3;
                message.obj = "用户已存在";
                rHandler.sendMessage(message);//发送信息给handler
            }else{
                User user = new User();
                user.setName(username);
                user.setPasd(password);
                user.setRole(role);
                uService.register(user);//注册
                Intent intent = new Intent(RegiteActivity.this,SignInActivity.class);
                startActivity(intent);
                finish();
            }
        }
    }
}
