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

import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.Map;

/**
 * web容器
 *
 * @author nianchang
 * @ClassName ParseEngine5
 * @date 2021/06/17 10:58 上午
 */
@SuppressWarnings("all")
public class ParseEngine5 {

    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 url = dataMap.get("url") != null ? dataMap.get("url").toString() : "";
        boolean isHaveParams = dataMap.get("isHaveParams") != null ? Boolean.valueOf(dataMap.get("isHaveParams").toString()) : false;

        Map<String, Object> paramMap = JSON.parseObject(dataMap.get("params").toString());
        String memberId = paramMap.get("memberId") != null ? paramMap.get("memberId").toString() : "";
        String phoneNumber = dataMap.get("phoneNumber") != null ? dataMap.get("phoneNumber").toString() : "";
        String memberName = dataMap.get("memberName") != null ? dataMap.get("memberName").toString() : "";

        Map<String, Object> source = buildDataSource(id, elName, url, isHaveParams, memberId, phoneNumber, memberName);
        return source;
    }

    private static Map<String, Object> buildDataSource(String id, String elName, String url, boolean isHaveParams, String memberId,
                                                       String phoneNumber, String memberName) {
        Map<String, Object> data = new HashMap<>();
        data.put("elName", elName);
        data.put("title", "web容器");
        Map<String, Object> map = buildCommonStyleMap();
        data.put("commonStyle", map);
        data.put("isFixed", false);
        data.put("propsValue", buildPropsValue(url, isHaveParams, memberId, phoneNumber, memberName));
        data.put("events", buildEvents());
        data.put("icon", "qimai-juxing");
        data.put("id", id);
        String miniStyle = BuildStyle.build(map, elName);
        data.put("miniStyle", miniStyle);
        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> 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 url, boolean isHaveParams, String memberId,
                                                       String phoneNumber, String memberName) {
        Map<String, Object> propsValue = new HashMap<>();
        propsValue.put("url", url);
        propsValue.put("urlText", url.replace("https://", ""));
        propsValue.put("isHaveParams", isHaveParams);
        propsValue.put("params", buildParams(memberId, phoneNumber, memberName));
        return propsValue;
    }

    private static Map<String, Object> buildParams(String memberId, String phoneNumber, String memberName) {
        Map<String, Object> params = new HashMap<>();
        params.put("memberId", memberId);
        params.put("phoneNumber", phoneNumber);
        params.put("memberName", memberName);
        return params;
    }

    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", 400);
        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", "");
        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;
    }
}
