package com.engine.organization.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.browser.bean.SearchConditionOption;
import org.apache.commons.lang.StringUtils;

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

/**
 * @author:dxfeng
 * @createTime: 2022/06/13
 * @version: 1.0
 */
public class SelectOptionParam {

    /**
     * 下拉框Json转换
     *
     * @param customValue
     * @return
     */
    public static List<SearchConditionOption> convertJsonToListOption(String customValue) {
        List<SearchConditionOption> selectOptions = new ArrayList<>();
        JSONArray objects = JSONObject.parseArray(customValue);
        if (objects.size() < 3) {
            return selectOptions;
        }
        JSONObject o = (JSONObject) objects.get(2);
        JSONArray datas = o.getJSONArray("datas");
        if (!datas.isEmpty()) {
            int size = datas.size();
            for (int i = 0; i < size; i++) {
                JSONObject jsonObject = (JSONObject) datas.get(i);
                // 只显示未封存的数据
                if (!"1".equals(jsonObject.getString("unuse"))) {
                    SearchConditionOption option = new SearchConditionOption(StringUtils.isEmpty(jsonObject.getString("key")) ? i + "" : jsonObject.getString("key"), jsonObject.getString("option"));
                    selectOptions.add(option);
                    // 设置默认数据
                    if ("1".equals(jsonObject.getString("default"))) {
                        option.setSelected(true);
                    }
                }
            }
        }
        return selectOptions;
    }

    /**
     * 获取自定义浏览按钮标识
     *
     * @param customValue
     * @return
     */
    public static String getCustomBrowserId(String customValue) {
        try {
            JSONArray objects = JSONObject.parseArray(customValue);
            if (null != objects && objects.size() > 2) {
                JSONObject object = (JSONObject) objects.get(2);
                return object.getString("value");
            }
        } catch (Exception e) {

        }
        return customValue;
    }

    /**
     * 获取自定义浏览按钮标识
     *
     * @param customValue
     * @return
     */
    public static String getCustomBrowserValueSpan(String customValue) {
        JSONArray objects = JSONObject.parseArray(customValue);
        if (null != objects && objects.size() > 2) {
            JSONObject object = (JSONObject) objects.get(2);
            return object.getString("valueSpan");
        }
        return customValue;
    }

    /**
     * 获取文本长度
     *
     * @param customValue
     * @return
     */
    public static String getTextLength(String customValue) {
        JSONArray objects = JSONObject.parseArray(customValue);

        if (null != objects && objects.size() > 2) {
            return objects.get(2).toString();
        }
        return customValue;
    }

    /**
     * 获取下拉框信息
     *
     * @param customValue
     * @return
     */
    public static List<Object> getSelectFields(String customValue) {
        List<Object> selectFields = new ArrayList<>();
        JSONArray objects = JSONObject.parseArray(customValue);
        if (objects.size() < 3) {
            return selectFields;
        }
        JSONObject o = (JSONObject) objects.get(2);
        JSONArray datas = o.getJSONArray("datas");
        if (!datas.isEmpty()) {
            int size = datas.size();
            for (int i = 0; i < size; i++) {
                selectFields.add(datas.get(i));
            }
        }
        return selectFields;
    }

    /**
     * 根据ID获取下拉框的展示文本
     * @param customValue
     * @param id
     * @return
     */
    public static String getSelectOption(String customValue,String id) {
        JSONArray objects = JSONObject.parseArray(customValue);
        JSONObject o = (JSONObject) objects.get(2);
        JSONArray datas = o.getJSONArray("datas");
        List<JSONObject> selectFields = new ArrayList<>();
        if (!datas.isEmpty()) {
            for (int i = 0; i < datas.size(); i++) {
                selectFields.add((JSONObject) datas.get(i));
            }
        }
        JSONObject filter = selectFields.stream().filter(item -> id.equals(item.getString("id"))).findFirst().orElse(null);
        if (null == filter) {
            return "";
        }
        return filter.getString("option");
    }


}
