package net.micode.notes.ui;

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

import net.micode.notes.R;

public class RegisterLoginPassword extends Activity{
    EditText password01;
    EditText password02;
    Button registered;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_loginpassword);
        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
                        | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        password01=(EditText) findViewById(R.id.rg_password01);
        password02=(EditText) findViewById(R.id.rg_password02);
        registered=(Button)findViewById(R.id.rg_registered);
        registered.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text02 = password01.getText().toString();
                String text03 = password02.getText().toString();
                if(text02.equals("")==true) {
                    Toast.makeText(RegisterLoginPassword.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                }else if (text02.equals(text03) == false) {
                    Toast.makeText(RegisterLoginPassword.this, "密码不匹配，请重新输入密码", Toast.LENGTH_SHORT).show();
                    password02.setText("");
                }else if (text02.equals(text03) == true){
                    SharedPreferences.Editor editor=getSharedPreferences("user management",
                            MODE_PRIVATE).edit();
                    editor.putBoolean("user",true);//true表示已经设置登录密码
                    editor.putString("password",text02);
                    editor.apply();
                    Log.d("RegisterLoginPassword","password is "+text02);
                    Toast.makeText(RegisterLoginPassword.this, "设置密码成功", Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(RegisterLoginPassword.this,NotesListActivity.class);
                    startActivity(intent);
                    finish();
                }

            }
        });
    }

    @Override
    public void onBackPressed() {
        Intent intent=new Intent(RegisterLoginPassword.this,NotesListActivity.class);
        startActivity(intent);
        finish();
    }

}
