package net.micode.notes.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import net.micode.notes.R;
import net.micode.notes.mysql.ConnectRDS;


public class Addaccount extends Activity {

    final int ADDACOUNTNUMBER = 2;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addaccount);
        findViewById(R.id.alreadyMember).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Addaccount.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
        findViewById(R.id.create).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText editText1 =(EditText) findViewById (R.id.userName);
                final String outputaccount = editText1.getText().toString();
                EditText editText2 =(EditText) findViewById (R.id.Password);
                final String outputpwd = editText2.getText().toString();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try{
                            String result = ConnectRDS.addAccount(outputaccount,outputpwd);
                            if (result.equals("success")){
                                showMessage("注册成功！");
                                Intent intent = new Intent(Addaccount.this, LoginActivity.class);
                                startActivity(intent);
                                finish();
                            } else if (result.equals("existed")){
                                showMessage("用户已存在，请重新输入！");
                            } else {
                                showMessage("注册失败，请检查网络连接！");
                            }
                        } catch(Exception e){
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }
    private void showMessage(final String msg) {
        if (TextUtils.isEmpty(msg))
            return;

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();//暂时提示
            }
        });
    }

}