package com.zm.order.tool.server.engine;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;

import java.util.*;

/**
 * 优惠券
 *
 * @author nianchang
 * @ClassName ParseEngine15
 * @date 2021/06/17 10:59 上午
 */
@SuppressWarnings("all")
public class ParseEngine15 {

    public static Map<String, Object> parse(Map<String, Object> BaseMap, Map<String, Object> dataMap, String elName) {
        String id = BaseMap.get("id") != null ? BaseMap.get("id").toString() : "";
        String marginTop = BaseMap.get("marginTop") != null ? BaseMap.get("marginTop").toString(): "";
        String marginBottom = BaseMap.get("marginBottom") != null ? BaseMap.get("marginBottom").toString() : "";
        boolean isTimeShow = BaseMap.get("isTimeShow") != null ? Boolean.valueOf(BaseMap.get("isTimeShow").toString()) : true;
        List<String> timeRange = BaseMap.get("timeRange") != null ?
                JSON.parseArray(BaseMap.get("timeRange").toString(), String.class) : Lists.newArrayList();

        String head_text = dataMap.get("text") != null ? dataMap.get("text").toString() : "";
        String list_type = dataMap.get("list_type") != null ? dataMap.get("list_type").toString() : "";

        List<Map> dataList = new ArrayList<>();
        if (Objects.nonNull(dataMap.get("data"))) {
            List<Map> hotList1 = JSON.parseArray(dataMap.get("data").toString(), Map.class);
            dataList.addAll(hotList1);
        }

        Map<String, Object> source = buildDataSource(elName, id, marginTop, marginBottom, isTimeShow, head_text,
                list_type, dataList, timeRange);
        return source;
    }

    private static Map<String, Object> buildDataSource(String elName, String id, String marginTop, String marginBottom,
                                                       boolean isTimeShow, String head_text, String list_type,
                                                       List<Map> dataList, List<String> timeRange) {
        Map<String, Object> data = new HashMap<>();
        data.put("elName", elName);
        data.put("title", "虚拟导航");
        data.put("commonStyle", buildCommonStyleMap(marginTop, marginBottom));
        data.put("propsValue", buildPropsValue(isTimeShow, head_text, list_type, dataList, timeRange));
        data.put("mkName", "qm-cash-coupon");
        data.put("icon", "qimai-daohang2");
        data.put("id", id);
        return data;
    }

    private static Map<String, Object> buildPropsValue(boolean isTimeShow, String head_text, String list_type,
                                                       List<Map> dataList, List<String> timeRange) {
        Map<String, Object> propsValue = new HashMap<>();
        propsValue.put("isTimeShow", isTimeShow);
        propsValue.put("timeRange", JSON.toJSONString(timeRange));
        propsValue.put("head_text", head_text);
        propsValue.put("list_type", list_type);
        propsValue.put("data_list", Lists.newArrayList());
        return propsValue;
    }

    private static Map<String, Object> buildCommonStyleMap(String marginTop, String marginBottom) {
        Map<String, Object> commonStyle = new HashMap<>();
        commonStyle.put("position", "relative");
        commonStyle.put("height", "auto");
        commonStyle.put("top", 0);
        commonStyle.put("left", 0);
        commonStyle.put("paddingTop", 0);
        commonStyle.put("paddingLeft", 0);
        commonStyle.put("paddingRight", 0);
        commonStyle.put("paddingBottom", 0);
        commonStyle.put("marginTop", marginTop);
        commonStyle.put("marginLeft", 0);
        commonStyle.put("marginRight", 0);
        commonStyle.put("marginBottom", marginBottom);
        commonStyle.put("borderWidth", 0);
        commonStyle.put("borderColor", "transparent");
        commonStyle.put("borderStyle", "solid");
        commonStyle.put("borderRadius", 0);
        commonStyle.put("boxShadow", "");
        commonStyle.put("fontSize", 14);
        commonStyle.put("fontWeight", 400);
        commonStyle.put("lineHeight", 24);
        commonStyle.put("letterSpacing", 0);
        commonStyle.put("textAlign", "left");
        commonStyle.put("color", "#333");
        commonStyle.put("backgroundColor", "#fff");
        commonStyle.put("backgroundImage", "");
        commonStyle.put("backgroundSize", "contain");
        commonStyle.put("backgroundRepeat", "no-repeat");
        commonStyle.put("backgroundPosition", "center");
        commonStyle.put("opacity", 1);
        commonStyle.put("rotate", 0);
        commonStyle.put("zIndex", 1);
        return commonStyle;
    }
}
