package com.example.cici.voice;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;


public class ResPwdActivity extends AppCompatActivity {
    private EditText editUsername;
    private EditText editPwd;
    private EditText editResPwd;
    private String name,password;
    private Button btnConfirm;
    boolean ishave=false;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.res_password);
        editUsername =  findViewById(R.id.editName);
        editPwd =  findViewById(R.id.editPwd);
        editResPwd =  findViewById(R.id.editResPwd);
        btnConfirm = findViewById(R.id.btnConfirm);
    }

    /**
     * confirm event
     * @param v
     */
    public void OnMyConfirmClick(View v) {
        confirmInfo();
    }

    /**
     * confirm event
     */
    private void confirmInfo() {
        name=editUsername.getText().toString();
        if(TextUtils.isEmpty(editUsername.getText().toString().trim())|| TextUtils.isEmpty(editPwd.getText().toString()) || TextUtils.isEmpty(editResPwd.getText().toString())){
            Toast.makeText(this, "账号或密码不能为空！", Toast.LENGTH_SHORT).show();
            return;
        }
        if(!editPwd.getText().toString().equals(editResPwd.getText().toString())) {
            Toast.makeText(this, "输入密码不正确！", Toast.LENGTH_SHORT).show();
            return;
        }
        isHaveUser(name);
        if(ishave){
            Toast.makeText(this, "密码重置成功！", Toast.LENGTH_SHORT).show();
            this.finish();
        }

        else{
            new AlertDialog.Builder(this)
                    .setTitle("提示")
                    .setMessage("该用户不存在，请到注册界面进行注册！")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            setResult(RESULT_OK);
                            Intent intent=new Intent(ResPwdActivity.this,RegisterActivity.class);
                            ResPwdActivity.this.startActivity(intent);
                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            return;
                        }
                    })
                    .show();
        }
    }
    private boolean isHaveUser(String user){

        BmobQuery<BmobUser> query = new BmobQuery<BmobUser>();
        query.addWhereEqualTo("username", user);
        query.findObjects(new FindListener<BmobUser>() {
            @Override
            public void done(List<BmobUser> object, BmobException e) {
                if(e==null){

                    ishave=true;

                }
            }
        });
        return ishave;
    }
    public void toast(String msg) {
        Toast.makeText(ResPwdActivity.this, msg, Toast.LENGTH_SHORT).show();
    }
}
