package com.baoshi.swms.net.subscribe;

import com.baoshi.swms.bean.User;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import io.reactivex.Observable;
import io.reactivex.observers.DisposableObserver;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * SWMS API请求订阅器
 *
 * @author Zhendong Zhou
 * @date 2021/12/25 14:52
 */
public class SwmsSubscribe {
    private static final Long userId = Long.valueOf(User.getLocalUser().getId());

    public static void getPickingTask(String waveType, DisposableObserver<ResponseBody> subscriber) {
        Observable<ResponseBody> observable = SwmsBaseSubscribe.getOkHttpApi().getPickingTask(userId,waveType);
        WasBaseSubscribe.toSubscribe(observable, subscriber);
    }

    public static void getUserTodayWorkNum(DisposableObserver<ResponseBody> subscriber) {
        Observable<ResponseBody> observable = SwmsBaseSubscribe.getOkHttpApi().getUserTodayWorkNum(userId);
        WasBaseSubscribe.toSubscribe(observable, subscriber);
    }

    public static void giveUpTask(String code, DisposableObserver<ResponseBody> subscriber){
        Observable<ResponseBody> observable = SwmsBaseSubscribe.getOkHttpApi().giveUpTask(userId,code);
        WasBaseSubscribe.toSubscribe(observable, subscriber);
    }

    public static void picking(ArrayList<HashMap<String,Object>> items, String code, Boolean completeSign, DisposableObserver<ResponseBody> subscriber){
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("items", items);
        map.put("code", code);
        map.put("completeSign", completeSign);

        MediaType mediaType=MediaType.Companion.parse("application/json;charset=utf-8");
        RequestBody body=RequestBody.Companion.create(new JSONObject(map).toString(),mediaType);
        Observable<ResponseBody> observable = SwmsBaseSubscribe.getOkHttpApi().picking(body);
        WasBaseSubscribe.toSubscribe(observable, subscriber);
    }
}
