package net.micode.notes.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import net.micode.notes.R;


public class loginActivity extends Activity {

    private Button MyButton ;//定义变量两个按钮，用户输入的用户名和密码

    private Button MyButton2 ;

    private EditText MyUsernName ;

    private EditText MyPassWord ;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);//显示页面

        MyButton = (Button) findViewById(R.id.D);//根据id绑定按钮和编辑文本
        MyButton2 = (Button) findViewById(R.id.Z);
        MyUsernName = (EditText) findViewById(R.id.user);
        MyPassWord = (EditText) findViewById(R.id.password);
        setListener();//设置监听方法
    }
    private void setListener(){
        onClick onClick = new onClick() ;//监听方法中有两个点击事件
        MyButton.setOnClickListener(onClick);
        MyButton2.setOnClickListener(onClick);
    }

    public class onClick implements View.OnClickListener{//点击事件重载
        @Override
        public void onClick(View view) {
            switch (view.getId()){//设置开关，根据不同的id来对应不同的点击事件
                case R.id.D:
                    String username = MyUsernName.getText().toString() ;//存储用户名和密码
                    String password = MyPassWord.getText().toString() ;
                    String ok = "登录成功" ;
                    String fail = "账号或密码有误，请重新登录" ;

                    SharedPreferences preferences = getSharedPreferences("user_info", MODE_PRIVATE);
                    String savedUsername = preferences.getString("username", "");
                    String savedPassword = preferences.getString("password", "");//这里获取已经定义了的用户名和密码

                    if (savedUsername.equals(username) && savedPassword.equals(password)){
                        //如果用户名和密码与已经注册了的用户名和密码相同就可以执行登录操作
                        Toast toastCenter = Toast.makeText(getApplicationContext(), ok , Toast.LENGTH_SHORT);//界面美化，提示登录成功
                        toastCenter.setGravity(Gravity.CENTER , 0 , 0);//设置提醒语句文位置
                        toastCenter.show();
                        Intent intent = new Intent(loginActivity.this , NotesListActivity.class) ;//进行页面跳转
                        startActivity(intent);
                    }else{
                        Toast toastCenter =  Toast.makeText(getApplicationContext(), fail , Toast.LENGTH_SHORT);//提示登录失败
                        toastCenter.setGravity(Gravity.CENTER , 0 , 0);//设置提醒语句文位置
                        toastCenter.show();
                    }
                    break;
                case R.id.Z:
                    Intent intent = new Intent(loginActivity.this,signinActivity.class) ;//对应注册页面
                    startActivity(intent);
                    break;
            }
        }
    }
}