package net.micode.notes.ui;

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

import net.micode.notes.R;

public class LoginActivity extends Activity{

    EditText lgn_password;
    Button lgn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences pref = getSharedPreferences("user management", MODE_PRIVATE);
        boolean User_boolean = pref.getBoolean("user", false); //检查用户是否设置了密码
        if (!User_boolean) {  //如果用户没有设置密码,则直接跳转到便签主界面
            Intent intent = new Intent(LoginActivity.this, NotesListActivity.class);
            startActivity(intent);
            finish();
        }

        setContentView(R.layout.activity_login);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        lgn_password = (EditText)findViewById(R.id.lgn_password);
        lgn_login = (Button) findViewById(R.id.login);
        lgn_login.setOnClickListener(new View.OnClickListener() {
                                         @Override
                                         public void onClick(View v) {
                                             SharedPreferences pref = getSharedPreferences("user management", MODE_PRIVATE);
                                             String password = pref.getString("password", "");
                                             if (password.equals(" ") == false && password.equals(lgn_password.getText().toString()) == true) {
                                                 Intent intent = new Intent(LoginActivity.this, NotesListActivity.class);
                                                 startActivity(intent);
                                                 finish();
                                             } else {
                                                 Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
                                                 lgn_password.setText(""); //清空密码框内的输入
                                             }

                                         }
                                     }
        );
    }
}


