package com.sheep.gamegroup.presenter;

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.SheepSubscriber;
import com.sheep.gamegroup.util.ChannelContent;
import com.sheep.gamegroup.util.FastJsonUtils;
import com.sheep.gamegroup.util.MyDbManager;
import com.sheep.gamegroup.util.PreferenceUtils;
import com.sheep.gamegroup.util.UMConfigUtils;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.SpUtils;


import org.xutils.ex.DbException;

import javax.inject.Inject;

import rx.android.schedulers.AndroidSchedulers;
import rx.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.mContext, UMConfigUtils.LOGIN_TYPE,UMConfigUtils.Source.WECHAT);
            apiService.LoginByWX(j).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.mContext) {
                        @Override
                        public void onError(BaseMessage baseMessage) {
                            view.NetError(-1, "微信登录失败  请检查网络是否正常");
                        }

                        @Override
                        public void onNext(BaseMessage baseMessage) {

                            if(baseMessage!=null){
                                LoginEntity loginEty =FastJsonUtils.toBean( JSONObject.toJSONString(baseMessage.getData()),LoginEntity.class);
                                SpUtils.login(SheepApp.mContext, loginEty.getToken());
                                try {
                                    MyDbManager.getInstance().saveOrUpdateUser(loginEty.getUser());
                                } catch (DbException e) {
                                    e.printStackTrace();
                                }
                            }

                            view.NetSuccess(1,"登录成功");

                        }
                    });

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

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

                        @Override
                        public void onNext(BaseMessage baseMessage) {
                            if(baseMessage!=null){
                                LoginEntity loginEty =FastJsonUtils.toBean( JSONObject.toJSONString(baseMessage.getData()),LoginEntity.class);
                                SpUtils.login(SheepApp.mContext, loginEty.getToken());
                                try {
                                    MyDbManager.getInstance().saveOrUpdateUser(loginEty.getUser());
                                } catch (DbException e) {
                                    e.printStackTrace();
                                }
                            }
                            view.NetSuccess(1,"登录成功");

                        }
                    });

        }


    }
}
