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.gtask.remote.Network;

public class LoginActivity extends Activity {
    private  final int LOGINNUMBER = 1;

    private final String url = "jdbc:postgresql://localhost:5432/minote?useUnicode=true&characterEncoding=utf8&currentSchema=public&stringtype=unspecified";
    private String user = "postgres";
    private String password = "hacker";

    private static final String HOST = "192.168.43.231";
    private static final int PORT = 4000;
    private static String username;
    public static String getUsername(){return  username;}
    @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);
                String outputuser = editText1.getText().toString();
                EditText editText2 =(EditText) findViewById (R.id.accountPassword);
                String outputkey = editText2.getText().toString();
                Network login = new Network(LOGINNUMBER,outputuser,outputkey);
                try {
                    Thread thread1 = new Thread(login,"login");
                    thread1.start();
                    thread1.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                String returnvalue = login.getInTemp();

                if(returnvalue.equals("correct")){
                    showMessage("logining");
                    username = login.returnname();
                    Intent intent = new Intent(LoginActivity.this, syncActivity.class);
                    startActivity(intent);
                    finish();

                }
                else if(returnvalue.equals("timeout")){
                    showMessage("timeout");
                }
                else{
                    showMessage("not match");
                }
            }
        });



    }
    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();//暂时提示
            }
        });
    }


    

}
