package com.example.logistics.ui;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.logistics.R;
import com.example.logistics.manager.userManager;
import com.example.logistics.entity.User;

public class SignUpActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);

        Button signUpButton = (Button) this.findViewById(R.id.SignUpButton);
        Button backLoginButton = (Button) this.findViewById(R.id.BackLoginButton);

        signUpButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        register(v);
                    }
                }
        );
        // 返回登录按钮监听器
        backLoginButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 跳转到登录界面
                        Intent intent = new Intent(SignUpActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }
                }
        );

    }

    public void register(View view){
        EditText userName = (EditText) this.findViewById(R.id.UserNameEdit);
        EditText passWord = (EditText) this.findViewById(R.id.PassWordEdit);
        EditText passWordAgain = (EditText) this.findViewById(R.id.PassWordAgainEdit);
        EditText phone = (EditText) this.findViewById(R.id.PhoneEdit);

        String strUserName = userName.getText().toString().trim();
        String strPassWord = passWord.getText().toString().trim();
        String strPassWordAgain = passWordAgain.getText().toString().trim();
        String strPhoneNumber = phone.getText().toString().trim();

        if (strUserName.length() > 10) {
            Toast.makeText(SignUpActivity.this, "用户名长度必须小于10！", Toast.LENGTH_SHORT).show();
        } else if (strUserName.length() == 0) {
            Toast.makeText(SignUpActivity.this, "用户名不能为空！", Toast.LENGTH_SHORT).show();
        } else if (strPassWord.length() > 16) {
            Toast.makeText(SignUpActivity.this, "密码长度必须小于16！", Toast.LENGTH_SHORT).show();
        } else if (strPassWord.length() == 0) {
            Toast.makeText(SignUpActivity.this, "密码不能为空！", Toast.LENGTH_SHORT).show();
        } else if (!strPassWord.equals(strPassWordAgain)) {
            Toast.makeText(SignUpActivity.this, "两次密码输入不一致！", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(SignUpActivity.this, "注册成功！", Toast.LENGTH_SHORT).show();

            User user = new User();
            user.setUserName(strUserName);
            user.setPassWord(strPassWord);
            user.setPhoneNum(strPhoneNumber);

            new Thread(){
                @Override
                public void run(){
                    int msg = 0;
                    userManager userManager = new userManager();

                    User uu = userManager.findUser(user.getUserName());

                    if(uu != null){
                        msg = 1;        //have this account
                    }else{
                        boolean flag = userManager.register(user);
                        if(flag){
                            msg = 2;
                        }
                    }
                    hand.sendEmptyMessage(msg);
                }
            }.start();
        }
    }

    @SuppressLint("HandlerLeak")
    final Handler hand = new Handler(){
        public void handleMessage(Message msg){
            if(msg.what == 0){
                Toast.makeText(getApplicationContext(), "注册失败", Toast.LENGTH_SHORT).show();
            }else if(msg.what == 1){
                Toast.makeText(getApplicationContext(), "该账号已存在，请换一个注册", Toast.LENGTH_SHORT).show();
            }else if(msg.what == 2){
                Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(SignUpActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        }
    };

}
