package com.sheep.gamegroup.view.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.di.components.DaggerRealNameAutherComponent;
import com.sheep.gamegroup.di.modules.RealNameAutherModule;
import com.sheep.gamegroup.model.entity.DialogConfig;
import com.sheep.gamegroup.model.entity.UserEntity;
import com.sheep.gamegroup.presenter.RealNameAutherContract;
import com.sheep.gamegroup.presenter.RealNameAutherPresenter;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.StringUtils;
import com.sheep.gamegroup.util.TestUtil;
import com.sheep.gamegroup.util.UMConfigUtils;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import java.util.HashMap;
import java.util.Locale;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import rx.functions.Action1;

/**
 * Created by Administrator on 2018/3/26.
 */

public class RealNameAuthenAct extends BaseActivity implements RealNameAutherContract.View {

    @BindView(R.id.et_id_card)
    EditText etIdCard;
    @BindView(R.id.et_bank)
    EditText etBank;
    @BindView(R.id.et_bank_mobile)
    EditText et_bank_mobile;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;
    @BindView(R.id.et_name)
    EditText etName;

    UserEntity userEntity;
    @Inject
    RealNameAutherPresenter presenter;

    HashMap<String, String> map = new HashMap<>();

    private int whereFrom;

    private int count = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.x_real_name_auther_layout;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initListener() {
    }
    public static final int MAX_COUNT = 2;//修改为只验证身份证与姓名
    @Override
    public void initData() {
        DaggerRealNameAutherComponent.builder().netComponent(SheepApp.getInstance().getNetComponent()).realNameAutherModule(new RealNameAutherModule(this)).build().inject(this);
        TitleBarUtils
                .getInstance()
                .setShowOrHide(this, true)
                .setTitle(this, "实名认证")
                .setTitleFinish(this);


        Intent i = getIntent();
        if (i != null && i.hasExtra("info")) {
            whereFrom = i.getIntExtra("where_from", 0);
        }
        tvSubmit.setVisibility(View.GONE);
        userEntity = DataUtil.getInstance().getUserEntity();
        if(userEntity != null){
            if (!TextUtils.isEmpty(RealNameAuthenAct.this.userEntity.getReal_name())) {
                etName.setText(RealNameAuthenAct.this.userEntity.getReal_name());
                count = count + 1;
                etName.setEnabled(false);
            }
            if (!TextUtils.isEmpty(RealNameAuthenAct.this.userEntity.getId_number())) {
                etIdCard.setText(RealNameAuthenAct.this.userEntity.getId_number());
                count = count + 1;
                etIdCard.setEnabled(false);
            }

            if (!TextUtils.isEmpty(RealNameAuthenAct.this.userEntity.getBank_card())) {
                etBank.setText(RealNameAuthenAct.this.userEntity.getBank_card());
                count = count + 1;
                etBank.setEnabled(false);
            }
            if (!TextUtils.isEmpty(RealNameAuthenAct.this.userEntity.getBank_card_mobile())) {
                et_bank_mobile.setText(RealNameAuthenAct.this.userEntity.getBank_card_mobile());
                count = count + 1;
                et_bank_mobile.setEnabled(false);
            }
            if (count >= MAX_COUNT) {
                tvSubmit.setVisibility(View.GONE);
            } else {
                tvSubmit.setVisibility(View.VISIBLE);
                ViewUtil.showMsgDialog(RealNameAuthenAct.this, new DialogConfig().setTitle("温馨提示")
                        .setMsg("根据国家相关法律规定，为保障线上交易活动的安全，参与互联网大额提现等资金活动时需要进行实名认证。\n完成实名认证后，再次提现将不需要进行验证。")
                        .setMsgGravity(Gravity.START).setMsgIndent(2).setBtnLeftText("确定"));
            }

        }


    }

    @Override
    public void NetSuccess(int code, String msg, String string) {
        hideProgress();
        G.showToast(msg);
        updateUserInfo(new Action1<UserEntity>() {

            @Override
            public void call(UserEntity result) {
                if (result != null)
                    userEntity = result;
                    if (whereFrom == 1) {//从提现过来的
                        Jump2View.getInstance().goWithdrawal(RealNameAuthenAct.this);
                    }
                finish();
            }
        });
    }

