package com.hw.nativeapp.utils;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.SPUtils;
import com.hw.nativeapp.config.Contents;
import com.hw.nativeapp.httpnet.entity.BottleCode;

import java.util.ArrayList;
import java.util.List;

public class StockUtils {

    //设置操作仓库
    public static void setOpStock(JSONObject stockJson){
        SPUtils.getInstance().put(Contents.SPU_DEFAULT_STOCK_KEY, stockJson.toJSONString());
    }

    //获取操作仓库信息
    public static JSONObject getOpStock(){
        JSONObject result = new JSONObject();
        String cacheStr = SPUtils.getInstance().getString(Contents.SPU_DEFAULT_STOCK_KEY);
        if (!StringUtils.isEmpty(cacheStr)){
            JSONObject cachceJson = JSON.parseObject(cacheStr);
            result.put("houseId", cachceJson.getString("id"));
            result.put("name", cachceJson.getString("name"));
            JSONArray childList = cachceJson.getJSONArray("child");
            result.put("child", childList);
            List<String> childIds = new ArrayList<>();
            if (childList !=  null && !childList.isEmpty()){
                for (Object item : childList) {
                    JSONObject itemJson = JSON.parseObject(JSON.toJSONString(item));
                    boolean isCheck = itemJson.containsKey("isCheck")?itemJson.getBoolean("isCheck"):false;
                    if (isCheck){
                        childIds.add(itemJson.getString("id"));
                    }
                }
            }
            if (!childIds.isEmpty()){
                result.put("postId", StringUtils.splitJoin(childIds, ","));
            }
            return result;
        }else {
            return null;
        }

    }

    //设置瓶码信息
    public static void setBottomCode(JSONArray bottomCode){
        JSONObject result = new JSONObject();
        result.put("result", bottomCode);
        SPUtils.getInstance().put(Contents.SPU_BOTTOM_CODE_KEY, result.toJSONString());
    }

    //获取瓶码信息
    public static JSONArray getBottomCode() {
        JSONArray result = new JSONArray();
        String cacheStr = SPUtils.getInstance().getString(Contents.SPU_BOTTOM_CODE_KEY);
        if (!StringUtils.isEmpty(cacheStr)){
            JSONObject map = JSON.parseObject(cacheStr);
            result = map.getJSONArray("result");
        }
        return result;
    }

    //设置采购入库物流码信息
    public static void setBarcodeList(JSONArray barcodeList){
        JSONObject result = new JSONObject();
        result.put("result", barcodeList);
        SPUtils.getInstance().put(Contents.SPU_BOTTOM_CODE_KEY, result.toJSONString());
    }

    //获取采购入库物流码信息
    public static JSONArray getBarcodeList() {
        JSONArray result = new JSONArray();
        String cacheStr = SPUtils.getInstance().getString(Contents.SPU_BOTTOM_CODE_KEY);
        if (!StringUtils.isEmpty(cacheStr)){
            JSONObject map = JSON.parseObject(cacheStr);
            result = map.getJSONArray("result");
        }
        return result;
    }

    //设置审核流程信息
    public static void setProcessObj(JSONObject ProcessObj){
        JSONObject result = new JSONObject();
        result.put("result", ProcessObj);
        SPUtils.getInstance().put(Contents.SPU_PROCESS_KEY, result.toJSONString());
    }

    //获取审核流程信息
    public static JSONObject getProcessObj() {
        JSONObject result = new JSONObject();
        String cacheStr = SPUtils.getInstance().getString(Contents.SPU_PROCESS_KEY);
        if (!StringUtils.isEmpty(cacheStr)){
            result = JSON.parseObject(cacheStr);
        }
        return result;
    }

    //设置广播标识信息
    public static void setBarCodeType(String bottomCode){
        JSONObject result = new JSONObject();
        result.put("result", bottomCode);
        SPUtils.getInstance().put(Contents.BROADCAST_CODE_TYPE, result.toJSONString());
    }

    //获取广播标识信息
    public static String getBarCodeType() {
        String result = "";
        String cacheStr = SPUtils.getInstance().getString(Contents.BROADCAST_CODE_TYPE);
        if (!StringUtils.isEmpty(cacheStr)){
            JSONObject map = JSON.parseObject(cacheStr);
            result = map.getString("result");
        }
        return result;
    }
}
