package com.sheep.gamegroup.presenter;

import com.sheep.gamegroup.model.api.ApiService;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.RequestParamEty;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.jiuyan.samllsheep.SheepApp;

import java.util.HashMap;

import javax.inject.Inject;

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

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

public class TryMakeMoneyPresenter implements TryMakeMoneyContract.Presenter {
    private TryMakeMoneyContract.View view;
    private ApiService apiService;

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

    @Override
    public void getData(Object o) {

    }

    @Override
    public void acceptedTask(RequestParamEty o) {
        apiService.acceptedTask(o.getPage(), o.getPer_page(), 0)
                .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);

                    }
                });

    }

    /**
     * 获取正在运行的任务
     * @param is_succession
     * @param show_game_task: 0 不显示游戏任务 , 1 显示游戏任务
     */
    @Override
    public void returnTask(int is_succession, int show_game_task) {

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

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

                    }
                });
    }
    @Override
    public void runGameTask() {

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

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

                    }
                });
    }

    @Override
    public void releaseTask(RequestParamEty o) {

        HashMap<String, String> hashMap = o.getHashMap();
        final int about_to_begin = Integer.valueOf(hashMap.get("about_to_begin"));
        apiService.releaseTask(Integer.valueOf(hashMap.get("page")),Integer.valueOf(hashMap.get("per_page")),
                Integer.valueOf(hashMap.get("platform")),Integer.valueOf(hashMap.get("is_hot")),
                Integer.valueOf(hashMap.get("is_recommend")),about_to_begin,
                hashMap.get("task_type"), Integer.valueOf(hashMap.get("is_succession")))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        view.failReleaseTask2View(baseMessage, about_to_begin);
                    }

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        view.releaseTaskTaskUpdateView(baseMessage, about_to_begin);

                    }
                });
    }

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

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