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 LoginActivity extends Activity {
    public static String userAccount;
    public static String userPwd;
    public static String getUserAccount(){ return userAccount; }
    public static String getUserPwd() { return userPwd; }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        findViewById(R.id.signUp).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, Addaccount.class);
                startActivity(intent);
                finish();
            }
        });
        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                EditText editText1 =(EditText) findViewById (R.id.accountName);
                final String outputaccount = editText1.getText().toString();
                EditText editText2 =(EditText) findViewById (R.id.accountPassword);
                final String outputpwd = editText2.getText().toString();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try{
                            if (ConnectRDS.login(outputaccount,outputpwd)){
                                userAccount = outputaccount;
                                userPwd = outputpwd;
                                Intent intent = new Intent(LoginActivity.this, syncActivity.class);
                                startActivity(intent);
                                finish();
                            } 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();//暂时提示
            }
        });
    }
}

