package com.casic.dcms.ui;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.casic.dcms.base.AndroidxBaseActivity;
import com.casic.dcms.bean.ActionResultBean;
import com.casic.dcms.databinding.ActivityPasswordBinding;
import com.casic.dcms.mvp.presenter.impl.ChangePasswordPresenterImpl;
import com.casic.dcms.mvp.view.IChangePasswordView;
import com.casic.dcms.utils.AuthenticationHelper;
import com.casic.dcms.utils.Constant;
import com.casic.dcms.utils.RSAUtils;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.utils.ToastHelper;
import com.casic.dcms.widgets.AlertMessageDialog;

import java.nio.charset.StandardCharsets;
import java.security.PublicKey;

/**
 * @author : Pengxh
 * @time : 2021/4/14 13:20
 * @email : 290677893@qq.com
 */
public class ChangePasswordActivity extends AndroidxBaseActivity<ActivityPasswordBinding> implements IChangePasswordView {

    private ChangePasswordPresenterImpl changePasswordPresenter;

    @Override
    protected void setupTopBarLayout() {
        viewBinding.titleBarLayout.titleView.setText("修改密码");
        viewBinding.titleBarLayout.leftBackView.setOnClickListener(v -> finish());
    }

    @Override
    protected void initData() {
        EditText newPsdEditText = viewBinding.newPsdLayout.getEditText();
        assert newPsdEditText != null;
        newPsdEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.toString().trim().length() > 12) {
                    viewBinding.newPsdLayout.setError("密码长度超出限制");
                } else {
                    viewBinding.newPsdLayout.setError(null);
                }
            }
        });

        EditText confirmPsdEditText = viewBinding.confirmPsdLayout.getEditText();
        assert confirmPsdEditText != null;
        confirmPsdEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.toString().trim().length() > 12) {
                    viewBinding.confirmPsdLayout.setError("密码长度超出限制");
                } else {
                    viewBinding.confirmPsdLayout.setError(null);
                }
            }
        });

        changePasswordPresenter = new ChangePasswordPresenterImpl(this);
    }

    @Override
    protected void initEvent() {
        viewBinding.changePasswordButton.setChangeAlphaWhenPress(true);
        viewBinding.changePasswordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String oldPassword = viewBinding.originalEditView.getText().toString();
                if (TextUtils.isEmpty(oldPassword)) {
                    ToastHelper.showToast("请先输入原密码", ToastHelper.WARING);
                    return;
                }
                String newPassword = viewBinding.newEditView.getText().toString();
                if (TextUtils.isEmpty(newPassword)) {
                    ToastHelper.showToast("请先输入新密码", ToastHelper.WARING);
                    return;
                }
                String confirmPassword = viewBinding.confirmEditView.getText().toString();
                if (TextUtils.isEmpty(confirmPassword)) {
                    ToastHelper.showToast("请再次输入新密码", ToastHelper.WARING);
                    return;
                }
                if (!newPassword.equals(confirmPassword)) {
                    ToastHelper.showToast("新密码和确认密码输入不一致，请重新输入", ToastHelper.WARING);
                    return;
                }
                if (newPassword.length() < 6 || newPassword.length() > 12) {
                    ToastHelper.showToast("新密码长度不正确，请重新输入", ToastHelper.WARING);
                    return;
                }
                if (!StringHelper.isLetterAndDigit(newPassword)) {
                    ToastHelper.showToast("新密码请输入6-12位的数字字母组合", ToastHelper.WARING);
                    return;
                }
                //数据校验OK，可以修改密码
                PublicKey publicKey = RSAUtils.keyStrToPublicKey(AuthenticationHelper.getPublicKey());
                String oldPassKey = RSAUtils.encryptDataByPublicKey(oldPassword.getBytes(StandardCharsets.UTF_8), publicKey);
                String newPassKey = RSAUtils.encryptDataByPublicKey(newPassword.getBytes(StandardCharsets.UTF_8), publicKey);
                changePasswordPresenter.onReadyRetrofitRequest(oldPassKey, newPassKey);
            }
        });
    }

    @Override
    public void obtainChangePassResult(ActionResultBean resultBean) {
        if (resultBean.getCode() == 200) {
            new AlertMessageDialog.Builder()
                    .setContext(this)
                    .setTitle("温馨提示")
                    .setMessage("密码已修改成功，请重新登录")
                    .setPositiveButton("确定")
                    .setOnDialogButtonClickListener(new AlertMessageDialog.OnDialogButtonClickListener() {
                        @Override
                        public void onConfirmClick() {
                            MainActivity.sendEmptyMessage(Constant.LOGIN_OUT);
                        }
                    })
                    .build().show();
        }
    }

    @Override
    public void obtainDataFail() {
        ToastHelper.showToast("操作失败", ToastHelper.ERROR);
    }
}