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.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kfzs.duanduan.event.BigEvent;
import com.kfzs.duanduan.event.EventTypes;
import com.sheep.gamegroup.di.components.DaggerBindOrChangeWeixinComponent;
import com.sheep.gamegroup.di.modules.BindOrChangeWeixinModule;
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.ActivityManager;
import com.sheep.gamegroup.util.MyDbManager;
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.SpUtils;
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)
    AppCompatEditText weinxinEtAccount;
    @BindView(R.id.bindweixin_sure_text)
    TextView bindweixinSureText;

    Activity activity;
    @Inject
    BindOrChangeWeixinPresenter presenter;

    private String code;
    private boolean isBindOrChange;
    private String isBindOrChangeStr;


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

    @Override
    public void initView() {
        activity = this;
        isBindOrChange = getIntent().getBooleanExtra("type", false);
        if(isBindOrChange){
            isBindOrChangeStr = "更换微信号";
        }else {
            isBindOrChangeStr = "绑定微信号";
        }
        TitleBarUtils
                .getInstance()
                .setTitle(activity, "绑定微信号")
                .setTitleFinish(activity);
        DaggerBindOrChangeWeixinComponent.builder()
                .netComponent(SheepApp.get(this).getNetComponent())
                .bindOrChangeWeixinModule(new BindOrChangeWeixinModule(this))
                .build()
                .inject(this);
    }

    @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)){
                    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 o) {
        hideProgress();
        G.showToast("提交成功");
        try {
            UserEntity userEntity = MyDbManager.getInstance().dbFindUser();
            if(userEntity == null){
                userEntity.setWx_openid("succes");
                MyDbManager.getInstance().saveOrUpdateUser(userEntity);
                SpUtils.saveWeixin(activity, "succes");

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ActivityManager.getInstance()
                .endActivity(ChangeWxOrTelAct.class);
        finish();
        EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.ONFRESH_FGT_FAMLLSHEEP));
        EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.REFRESH_DATA_PHONE));
    }

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

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