    private void updateUserInfo(final Action1<UserEntity> action1) {
        CommonUtil.getInstance().updateUserInfo(action1);
    }

    @Override
    public void NetError(int code, String errorMsg) {
        hideProgress();
        updateUserInfo(null);
        G.showToast(errorMsg);
    }


    @OnClick({R.id.tv_submit, R.id.name_tip_iv, R.id.real_name_know_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_submit:
                trySubmit();
                break;
            case R.id.real_name_know_tv:
                ViewUtil.showMsgDialog(this, "安全保障：风控系统实时监控，保护你的资金安全隐私保护：高强度数据加密，保护用户隐私信息了解详情，请致电小绵羊客服：028-88612993");
                break;
            case R.id.name_tip_iv:
                ViewUtil.showMsgDialog(this, "为了保证账户及账户资金安全，只能绑定认证用户本人的银行卡及身份证号", "持卡人说明");
                break;
        }
    }

    public void trySubmit() {
        if (userEntity == null) {
            userEntity = DataUtil.getInstance().getUserEntity();
        }
        if (userEntity == null) {
            G.showToast(R.string.unknown_error);
            return;
        }
        map.clear();
        toNext();
    }

    public void toNext() {
        String realName;
        String idNumber;
        String bankCard;
        String bankCardMobile;
        if (TextUtils.isEmpty((realName = etName.getText().toString()))) {
            if (TestUtil.isDev()) {
                NetSuccess(0, "测试成功", "");
                return;
            }
            G.showToast("姓名不能为空");
            return;
        } else if (TextUtils.isEmpty((idNumber = etIdCard.getText().toString()))) {
            G.showToast("身份证不能为空");
            return;
        } else if (MAX_COUNT >= 4 && TextUtils.isEmpty((bankCard = etBank.getText().toString()))) {
            G.showToast("银行卡号不能为空");
            return;
        } else if (MAX_COUNT >= 4 && TextUtils.isEmpty((bankCardMobile = et_bank_mobile.getText().toString()))) {
            G.showToast("银行行预留手机号不能为空");
            return;
        }
        if (!StringUtils.isChinese(realName)) {
            G.showToast("姓名必须是基本汉字！");
            return;
        }
        map.put("real_name", realName);

        String str = StringUtils.IDCardValidate(idNumber);
        if (!str.equals("YES")) {
            G.showToast(str + ":请填写正确的身份证信息哈！");
            return;
        }
        map.put("id_number", idNumber);
        if (MAX_COUNT >= 4) {
            if (!StringUtils.checkBankCard(bankCard)) {
                G.showToast("银行卡不合法哈!");
                return;
            }
            map.put("bank_card", bankCard);

            if (!StringUtils.isMobile(bankCardMobile)) {
                G.showToast("银行卡预留手机号不合法哈!");
                return;
            }
            map.put("bank_card_mobile", bankCardMobile);
        }

        ViewUtil.showMsgDialog(this, new DialogConfig().setTitle("重要提示").setMsgMore(String.format(Locale.CHINA, "你已执行%d次实名认证操作", userEntity.getCertification_count()))
                .setMsg("实名认证首次免费。请务必确认您所提交的认证信息真实有效。若因信息填写错误造成认证失败。再次申请认证时需扣除您0.5绵羊币/次作为认证费用，请务必谨慎填写认证信息。")
                .setMsgColor("0.5绵羊币/次", "#FF0000").setMsgIndent(2).setMsgGravity(Gravity.START)
                .setBtnLeftText("返回修改").setBtnRightText("确认提交")
                .setBtnRightOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (userEntity != null)
                            userEntity.setCertification_count(userEntity.getCertification_count() + 1);
                        presenter.getTask(map);
                        UMConfigUtils.onEvent(UMConfigUtils.Event.USER_AUTHENTICATION);
                    }
                }));
    }


    @Override
    public void showGoProgress() {
        showProgress();
    }
}
