package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.support.v7.widget.AppCompatEditText;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.sheep.gamegroup.di.components.DaggerBindOrChangeWeixinComponent;
import com.sheep.gamegroup.di.modules.BindOrChangeWeixinModule;
import com.sheep.gamegroup.event.BindWXResultEvent;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.UserEntity;
import com.sheep.gamegroup.presenter.BindOrChangeWeixinContract;
import com.sheep.gamegroup.presenter.BindOrChangeWeixinPresenter;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.TestUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 绑定微信或更改微信
 * Created by ljy on 2018/3/18.
 */

public class BindOrChangeWeixinAct extends BaseActivity implements BindOrChangeWeixinContract.View {

    @BindView(R.id.weinxin_et_account)
    EditText weinxinEtAccount;
    @BindView(R.id.bindweixin_sure_text)
    TextView bindweixinSureText;

    @Inject
    BindOrChangeWeixinPresenter presenter;

    private String code;
    private boolean isBindOrChange;
    private int from;
    private String isBindOrChangeStr;


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

    @Override
    public void initView() {
        isBindOrChange = getIntent().getBooleanExtra("type", false);
        from = getIntent().getIntExtra("from", ChangeWxOrTelAct.TYPE_NORMAL);
        if(isBindOrChange){
            isBindOrChangeStr = "更换微信号";
        }else {
            isBindOrChangeStr = "绑定微信号";
        }
        TitleBarUtils
                .getInstance()
                .setShowOrHide(this, true)
                .setTitle(this, isBindOrChangeStr)
                .setTitleFinish(this);
        DaggerBindOrChangeWeixinComponent.builder()
                .netComponent(SheepApp.getInstance().getNetComponent())
                .bindOrChangeWeixinModule(new BindOrChangeWeixinModule(this))
                .build()
                .inject(this);
        userEntity = DataUtil.getInstance().getUserEntity();
    }
    private UserEntity userEntity;

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @OnClick(R.id.bindweixin_sure_text)
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.bindweixin_sure_text:
                code = weinxinEtAccount.getText().toString();
                if(TextUtils.isEmpty(code)){
                    if(TestUtil.isDev()){
                        updateView(null);
                        return;
                    }
                    G.showToast("请输入验证码");
                    return;
                }
                submit();
                break;
        }
    }

    private void submit() {
        JSONObject j = new JSONObject();
        j.put("code",code );
        showProgress();
        presenter.bindWeixin(j);
    }

    @Override
    public void updateView(Object object) {
        hideProgress();
        G.showToast("提交成功");
        CommonUtil.getInstance()
                .getUserInfo(SheepApp.getInstance());
        if(from == ChangeWxOrTelAct.TYPE_RECHARGE){//来自充值功能,需要检查并跳转到绑定身份与银行卡信息界面
            if(
                    TestUtil.isDev() ||
                    userEntity != null && userEntity.notCertification()){//没有实名认证
                Jump2View.getInstance().goRealNameAuther(this, userEntity, 1);
            }
        }
        finish();
        CommonUtil.getInstance().updateUserInfo(null);
        EventBus.getDefault().post(new BindWXResultEvent());
    }

    @Override
    public void failData(Object o) {
        hideProgress();
        G.showToast(((BaseMessage)o).getMsg()+ "");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
