package com.sheep.gamegroup.presenter;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.sheep.gamegroup.model.api.ApiService;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.LoginEntity;
import com.sheep.gamegroup.model.util.EntityUtils;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.ChannelContent;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.FastJsonUtils;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.LogUtil;
import com.sheep.gamegroup.util.PreferenceUtils;
import com.sheep.gamegroup.util.TestUtil;
import com.sheep.gamegroup.util.UMConfigUtils;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.SpUtils;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

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

public class LoginPresenter implements LoginContract.Presenter {
    private LoginContract.View view;
    private ApiService apiService;

    @Inject
    public LoginPresenter(LoginContract.View view, ApiService apiService) {
        this.view = view;
        this.apiService = apiService;
    }


    @Override
    public void logByAuther(int type, String access_token, String openid) {
        JSONObject j = new JSONObject();
        j.put("code",access_token);
        j.put("username",openid);
        j.put("scope",ChannelContent.getInstance().getChannel_name() );
        if(type==1){
            PreferenceUtils.setPrefString(SheepApp.getInstance(), UMConfigUtils.LOGIN_TYPE,UMConfigUtils.Source.WECHAT);
            apiService.LoginByWX(j).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                        @Override
                        public void onError(BaseMessage baseMessage) {
                            view.NetError(-1, "微信登录失败  请检查网络是否正常");
                        }

                        @Override
                        public void onNext(BaseMessage baseMessage) {
                            loginSuccess(view, baseMessage);
                        }
                    });

        }else{
            PreferenceUtils.setPrefString(SheepApp.getInstance(), UMConfigUtils.LOGIN_TYPE,UMConfigUtils.Source.QQ);
            apiService.LoginByQQ(j).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {

                        @Override
                        public void onError(BaseMessage baseMessage) {
                            view.NetError(-1, "QQ登录失败  请检查网络是否正常");
                        }

                        @Override
                        public void onNext(BaseMessage baseMessage) {
                            loginSuccess(view, baseMessage);

                        }
                    });

        }


    }
    private void loginSuccess(LoginContract.View view, BaseMessage baseMessage){
        LoginEntity loginEty = null;
        if(baseMessage!=null){
            loginEty =FastJsonUtils.toBean( JSONObject.toJSONString(baseMessage.getData()),LoginEntity.class);
            if(loginEty.getCode() != null){
                switch (loginEty.getCode()){
                    case "bind_mobile":
                        view.NetError(1, "请先绑定手机号");
                        Jump2View.getInstance().goBindPhoneRegister(SheepApp.getInstance(), loginEty);
                        return;
                }
            }
            SpUtils.saveToken(SheepApp.getInstance(), loginEty.getToken());
            DataUtil.getInstance().initUserEntity(loginEty.getUser());
//            LogUtil.logI("token--------"+loginEty.getToken());
        }
        view.NetSuccess(1,"登录成功", EntityUtils.getUserCode(loginEty));
        TestUtil.saveUser(loginEty);
    }
}
