package com.casic.dcms.ui;

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

import androidx.core.content.ContextCompat;

import com.casic.dcms.R;
import com.casic.dcms.base.BaseActivity;
import com.casic.dcms.bean.ActionResultBean;
import com.casic.dcms.mvp.presenter.ChangePasswordPresenterImpl;
import com.casic.dcms.mvp.view.IChangePasswordView;
import com.casic.dcms.utils.AuthenticationHelper;
import com.casic.dcms.utils.RSAUtils;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.utils.ToastHelper;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

import java.security.PublicKey;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author : Pengxh
 * @time : 2021/4/14 13:20
 * @email : 290677893@qq.com
 **/
public class ChangePasswordActivity extends BaseActivity implements View.OnClickListener, IChangePasswordView {

    private static final String TAG = "ChangePasswordActivity";
    @BindView(R.id.passwordTopLayout)
    QMUITopBarLayout passwordTopLayout;
    @BindView(R.id.originalEditView)
    EditText originalEditView;
    @BindView(R.id.newEditView)
    EditText newEditView;
    @BindView(R.id.confirmEditView)
    EditText confirmEditView;
    @BindView(R.id.changePasswordButton)
    QMUIRoundButton changePasswordButton;
    private ChangePasswordPresenterImpl changePasswordPresenter;

    @Override
    public int initLayoutView() {
        return R.layout.activity_password;
    }

    @Override
    protected void setupTopBarLayout() {
        passwordTopLayout.setTitle("修改密码").setTextColor(ContextCompat.getColor(this, R.color.white));
        passwordTopLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.mainThemeColor));
        passwordTopLayout.addLeftImageButton(R.drawable.ic_left_white, 1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void initData() {
        changePasswordPresenter = new ChangePasswordPresenterImpl(this);
    }

    @Override
    public void initEvent() {
        changePasswordButton.setChangeAlphaWhenPress(true);
    }

    @OnClick(R.id.changePasswordButton)
    @Override
    public void onClick(View v) {
        String oldPassword = originalEditView.getText().toString();
        if (TextUtils.isEmpty(oldPassword)) {
            ToastHelper.showToast("请先输入原密码", ToastHelper.WARING);
            return;
        }
        String newPassword = newEditView.getText().toString();
        if (TextUtils.isEmpty(newPassword)) {
            ToastHelper.showToast("请先输入新密码", ToastHelper.WARING);
            return;
        }
        String confirmPassword = 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(), publicKey);
        String newPassKey = RSAUtils.encryptDataByPublicKey(newPassword.getBytes(), publicKey);
        changePasswordPresenter.onReadyRetrofitRequest(oldPassKey, newPassKey);
    }

    @Override
    public void obtainChangePassResult(ActionResultBean resultBean) {
        if (resultBean.isSuccess()) {
            ToastHelper.showToast("密码修改成功", ToastHelper.SUCCESS);
            finish();
        }
    }

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