package com.tofly.ghrq_route.common.ui.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.DialogTitle;

import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.module.user.domain.UserBean;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Amu on 2017/2/13.
 */

public class UserPasswordModifyDailog extends AlertDialog {
    @BindView(android.R.id.icon)
    ImageView icon;
    @BindView(R.id.alertTitle)
    DialogTitle alertTitle;
    @BindView(android.R.id.message)
    TextView alertMessage;
    @BindView(R.id.edit_old_password)
    EditText editOldPassword;
    @BindView(R.id.edit_new_password)
    EditText editNewPassword;
    @BindView(R.id.edit_repeat_password)
    EditText editRepeatPassword;
    @BindView(android.R.id.button2)
    Button button2;
    @BindView(android.R.id.button1)
    Button button1;
    private Context mContext;

    private ClickListenerInterface mClickListenerInterface;

    public UserPasswordModifyDailog(Context context) {
        super(context, R.style.AlertDialog);
        mContext = context;
    }

    public interface ClickListenerInterface {
        public void doOk(String oldPassword, String newPassword);

        public void doCancel();
    }

    public void setClicklistener(ClickListenerInterface clickListenerInterface) {
        this.mClickListenerInterface = clickListenerInterface;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        setContentView(R.layout.dialog_password_modify);
        ButterKnife.bind(this);

        initView();
    }

    private void initView() {
        alertTitle.setText("修改用户密码");
        alertMessage.setText("修改用户密码成功后下次进入应用需要重新登陆");
        button1.setText(android.R.string.ok);
        button2.setText(android.R.string.cancel);
    }

    @OnClick({android.R.id.button1, android.R.id.button2})
    public void onClick(View view) {
        switch (view.getId()) {
            /*Button OK*/
            case android.R.id.button1:
                String oldPassword = editOldPassword.getText().toString();
                String newPassword = editNewPassword.getText().toString();
                String repeatPassword = editRepeatPassword.getText().toString();
                if (oldPassword.isEmpty() || newPassword.isEmpty() || repeatPassword.isEmpty()) {
                    ToastUtil.showToast(mContext, R.string.modify_user_input_empty);
                    return;
                }
                if (!newPassword.equals(repeatPassword)) {
                    ToastUtil.showToast(mContext, "两次输入的密码不一致");
                    return;
                }
                if (oldPassword.equals(newPassword)) {
                    ToastUtil.showToast(mContext, "新的密码不能和旧密码相同");
                    return;
                }

                UserBean userBean = BaseApp.getInstance().getLoginUser();
                if (userBean != null){
                    String savePassword = userBean.getPassword();
                    String encPassword = BaseApp.SHA1EncodeString(oldPassword);
                    if (!savePassword.equals(encPassword)) {
                        ToastUtil.showToast(mContext, "请输入正确的旧密码!");
                        return;
                    }
                }

                if (mClickListenerInterface != null) {
                    mClickListenerInterface.doOk(oldPassword, newPassword);
                }
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
                this.dismiss();
                break;
            case android.R.id.button2:
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
                this.dismiss();
                break;
        }
    }
}
