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.util.SheepSubscriber;
import com.sheep.jiuyan.samllsheep.SheepApp;

import javax.inject.Inject;

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

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

public class TaskListPresenter implements TaskListContract.Presenter {
    private TaskListContract.View view;
    private ApiService apiService;

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

    @Override
    public void acceptedTask(int page, int pre_page) {
        apiService.acceptedTask(page, pre_page, 0)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        view.failView(baseMessage);
                    }

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

    @Override
    public void taskStatus(JSONObject jsonObject) {

        apiService.taskStatus(jsonObject.getInteger("id"),jsonObject.getString("screenshots"),
                jsonObject.getString("remark"),jsonObject.getString("package_name"),jsonObject.getString("status"),jsonObject.getString("device"))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        view.taskStatusFaile(baseMessage);
                    }

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