package com.sheep.gamegroup.presenter;

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


import com.alibaba.fastjson.JSONObject;

import javax.inject.Inject;

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

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

public class TaskDialogPresenter implements TaskDialogContract.Presenter {
    private ApiService apiService;
    private TaskDialogContract.View view;

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

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

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

    @Override
    public void getTaskState(int o) {

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

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

    }
}
