package com.sheep.gamegroup.presenter;

import android.util.Log;

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.MoreDataEntity;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.FastJsonUtils;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.SpUtils;

import java.util.List;

import javax.inject.Inject;

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

/**
 * Created by ljy on 2018/3/19.
 */

public class SmallSheepPresenter implements SmallSheepContract.Persenter {
    private SmallSheepContract.View view;
    private ApiService apiService;

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

    @Override
    public void getUserInfo() {
        String token = SpUtils.getToken(SheepApp.getInstance());

        Log.e("token---user",token+"");
        apiService.getInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        view.failData2View(baseMessage);

                    }

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        view.updateData2View(baseMessage);

                    }
                });
    }

    @Override
    public void getAwardDetail() {
        apiService.awardDetail(0,100)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onError(BaseMessage baseMessage) {


                    }

                    @Override
                    public void onNext(BaseMessage baseMessage) {

                        if(baseMessage!=null){

                            List<MoreDataEntity> loginEty = baseMessage.getDataList(MoreDataEntity.class);
//                            List<MoreDataEntity> loginEty = FastJsonUtils.toList( JSONObject.toJSONString(baseMessage.getData()),MoreDataEntity.class);
                            if(loginEty != null)
                                view.showMore(loginEty);

                        }


                    }
                });
    }

    @Override
    public void homeList() {
        apiService.homeList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        view.failhomeList(baseMessage);
                    }

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        view.successhomeList(baseMessage);
                    }
                });
    }

    @Override
    public void slideshow(Object o) {
        apiService.slideshow(1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        view.failSlideshow(baseMessage);
                    }

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        view.successSlideshow(baseMessage);
                    }
                });
    }
    @Override
    public void bulletin(Object o) {
        apiService.bulletin(1,10)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        view.failbulletin(baseMessage);
                    }

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        view.successbulletin(baseMessage);
                    }
                });
    }
}
