package com.realnet.OpenAi.Services;

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

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import com.realnet.OpenAi.Renders.NavbarRenderer;

@Service
public class HtmlGenerTorService {

	public String generateHtmlFromJson(String jsonBody) {
		JSONObject contentJson = new JSONObject(jsonBody);
		StringBuilder htmlBuilder = new StringBuilder();
		generateHtml(contentJson, htmlBuilder);
		return htmlBuilder.toString();
	}

	// Java 8 style static CSS map
	private static final Map<String, Map<String, String>> CSS = new HashMap<>();

	static {
		CSS.put("header-1", createStyle(new String[][] { { "display", "flex" }, { "justifyContent", "space-between" },
				{ "alignItems", "center" }, { "padding", "20px 40px" } }));
		CSS.put("logo-1", createStyle(new String[][] { { "fontFamily", "cursive" }, { "fontSize", "20px" } }));
		CSS.put("nav-1", createStyle(new String[][] { { "display", "flex" }, { "gap", "20px" } }));
		CSS.put("button-join", createStyle(new String[][] { { "background", "black" }, { "color", "white" },
				{ "padding", "8px 16px" }, { "border", "none" }, { "borderRadius", "4px" } }));
		CSS.put("hero-1", createStyle(new String[][] { { "textAlign", "center" }, { "padding", "60px 20px" } }));
		CSS.put("hero-heading", createStyle(new String[][] { { "fontSize", "36px" }, { "fontWeight", "bold" } }));
		CSS.put("hero-paragraph", createStyle(new String[][] { { "margin", "20px auto" }, { "maxWidth", "600px" } }));
		CSS.put("hero-buttons", createStyle(new String[][] { { "marginTop", "20px" } }));
		CSS.put("button-signup", createStyle(new String[][] { { "padding", "10px 20px" }, { "background", "black" },
				{ "color", "white" }, { "marginRight", "10px" }, { "border", "none" } }));
		CSS.put("button-learn", createStyle(new String[][] { { "padding", "10px 20px" }, { "background", "white" },
				{ "border", "1px solid #ccc" } }));
		CSS.put("image-grid-1",
				createStyle(new String[][] { { "display", "grid" },
						{ "gridTemplateColumns", "repeat(auto-fill, minmax(200px, 1fr))" }, { "gap", "20px" },
						{ "padding", "40px" } }));
		CSS.put("tile", createStyle(new String[][] { { "background", "#eee" }, { "height", "150px" },
				{ "display", "flex" }, { "alignItems", "center" }, { "justifyContent", "center" } }));
	}

	private static Map<String, String> createStyle(String[][] pairs) {
		Map<String, String> map = new HashMap<>();
		for (String[] pair : pairs) {
			map.put(pair[0], pair[1]);
		}
		return map;
	}

	private void generateHtml(JSONObject node, StringBuilder html) {
		String type = node.optString("type");
		String ref = node.optString("ref");
		String tag = mapTypeToTag(type);

		if (tag != null) {
			html.append("<").append(tag);

			if (!ref.isEmpty() && CSS.containsKey(ref)) {
				html.append(" style=\\").append(convertStyle(CSS.get(ref))).append("\\\"");
			}

			html.append(">");
		}

		if (node.has("content")) {
			html.append(node.getString("content"));
		}

		if (node.has("children")) {
			JSONArray children = node.getJSONArray("children");
			for (int i = 0; i < children.length(); i++) {
				generateHtml(children.getJSONObject(i), html);
			}
		}

		if (tag != null) {
			html.append("</").append(tag).append(">");
		}
	}

	private String mapTypeToTag(String type) {
		if ("page".equals(type))
			return null;
		if ("header".equals(type))
			return "header";
		if ("logo".equals(type) || "paragraph".equals(type))
			return "div";
		if ("nav".equals(type))
			return "nav";
		if ("link".equals(type))
			return "a";
		if ("button".equals(type))
			return "button";
		if ("button-group".equals(type))
			return "div";
		if ("hero-section".equals(type))
			return "section";
		if ("heading".equals(type))
			return "h1";
		if ("image-grid".equals(type))
			return "section";
		if ("image-tile".equals(type))
			return "div";
		return "div";
	}

	private String convertStyle(Map<String, String> styleMap) {
		StringBuilder css = new StringBuilder();
		for (Map.Entry<String, String> entry : styleMap.entrySet()) {
			css.append(camelToKebab(entry.getKey())).append(": ").append(entry.getValue()).append("; ");
		}
		return css.toString().trim();
	}

	private String camelToKebab(String str) {
		return str.replaceAll("([a-z])([A-Z]+)", "$1-$2").toLowerCase();
	}

}
