package com.sheep.gamegroup.view.activity;

import android.content.Intent;
import android.widget.EditText;
import android.widget.TextView;

import com.googlecode.protobuf.format.util.TextUtils;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.di.components.DaggerRealNameAutherComponent;
import com.sheep.gamegroup.di.modules.RealNameAutherModule;
import com.sheep.gamegroup.event.RealNameAutehrChange;
import com.sheep.gamegroup.model.entity.UserEntity;
import com.sheep.gamegroup.presenter.RealNameAutherContract;
import com.sheep.gamegroup.presenter.RealNameAutherPresenter;
import com.sheep.gamegroup.util.StringUtils;
import com.sheep.gamegroup.util.UMConfigUtils;
import com.sheep.gamegroup.view.dialog.DialogRealNameAuther;
import com.sheep.jiuyan.samllsheep.R;

import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 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.tv_submit)
    TextView tvSubmit;
    @BindView(R.id.et_name)
    EditText etName;

    UserEntity mEntity;
    @Inject
    RealNameAutherPresenter  presenter;

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

    private  int  count=0;

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

    @Override
    public void initView() {

    }

    @Override
    public void initListener() {
    }
    @Override
    public void initData() {
       DaggerRealNameAutherComponent.builder().netComponent(SheepApp.get(this).getNetComponent()).realNameAutherModule( new RealNameAutherModule(this)).build().inject(this);
        TitleBarUtils
                .getInstance()
                .setTitle(this, "实名认证")
                .setTitleFinish(this);


        Intent i = getIntent();
        if (i != null && i.hasExtra("info")) {
            mEntity = (UserEntity) i.getSerializableExtra("info");
            if(mEntity!=null) {

                if(!android.text.TextUtils.isEmpty(mEntity.getReal_name())){
                    etName.setText(mEntity.getReal_name());
                    count=count+1;
                    etName.setEnabled(false);
                }
                if(!android.text.TextUtils.isEmpty(mEntity.getId_number())){
                    etIdCard.setText(mEntity.getId_number());
                    count=count+1;
                    etIdCard.setEnabled(false);
                }

                if(!android.text.TextUtils.isEmpty(mEntity.getBank_card())){
                    etBank.setText(mEntity.getBank_card());
                    count=count+1;
                    etBank.setEnabled(false);
                }
                if(count>=3){
                    tvSubmit.setEnabled(false);
                    tvSubmit.setBackgroundColor(getResources().getColor(R.color.txt_black_9e9c9c));
                }else{
                    DialogRealNameAuther.showDialog(this);
                }

            }
        }


    }

    @Override
    public void NetSuccess(int code, String msg) {
          hideProgress();
          showToast(msg);
           RealNameAutehrChange  change=new RealNameAutehrChange();
           for(String key:map.keySet())
           {
              if(key.equals("real_name")&& !android.text.TextUtils.isEmpty(map.get(key))){
                  change.setRealName(map.get(key));
              }
               if(key.equals("id_number")&& !android.text.TextUtils.isEmpty(map.get(key))){
                   change.setIdCard(map.get(key));
               }
               if(key.equals("bank_card")&& !android.text.TextUtils.isEmpty(map.get(key))){
                   change.setBanck(map.get(key));
               }

         }
        EventBus.getDefault().post(change);
          finish();
    }

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



    @OnClick(R.id.tv_submit)
    public void onClick() {
        String realName;
        String idNumber;
        if(android.text.TextUtils.isEmpty((realName = etName.getText().toString()))){
           showToast("姓名不能为空");
           return;
        } else if(android.text.TextUtils.isEmpty((idNumber = etIdCard.getText().toString()))){
            showToast("身份证不能为空");
            return;
        }
        map.put("real_name", realName);
        String str=StringUtils.IDCardValidate(idNumber);
         if(!str.equals("YES")){
             showToast(str+":请填写正确的身份证信息哈！");
             return ;

         }else{
             map.put("id_number", idNumber);
//             if(!android.text.TextUtils.isEmpty(etBank.getText().toString())){
//                 if(StringUtils.checkBankCard(etBank.getText().toString())){
//                     map.put("bank_card",etBank.getText().toString());
//                 }else{
//                     showToast("银行卡不合法哈!");
//                     return ;
//
//                 }
//             }
         }


        if(map.size()>0){
            presenter.getTask(map);
            UMConfigUtils.onEvent(UMConfigUtils.Event.USER_AUTHENTICATION);
        }else{
            showToast("亲！ 请填写修改信息哈");
        }





    }


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