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

import com.google.common.collect.Maps;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 标题
 *
 * @author nianchang
 * @ClassName ParseEngine
 * @date 2021/06/17 10:58 上午
 */
@SuppressWarnings("all")
public class ParseEngine1 {

    public static Map<String, Object> parse(Map<String, Object> BaseMap, Map<String, Object> dataMap, String elName) {
        String text = dataMap.get("text") != null ? dataMap.get("text").toString() : "";

        String id = BaseMap.get("id") != null ? BaseMap.get("id").toString() : "";
        Integer marginTop = BaseMap.get("marginTop") != null ? Integer.valueOf(BaseMap.get("marginTop").toString()) : 0;
        Integer marginBottom = BaseMap.get("marginBottom") != null ? Integer.valueOf(BaseMap.get("marginBottom").toString()) : 0;
        String backgroundColor = BaseMap.get("backgroundColor") != null ? BaseMap.get("backgroundColor").toString() : "";
        Map<String, Object> source = buildDataSource(elName, id, marginTop, marginBottom, backgroundColor, text);
        return source;
    }

    private static Map<String, Object> buildDataSource(String elName, String id, Integer marginTop, Integer marginBottom,
                                                String backgroundColor, String text) {
        Map<String, Object> data = new HashMap<>();
        data.put("title", "标题");
        Map<String, Object> styleMap = buildCommonStyleMap(marginTop, marginBottom, backgroundColor);
        data.put("commonStyle", styleMap);
        data.put("isFixed", false);
        data.put("propsValue", Maps.newHashMap());
        data.put("events", buildEvents());
        data.put("elements", buildElements(elName, text));
        data.put("temName", "qm-title");
        data.put("icon", "qimai-biaoti1");
        data.put("id", id);
        data.put("miniStyle", BuildStyle.build(styleMap, elName));
        return data;
    }

    private static Map<String, Object> buildEvents() {
        Map<String, Object> eventsMap = new HashMap<>();
        eventsMap.put("type", "");
        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> 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> buildAliPay() {
        Map<String, Object> alipay = new HashMap<>();
        alipay.put("tntInstId", "");
        alipay.put("scene", "");
        return alipay;
    }

    private static List<Map<String, Object>> buildElements(String elName, String text) {
        List<Map<String, Object>> elements = new ArrayList<>();
        Map<String, Object> data = new HashMap<>();
        data.put("elName", elName);
        Map<String, Object> map = buildCommonStyle();
        data.put("commonStyle", map);
        data.put("propsValue", buildPropsValue1(text));
        String miniStyle = BuildStyle.build(map, elName);
        data.put("miniStyle", miniStyle);
        elements.add(data);
        return elements;
    }

    private static Map<String, Object> buildPropsValue1(String text) {
        Map<String, Object> propsValue = new HashMap<>();
        propsValue.put("text", text);
        return propsValue;
    }

    private static Map<String, Object> buildCommonStyle() {
        Map<String, Object> commonStyle = new HashMap<>();
        commonStyle.put("height", 40);
        commonStyle.put("fontSize", 16);
        commonStyle.put("fontWeight", 600);
        commonStyle.put("lineHeight", 40);
        commonStyle.put("paddingLeft", 10);
        commonStyle.put("paddingRight", 10);
        commonStyle.put("textAlign", "center");
        return commonStyle;
    }

    private static Map<String, Object> buildCommonStyleMap(Integer marginTop, Integer marginBottom,
                                                    String backgroundColor) {
        Map<String, Object> commonStyle = new HashMap<>();
        commonStyle.put("position", "");
        commonStyle.put("height", 40);
        commonStyle.put("top", 0);
        commonStyle.put("left", 0);
        commonStyle.put("paddingTop", 0);
        commonStyle.put("paddingLeft", 0);
        commonStyle.put("paddingRight", 0);
        commonStyle.put("paddingBottom", marginBottom);
        commonStyle.put("marginTop", marginTop);
        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", backgroundColor);
        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;
    }
}
