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

import org.apache.commons.lang3.StringUtils;

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

/**
 * @author nianchang
 * @ClassName Build
 * @date 2021/07/12 4:04 下午
 */
@SuppressWarnings("all")
public class BuildStyle {

    private static String RPX = "rpx";

    public static String build(Map<String, Object> styleMap, String elName) {
        Map<String, Object> parse = parse(styleMap, elName);
        StringBuilder sb = new StringBuilder();

        Object position1 = parse.get("position");
        if (Objects.nonNull(position1)) {
            String position = parse.get("position").toString();
            sb.append("position:").append(position).append(";");
        }

        Object width1 = parse.get("width");
        if (Objects.nonNull(width1)) {
            String width = parse.get("width").toString();
            sb.append("width:").append(width).append(";");
        } else {
            sb.append("width:").append("").append(";");
        }

        if (Objects.nonNull(parse.get("height"))) {
            String height = parse.get("height").toString();
            sb.append("height:").append(height).append(";");
        }

        if (Objects.nonNull(parse.get("top"))) {
            String top = parse.get("top").toString();
            sb.append("top:").append(top).append(";");
        }

        if (Objects.nonNull(parse.get("left"))) {
            String left = parse.get("left").toString();
            sb.append("left:").append(left).append(";");
        }

        if (Objects.nonNull(parse.get("right"))) {
            String right = parse.get("right").toString();
            sb.append("right:").append(right).append(";");
        } else {
            sb.append("right:").append(0).append(";");
        }

        String paddingTop = parse.get("paddingTop").toString();
        String paddingRight = parse.get("paddingRight").toString();
        String paddingLeft = parse.get("paddingLeft").toString();
        String paddingBottom = parse.get("paddingBottom").toString();

        if (org.springframework.util.StringUtils.isEmpty(paddingTop)
            && org.springframework.util.StringUtils.isEmpty(paddingRight)
            && org.springframework.util.StringUtils.isEmpty(paddingLeft)
            && org.springframework.util.StringUtils.isEmpty(paddingBottom)) {
            sb.append("padding:").append(0).append(";");
        } else {
            sb.append("padding:").append(paddingTop).append(" ").append(paddingRight).append(" ").append(paddingLeft)
                .append(" ").append(paddingBottom).append(";");
        }

        String marginTop = parse.get("marginTop").toString();
        String marginLeft = parse.get("marginLeft").toString();
        String marginRight = parse.get("marginRight").toString();
        String marginBottom = parse.get("marginBottom").toString();

        if (org.springframework.util.StringUtils.isEmpty(marginTop)
            && org.springframework.util.StringUtils.isEmpty(marginLeft)
            && org.springframework.util.StringUtils.isEmpty(marginRight)
            && org.springframework.util.StringUtils.isEmpty(marginBottom)) {
            sb.append("margin:").append(0).append(";");
        } else {
            sb.append("margin:").append(marginTop).append(" ").append(marginLeft).append(" ").append(marginRight)
                .append(" ").append(marginBottom).append(";");
        }

        if (Objects.nonNull(parse.get("borderRadius"))) {
            String borderRadius = parse.get("borderRadius").toString();
            sb.append("border-radius:").append(borderRadius).append(";");
        }

        if (Objects.nonNull(parse.get("fontSize"))) {
            String fontSize = parse.get("fontSize").toString();
            sb.append("font-size:").append(fontSize).append(";");
        }

        if (Objects.nonNull(parse.get("fontWeight"))) {
            String fontWeight = parse.get("fontWeight").toString();
            sb.append("font-weight:").append(fontWeight).append(";");
        }

        if (Objects.nonNull(parse.get("letterSpacing"))) {
            String letterSpacing = parse.get("letterSpacing").toString();
            sb.append("letter-spacing:").append(letterSpacing).append(";");
        }

        if (Objects.nonNull(parse.get("textAlign"))) {
            String textAlign = parse.get("textAlign").toString();
            sb.append("text-align:").append(textAlign).append(";");
        }

        if (Objects.nonNull(parse.get("color"))) {
            String color = parse.get("color").toString();
            sb.append("color:").append(color).append(";");
        } else {
            sb.append("color:").append("#333").append(";");
        }

        if (Objects.nonNull(parse.get("zIndex"))) {
            String zIndex = parse.get("zIndex").toString();
            sb.append("z-index:").append(zIndex).append(";");
        }

        if (Objects.nonNull(parse.get("overflow"))) {
            String overflow = parse.get("overflow").toString();
            sb.append("overflow:").append(overflow).append(";");
        } else {
            sb.append("overflow:").append("hidden").append(";");
        }

        if (Objects.nonNull(parse.get("opacity"))) {
            sb.append("opacity").append(styleMap.get("opacity")).append(";");
        }

        Object borderWidthObj = styleMap.get("borderWidth");
        if (Objects.nonNull(borderWidthObj)) {
            Integer borderWidth = Integer.valueOf(borderWidthObj.toString());
            if(Objects.equals(0, borderWidth)){
                sb.append("border:").append(borderWidth).append(";");
            }else {
                // todo
                sb.append("border:").append(borderWidth).append(" ").append("borderStyle").append(" ").append("borderColor").append(";");
            }

        }

        if (Objects.nonNull(styleMap.get("backgroundImage"))) {
            String backgroundImage = styleMap.get("backgroundImage").toString();
            if (!org.springframework.util.StringUtils.isEmpty(backgroundImage)) {
                sb.append("background:").append(backgroundImage).append(" ").append(styleMap.get("backgroundRepeat")).append(" ")
                    .append(styleMap.get("backgroundPosition")).append("/").append(styleMap.get("backgroundSize")).append(";");
            }
        }

        if (Objects.nonNull(styleMap.get("backgroundColor"))) {
            String backgroundColor = styleMap.get("backgroundColor").toString();
            if (!org.springframework.util.StringUtils.isEmpty(backgroundColor)) {
                sb.append("background-color:").append(backgroundColor).append(";");
            }
        }

        return sb.toString();
    }
    
