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 DeletingPassword extends Activity {
    EditText Dt_password;
    Button Acknowledged;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_password);
        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
                        | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        Dt_password=(EditText) findViewById(R.id.thepassword);
        Acknowledged=(Button)findViewById(R.id.Dt_Acknowledged);
        Acknowledged.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text02 = Dt_password.getText().toString();
                if(text02.equals("")==true)
                    Toast.makeText(DeletingPassword.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                SharedPreferences pref=getSharedPreferences("user management",MODE_PRIVATE);
                String password = pref.getString("password","");
                if(password.equals("")==false&&password.equals(text02)==true){
                    SharedPreferences.Editor editor=getSharedPreferences("user management",
                            MODE_PRIVATE).edit();
                    editor.putBoolean("user",false);//false表示已经设置登录密码
                    editor.putString("password","");
                    editor.apply();

                    Toast.makeText(DeletingPassword.this, "已经删除登录密码", Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(DeletingPassword.this,NotesListActivity.class);
                    startActivity(intent);
                    finish();
                }
                else{
                    Toast.makeText(DeletingPassword.this, "密码错误", Toast.LENGTH_SHORT).show();
                    Dt_password.setText("");//把密码框内输入过的错误密码清空
                }
            }
        });
    }

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

