package net.micode.notes.ui;

import android.app.Activity;
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 net.micode.notes.R;

public class ChangingPassword extends Activity {
    EditText OldPassword;
    EditText NewPassword;
    EditText AckPassword;
    Button Acknowledged;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
                        | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        OldPassword=(EditText) findViewById(R.id.old_password);
        NewPassword=(EditText) findViewById(R.id.new_password);
        AckPassword=(EditText) findViewById(R.id.ack_password);
        Acknowledged=(Button)findViewById(R.id.Acknowledged);
        Acknowledged.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String old_password = OldPassword.getText().toString();
                String new_password = NewPassword.getText().toString();
                String ack_password = AckPassword.getText().toString();
                SharedPreferences pref=getSharedPreferences("user management",MODE_PRIVATE);
                String login_password=pref.getString("password","");
                if(old_password.equals("")==true || new_password.equals("")==true || ack_password.equals("")==true) {
                    Toast.makeText(ChangingPassword.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                }else if (new_password.equals(ack_password) == false) {
                    Toast.makeText(ChangingPassword.this, "新建密码与重复密码不匹配，请重新输入密码", Toast.LENGTH_SHORT).show();
                    AckPassword.setText("");
                }else if(old_password.equals(login_password) == false){
                    Toast.makeText(ChangingPassword.this, "原有密码错误，请重新输入密码", Toast.LENGTH_SHORT).show();
                    OldPassword.setText("");
                }
                else if (new_password.equals(ack_password) == true && old_password.equals(login_password) == true){
                    SharedPreferences.Editor editor=getSharedPreferences("user management", MODE_PRIVATE).edit();
                    editor.putString("password",new_password);
                    editor.apply();
                    Toast.makeText(ChangingPassword.this, "修改密码成功", Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(ChangingPassword.this,NotesListActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        });
    }

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