    private static Map<String, Object> parse(Map<String, Object> styleMap, String elName) {
        Map<String, Object> map = new HashMap<>();

        String position = "";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("position"))) {
            position = styleMap.get("position").toString();
            if (StringUtils.isNumeric(position)) {
                position = position;
            }
        }
        map.put("position", position);

        String width = "";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("width"))) {
            width = styleMap.get("width").toString();
            if (StringUtils.isNumeric(width)) {
                width = Integer.valueOf(width) * 2 + RPX;
            }
        }
        map.put("width", width);

        String height = "";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("height"))) {
            height = styleMap.get("height").toString();
            if (StringUtils.isNumeric(height)) {
                if (Objects.equals(elName, "qm-banner")) {
                    Integer i = Integer.valueOf(height);
                    height = i + RPX;
                } else {
                    height = Integer.valueOf(height) * 2 + RPX;
                }
            }
        }
        map.put("height", height);

        String top = "";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("top"))) {
            top = styleMap.get("top").toString();
            if (StringUtils.isNumeric(top)) {
                top = Integer.valueOf(top) * 2 + RPX;
            }
        }
        map.put("top", top);

        String left = "";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("left"))) {
            left = styleMap.get("left").toString();
            if (StringUtils.isNumeric(left)) {
                left = Integer.valueOf(left) * 2 + RPX;
            }
        }
        map.put("left", left);

        String right = "0";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("right"))) {
            right = styleMap.get("right").toString();
            if (StringUtils.isNumeric(right)) {
                right = Integer.valueOf(right) * 2 + RPX;
            }
        }
        map.put("right", right);

        String lineHeight = "";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("lineHeight"))) {
            lineHeight = styleMap.get("lineHeight").toString();
            if (StringUtils.isNumeric(lineHeight)) {
                lineHeight = Integer.valueOf(lineHeight) * 2 + RPX;
            }
        }
        map.put("lineHeight", lineHeight);

        String paddingTop = "0";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("paddingTop"))) {
            paddingTop = styleMap.get("paddingTop").toString();
            if (StringUtils.isNumeric(paddingTop)) {
                paddingTop = Integer.valueOf(paddingTop) * 2 + RPX;
            }
        }
        map.put("paddingTop", paddingTop);

        String paddingLeft = "0";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("paddingLeft"))) {
            paddingLeft = styleMap.get("paddingLeft").toString();
            if (StringUtils.isNumeric(paddingLeft)) {
                paddingLeft = Integer.valueOf(paddingLeft) * 2 + RPX;
            }
        }
        map.put("paddingLeft", paddingLeft);

        String paddingRight = "0";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("paddingRight"))) {
            paddingRight = styleMap.get("paddingRight").toString();
            if (StringUtils.isNumeric(paddingRight)) {
                paddingRight = Integer.valueOf(paddingRight) * 2 + RPX;
            }
        }
        map.put("paddingRight", paddingRight);

        String paddingBottom = "0";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("paddingBottom"))) {
            paddingBottom = styleMap.get("paddingBottom").toString();
            if (StringUtils.isNumeric(paddingBottom)) {
                paddingBottom = Integer.valueOf(paddingBottom) * 2 + RPX;
            }
        }
        map.put("paddingBottom", paddingBottom);

        String marginTop = "0";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("marginTop"))) {
            marginTop = styleMap.get("marginTop").toString();
            if (StringUtils.isNumeric(marginTop)) {
                marginTop = Integer.valueOf(marginTop) * 2 + RPX;
            }
        }
        map.put("marginTop", marginTop);

        String marginLeft = "0";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("marginLeft"))) {
            marginLeft = styleMap.get("marginLeft").toString();
            if (StringUtils.isNumeric(marginLeft)) {
                marginLeft = Integer.valueOf(marginLeft) * 2 + RPX;
            }
        }
        map.put("marginLeft", marginLeft);

        String marginRight = "0";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("marginRight"))) {
            marginRight = styleMap.get("marginRight").toString();
            if (StringUtils.isNumeric(marginRight)) {
                marginRight = Integer.valueOf(marginRight) * 2 + RPX;
            }
        }
        map.put("marginRight", marginRight);

        String marginBottom = "0";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("marginBottom"))) {
            marginBottom = styleMap.get("marginBottom").toString();
            if (StringUtils.isNumeric(marginBottom)) {
                marginBottom = Integer.valueOf(marginBottom) * 2 + RPX;
            }
        }
        map.put("marginBottom", marginBottom);

        String borderWidth = "";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("borderWidth"))) {
            borderWidth = styleMap.get("borderWidth").toString();
            if (StringUtils.isNumeric(borderWidth)) {
                borderWidth = Integer.valueOf(borderWidth) * 2 + RPX;
            }
        }
        map.put("borderWidth", borderWidth);

        String borderRadius = "";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("borderRadius"))) {
            borderRadius = styleMap.get("borderRadius").toString();
            if (StringUtils.isNumeric(borderRadius)) {
                borderRadius = Integer.valueOf(borderRadius) * 2 + RPX;
            }
        }
        map.put("borderRadius", borderRadius);

        String fontSize = "";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("fontSize"))) {
            fontSize = styleMap.get("fontSize").toString();
            if (StringUtils.isNumeric(fontSize)) {
                fontSize = Integer.valueOf(fontSize) * 2 + RPX;
            }
        }
        map.put("fontSize", fontSize);

        String fontWeight = "";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("fontWeight"))) {
            fontWeight = styleMap.get("fontWeight").toString();
            if (StringUtils.isNumeric(fontWeight)) {
                fontWeight = fontWeight;
            }
        }
        map.put("fontWeight", fontWeight);

        String letterSpacing = "";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("letterSpacing"))) {
            letterSpacing = styleMap.get("letterSpacing").toString();
            if (StringUtils.isNumeric(letterSpacing)) {
                letterSpacing = Integer.valueOf(letterSpacing) * 2 + RPX;
            }
        }
        map.put("letterSpacing", letterSpacing);

        String textAlign = "";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("textAlign"))) {
            textAlign = styleMap.get("textAlign").toString();
        }
        map.put("textAlign", textAlign);

        String color = "#333";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("color"))) {
            color = styleMap.get("color").toString();
        }
        map.put("color", color);

        String zIndex = "";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("zIndex"))) {
            zIndex = styleMap.get("zIndex").toString();
        }
        map.put("zIndex", zIndex);

        String backgroundImage = "";
        if (!org.springframework.util.StringUtils.isEmpty(styleMap.get("backgroundImage"))) {
            backgroundImage = "url(" + styleMap.get("backgroundImage").toString() + ")";
        }
        map.put("backgroundImage", backgroundImage);
        return map;
    }

}
