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.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


import java.util.ArrayList;

import hunnu.sj.raise_money.DataBase.UserService;

public class SignInActivity extends AppCompatActivity {
    public static final String IS_SIGNIN = "is.user.sign.in";
    public static final int SEND = 0;
    EditText usnm;
    EditText pswd;
    UserService uService;
    User user;
    Handler mHandler  = new Handler(){//用于处理子线程传回的消息，对主线的UI进行修改
        int flag = -1;
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            String txt;
            switch (msg.what){
                case 0:
                    flag = 0;
                    txt = (String) msg.obj;
                    usnm.setError(txt);
                    break;
                case 1:
                    flag = 1;
                    txt = (String) msg.obj;
                    pswd.setError(txt);
                    break;
            }

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signin);
        uService = new UserService();
    }

    public void user_commit(View view){
        usnm = findViewById(R.id.user_name);
        pswd = findViewById(R.id.pass_word);
        new LoginThread().start();

    }

    public void onToRegister(View view){
        Intent intent = new Intent(this,RegiteActivity.class);
        startActivity(intent);
    }

    class LoginThread extends Thread{
        @Override
        public void run() {
            int flag = -1;//先设置flag为-1
            String name = usnm.getText().toString();
            String pasd = pswd.getText().toString();
            //UserService uService = new UserService();
            flag = uService.login(name,pasd);//用login函数来判断用户输入的用户名与密码是否正确
            Message message = mHandler.obtainMessage();
            if(flag==0){
                message.what = 0;
                message.obj = "用户名不存在";
                mHandler.sendMessage(message);//使用handler给主线程发送消息
            }else if(flag==1){
                message.what = 1;
                message.obj = "密码错误";
                mHandler.sendMessage(message);
            }else{
                User user = new User();
                user.setName(name);
                user.setPasd(pasd);
                String role = uService.getRole(name);
                user.setRole(role);
                Bundle bundle = new Bundle();//将类包装到bundle里，再放到intent里
                bundle.putSerializable("user",user);
                Intent intent =new Intent(SignInActivity.this,MainActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
                finish();
            }
        }
    }
}
