package com.bc_chat.im.activity.envelopes;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bc_chat.bc_base.config.RouteConfig;
import com.bc_chat.bc_base.contract.PayPasswordContract;
import com.bc_chat.bc_base.presenter.PayPasswordPresenter;
import com.bc_chat.im.R;
import com.zhaohaoting.framework.abs.BaseActivity;
import com.zhaohaoting.framework.utils.Preferences;
import com.zhaohaoting.framework.utils.ToastUtils;

import org.jetbrains.annotations.Nullable;

/**
 * 修改支付密码
 */
@Route(path = RouteConfig.MODIFY_PAY_PASSWORD_ACTIVITY)
public class ModifyPayPasswordActivity extends BaseActivity<PayPasswordPresenter> implements PayPasswordContract.View {

    public static final int REQUEST_CODE_SET = 100; //设置
    public static final int REQUEST_CODE_MODIFY = 101; //修改
    private Button btn_confirm;
    private EditText et_original_password;
    private EditText et_password;
    private EditText et_confirm_password;
    private boolean isSet;

    public static void start(Activity activity, int requestCode) {
        Intent intent = new Intent(activity, ModifyPayPasswordActivity.class);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected PayPasswordPresenter initPresenter() {
        return new PayPasswordPresenter<>(this);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_modify_payment_password;
    }

    @Override
    protected void initialize() {
        initView();
        initListener();
    }

    private void initListener() {
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                confirm();
            }
        });
    }

    private void confirm() {
        String original_password = et_original_password.getText().toString().trim();
        String password = et_password.getText().toString().trim();
        String confirm_password = et_confirm_password.getText().toString().trim();
        if (Preferences.getIsSetPayPwd()) {
            if (TextUtils.isEmpty(original_password)) {
                ToastUtils.show("请输入原密码");
                return;
            }
            if (TextUtils.isEmpty(password)) {
                ToastUtils.show("请输入新密码");
                return;
            }
            if (TextUtils.isEmpty(confirm_password)) {
                ToastUtils.show("请再次输入新密码");
                return;
            }
            if (password.equals(confirm_password)) {
                ToastUtils.show("两次输入的新密码不一致");
                return;
            }
            getPresenter().editPayKey(original_password, confirm_password);
        } else {
            if (TextUtils.isEmpty(password)) {
                ToastUtils.show("请输入新密码");
                return;
            }
            if (TextUtils.isEmpty(confirm_password)) {
                ToastUtils.show("请再次输入新密码");
                return;
            }
            if (password.equals(confirm_password)) {
                ToastUtils.show("两次输入的新密码不一致");
                return;
            }
            getPresenter().setPayKey(password);
        }
    }

    private void initView() {
        btn_confirm = findViewById(R.id.btn_confirm);
        et_original_password = findViewById(R.id.et_original_password);
        et_password = findViewById(R.id.et_password);
        et_confirm_password = findViewById(R.id.et_confirm_password);

        if (Preferences.getIsSetPayPwd()) {
            setTitle("修改支付密码");
        } else {
            setTitle("设置支付密码");
            et_original_password.setVisibility(View.GONE);
        }
    }

    @Override
    public void editPayKeySuccess() {
        ToastUtils.show("修改成功");
        finish();
    }

    @Override
    public void setPayKeySuccess() {
        ToastUtils.show("设置成功");
        Preferences.setIsSetPayPwd(true);
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void editPayKeyFailure(@Nullable Exception exception) {
    }

    @Override
    public void setPayKeyFailure(@Nullable Exception exception) {
    }

}
