package com.bc_chat.im.activity.envelopes;

import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bc_chat.bc_base.config.RouteConfig;
import com.bc_chat.bc_base.contract.ApplyWithdrawContract;
import com.bc_chat.bc_base.presenter.ApplyWithdrawPresenter;
import com.bc_chat.im.R;
import com.bc_chat.im.activity.envelopes.adapter.CodeUtils;
import com.jrmf360.rylib.common.util.ToastUtil;
import com.jrmf360.rylib.rp.widget.ClearEditText;
import com.zhaohaoting.framework.abs.BaseActivity;
import com.zhaohaoting.framework.utils.ToastUtils;

import org.jetbrains.annotations.Nullable;

/**
 *
 */
@Route(path = RouteConfig.APPLY_WITHDRAWAL_ACTIVITY)
public class ApplyWithdrawalActivity extends BaseActivity<ApplyWithdrawPresenter> implements ApplyWithdrawContract.View {

    private ClearEditText etPlatform;
    private ClearEditText etAccount;
    private ClearEditText etAmount;
    private ClearEditText etReason;
    private ClearEditText etDesc;
    private ClearEditText etVerifyCode;
    private ImageView ivVerifyCode;

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

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

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

    private void initView() {
        etPlatform = findViewById(R.id.etPlatform);
        etAccount = findViewById(R.id.etAccount);
        etAmount = findViewById(R.id.etAmount);
        etReason = findViewById(R.id.etReason);
        etDesc = findViewById(R.id.etDesc);
        etVerifyCode = findViewById(R.id.etVerifyCode);
        ivVerifyCode = findViewById(R.id.ivVerifyCode);
        ivVerifyCode.setImageBitmap(CodeUtils.getInstance().createBitmap());
        ivVerifyCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ivVerifyCode.setImageBitmap(CodeUtils.getInstance().createBitmap());
            }
        });
        findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                doAppWithdraw();
            }
        });
        findViewById(R.id.btn_record).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build(RouteConfig.WITHDRAWAL_RECORD_ACTIVITY).navigation();
            }
        });
    }

    private void doAppWithdraw() {
        String platform = etPlatform.getText().toString().trim();
        if (TextUtils.isEmpty(platform)) {
            ToastUtil.showToast(this, "请输入平台名称");
            return;
        }
        String account = etAccount.getText().toString().trim();
        if (TextUtils.isEmpty(account)) {
            ToastUtil.showToast(this, "请输入会员账号");
            return;
        }
        String amount = etAmount.getText().toString().trim();
        if (TextUtils.isEmpty(amount)) {
            ToastUtil.showToast(this, "请输入提现金额");
            return;
        }
        String reason = etReason.getText().toString().trim();
        if (TextUtils.isEmpty(reason)) {
            ToastUtil.showToast(this, "请输入提现理由");
            return;
        }
        String code = etVerifyCode.getText().toString().trim();
        if (TextUtils.isEmpty(code) || !CodeUtils.getInstance().getCode().equalsIgnoreCase(code)) {
            ToastUtil.showToast(this, "验证码为空或者错误");
            return;
        }
        String desc = etDesc.getText().toString().trim();
        getPresenter().applyWithdraw(amount, account, platform, reason, desc);
    }

    @Override
    public void applyWithdrawSuccess() {
        ToastUtils.show("提交成功，请等级审核");
        finish();
    }

    @Override
    public void applyWithdrawFailure(@Nullable Exception exception) {
//        ToastUtils.show("提交失败，请重新提交");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        return true;
    }

}
