package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.kfzs.duanduan.event.BigEvent;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.di.components.DaggerAccountAndSercuritComponent;
import com.sheep.gamegroup.di.modules.AccountAndSercuritModule;
import com.sheep.gamegroup.event.ImageHeadChange;
import com.sheep.gamegroup.event.MoneyChange;
import com.sheep.gamegroup.event.RealNameAutehrChange;
import com.sheep.gamegroup.event.UserNameChange;
import com.sheep.gamegroup.model.entity.UserEntity;
import com.sheep.gamegroup.presenter.AccountAndSecuritContract;
import com.sheep.gamegroup.presenter.AccountAndSecuritPresenter;
import com.sheep.gamegroup.util.ActivityManager;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.MyDbManager;
import com.sheep.gamegroup.util.UMConfigUtils;
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.SpUtils;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

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

public class AccountAndSecurityAct extends BaseActivity implements AccountAndSecuritContract.View {

    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_link_content)
    TextView tvLinkContent;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.rl_phone)
    RelativeLayout rlPhone;
    @BindView(R.id.tv_wx)
    TextView tvWx;
    @BindView(R.id.rl_wx)
    RelativeLayout rlWx;
    @BindView(R.id.tv_auther)
    TextView tvAuther;
    @BindView(R.id.rl_auther)
    RelativeLayout rlAuther;
    @BindView(R.id.rl_top)
    RelativeLayout rlTop;

    @Inject
    AccountAndSecuritPresenter presenter;
    UserEntity mEntity;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;

    private Activity activity;

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

    @Override
    public void initView() {
        activity = this;
        TitleBarUtils
                .getInstance()
                .setTitle(this, "账号与安全")
                .setTitleFinish(this);

        DaggerAccountAndSercuritComponent.builder()
                .netComponent(SheepApp.get(this).getNetComponent())
                .accountAndSercuritModule(new AccountAndSercuritModule(this))
                .build().inject(this);

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        presenter.getTask(null);
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(UserNameChange done) {
        UserEntity user=mEntity;
        user.setNickname(done.getNikeName());
        mEntity=user;
        tvName.setText(mEntity.getNickname());

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ImageHeadChange done) {
        UserEntity user=mEntity;
        user.setAvatar(done.getAvatar());
        mEntity=user;
        Glide.with(AccountAndSecurityAct.this)
                .load(mEntity.getAvatar())
                .apply(new RequestOptions().circleCrop().placeholder(R.drawable.icon))
                .into(ivHead);
    }



    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RealNameAutehrChange done) {

        UserEntity entiy=mEntity;
        entiy.setReal_name(done.getRealName());
        entiy.setId_number(done.getIdCard());
        entiy.setBank_card(done.getBanck());
        mEntity=entiy;
        refalshAuther(mEntity);

    }




    @OnClick({R.id.rl_phone, R.id.rl_wx, R.id.rl_auther, R.id.rl_top,R.id.tv_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_phone:   //跳转到绑定手机号
                Jump2View.getInstance().goBindPhone(activity, null);
                break;
            case R.id.rl_wx:  ////跳转到微信
                if(mEntity == null){
                    G.showToast("数据正在加载中，请稍候");
                } else if(TextUtils.isEmpty(mEntity.getWx_nickname())) {
                    Jump2View.getInstance().goBindOrChangeWeixinView(AccountAndSecurityAct.this, false);
                } else {
                    G.showToast("您已经绑定微信");
                }
                break;
            case R.id.rl_auther: ////跳转授权
                Jump2View.getInstance().goRealNameAuther(AccountAndSecurityAct.this,mEntity);
                break;

            case R.id.rl_top:
                Jump2View.getInstance().goPersionInfo(AccountAndSecurityAct.this,mEntity);
                break;
            case R.id.tv_submit:
                CommonUtil.getInstance().loginOut((BaseActivity) activity);
                break;
        }
    }

    @Override
    public void NetSuccess(int code, String msg) {
        showToast(msg);
    }

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

    }


    @Override
    public void showTask(final UserEntity entity) {
        UserEntity userEntity = null;
        try{
            userEntity = MyDbManager.getInstance()
                    .dbFindUser();
            if(userEntity == null){
                userEntity = new UserEntity();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
                mEntity = entity;
                tvName.setText(entity.getNickname());
                tvLinkContent.setText("绵羊号:" + entity.getInvitation_code());
                if (TextUtils.isEmpty(entity.getMobile())) {
                    tvPhone.setText("未绑定");
                } else {
                    tvPhone.setText(entity.getMobile());
                    userEntity.setMobile(entity.getMobile());
                    SpUtils.savePhone(activity, entity.getMobile());
                }

                Glide.with(AccountAndSecurityAct.this)
                        .load(mEntity.getAvatar())
                        .apply(new RequestOptions().circleCrop().placeholder(R.drawable.icon))
                        .into(ivHead);
                if (TextUtils.isEmpty(entity.getWx_nickname())) {
                    tvWx.setText("未绑定");
                } else {
                    tvWx.setText(entity.getWx_nickname());
                    SpUtils.saveWeixin(activity, entity.getWx_nickname());
                    userEntity.setWx_nickname(entity.getWx_nickname());
                }

                refalshAuther(entity);
                MoneyChange change=new MoneyChange();
                change.setBalance(entity.getBalance()+"");
                change.setTotal_asset(mEntity.getTotal_asset()+"");
                EventBus.getDefault().post(change);


                //Todo user 类都没有统一，先这样
                try{
                    MyDbManager.getInstance()
                            .saveOrUpdateUser(mEntity);
                }catch (Exception e){
                    e.printStackTrace();
                }


    }


    @Subscribe
    public void onEventMainThread(BigEvent event){
        switch (event.getEventTypes()){
            case REFRESH_DATA_PHONE:
                initData();
                break;
        }
    }

    private  void  refalshAuther(UserEntity entity ){
        boolean isBanck = TextUtils.isEmpty(entity.getBank_card());
        boolean isId_number = TextUtils.isEmpty(entity.getId_number());
        boolean isRoalName = TextUtils.isEmpty(entity.getReal_name());
        if (isBanck && isId_number && isRoalName) {
            tvAuther.setText("未认证");
        } else if (!isBanck && !isId_number && !isRoalName) {
            tvAuther.setText("已认证");
        } else {
            tvAuther.setText("认证中");
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
