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

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.zm.order.tool.server.constants.ImageEnum;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 虚拟导航
 *
 * @author nianchang
 * @ClassName ParseEngine14
 * @date 2021/06/17 10:59 上午
 */
@SuppressWarnings("all")
public class ParseEngine14 {

    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 virtualHeight = BaseMap.get("virtualHeight") != null ? BaseMap.get("virtualHeight").toString(): "";
        String size_type = BaseMap.get("size_type") != null ? BaseMap.get("size_type").toString() : "";
        String virtualNavBgColor = BaseMap.get("virtualNavBgColor") != null ? BaseMap.get("virtualNavBgColor").toString() : "";

        String img = dataMap.get("img") != null ? dataMap.get("img").toString() : "";

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

        Map<String, Object> source = buildDataSource(elName, id, virtualHeight, size_type, virtualNavBgColor, img, hotList);
        return source;
    }

    private static Map<String, Object> buildDataSource(String elName, String id, String virtualHeight, String size_type,
                                                       String virtualNavBgColor, String img, List<Map> hotList) {
        Map<String, Object> data = new HashMap<>();
        data.put("elName", elName);
        data.put("title", "虚拟导航");
        data.put("commonStyle", buildCommonStyleMap());
        data.put("propsValue", buildPropsValue(virtualHeight, size_type, virtualNavBgColor, img, hotList));
        data.put("events", buildEvents());
        data.put("icon", "qimai-daohang2");
        data.put("id", id);
        return data;
    }

    private static Map<String, Object> buildEvents() {
        Map<String, Object> eventsMap = new HashMap<>();
        eventsMap.put("type", "5");
        eventsMap.put("page", buildPage());
        eventsMap.put("phone", "");
        eventsMap.put("audioUrl", "");
        eventsMap.put("alipay", buildAliPay());
        eventsMap.put("map", buildMap());
        return eventsMap;
    }

    private static Map<String, Object> buildMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("lng", "");
        map.put("lat", "");
        map.put("name", "");
        map.put("address", "");
        return map;
    }

    private static Map<String, Object> buildAliPay() {
        Map<String, Object> alipay = new HashMap<>();
        alipay.put("tntInstId", "");
        alipay.put("scene", "");
        return alipay;
    }

    private static Map<String, Object> buildPage() {
        Map<String, Object> page = new HashMap<>();
        page.put("link", "");
        page.put("linkType", "");
        page.put("linkText", "");
        page.put("extraData", new HashMap<>());
        return page;
    }

    private static Map<String, Object> buildPropsValue(String virtualHeight, String size_type,
                                                       String virtualNavBgColor, String img, List<Map> hotList) {
        Map<String, Object> propsValue = new HashMap<>();
        propsValue.put("virtualHeight", virtualHeight);
        propsValue.put("size_type", size_type);
        propsValue.put("virtualNavBgColor", virtualNavBgColor);
        propsValue.put("img", img);
        propsValue.put("hotList", buildHotList(hotList));
        return propsValue;
    }

    private static List<Map<String, Object>> buildHotList(List<Map> hotList) {
        List<Map<String, Object>> newHotList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(hotList)) {
            for (Map<String, Object> map : hotList) {
                Map<String, Object> dataMap = new HashMap<>();
                String link = Objects.nonNull(map.get("link")) ? map.get("link").toString() : "";
                String linkText = Objects.nonNull(map.get("link_text")) ? map.get("link_text").toString() : "";
                ImageEnum imageEnum = ImageEnum.getValue(link);
                if (imageEnum != null) {
                    link = imageEnum.getNewValue();
                    linkText = imageEnum.getDesc();
                }
                dataMap.put("width", Objects.nonNull(map.get("width")) ? map.get("width") : 0);
                dataMap.put("height", Objects.nonNull(map.get("height")) ? map.get("height") : 0);
                dataMap.put("left", Objects.nonNull(map.get("left")) ? map.get("left") : 0);
                dataMap.put("top", Objects.nonNull(map.get("top")) ? map.get("top") : 0);
                dataMap.put("borderRadius", Objects.nonNull(map.get("borderRadius")) ? map.get("borderRadius") : 0);
                dataMap.put("link", link);
                dataMap.put("linkType", Objects.nonNull(map.get("linkType")) ? map.get("linkType") : "");
                dataMap.put("linkText", linkText);
                dataMap.put("extraData", Maps.newHashMap());
                newHotList.add(dataMap);
            }
        }
        return newHotList;
    }

    private static Map<String, Object> buildPoints(String activeColor, Double inactiveOpacity, Integer bottomDistance) {
        Map<String, Object> points = new HashMap<>();
        points.put("bottomDistance", bottomDistance);
        points.put("inactiveOpacity", inactiveOpacity);
        points.put("activeColor", activeColor);
        return points;
    }

    private static Map<String, Object> buildCommonStyleMap() {
        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", 0);
        commonStyle.put("marginLeft", 0);
        commonStyle.put("marginRight", 0);
        commonStyle.put("marginBottom", 0);
        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;
    }
}
