package com.realnet.OpenAi.Services;

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

public class HtmlBuilder5 {

	public static String buildHtml(Object nodeObj) {
		if (!(nodeObj instanceof Map))
			return "";
		Map<String, Object> node = (Map<String, Object>) nodeObj;

		StringBuilder html = new StringBuilder();
		// CASE: {"div": { ... }} or {"section": { ... }}
		if (node.size() == 1 && !node.containsKey("element")) {
			Map.Entry<String, Object> entry = node.entrySet().iterator().next();
			String tag = entry.getKey();
			Object value = entry.getValue();
			if (value instanceof Map) {
				((Map<String, Object>) value).put("element", tag);
				return buildHtml(value);
			}
		}

		// Standard tag name (from 'element' key)
		String tag = getString(node, "element", "div");

		// Start tag
		html.append("<").append(tag);
//		String clazz = getString(node, "class", "");
//		if (!clazz.isEmpty()) {
//			html.append(" class=\"").append(clazz).append("\"");
//		}
//		
//		<div class="newsletter-2-wrapper">
		// Common attributes handling
		List<String> commonAttributes = java.util.Arrays.asList("class", "id", "type", "placeholder", "href", "src",
				"alt", "title", "target", "name", "value", "onclick");

		for (String attr : commonAttributes) {
			appendAttribute(html, node, attr);
		}
		html.append(">");

		// Text content
		String text = getString(node, "text", "");
		if (!text.isEmpty()) {
			html.append(text);
		}

		// Single icon
		Object iconObj = node.get("icon");
		if (iconObj instanceof String) {
			String iconClass = iconObj.toString();
			String emoji = getIconEmoji(iconClass);
			html.append("<i class=\"").append(iconClass).append("\">").append(emoji).append("</i>");
		}

		// Multiple icons
		Object iconsObj = node.get("icons");
		if (iconsObj instanceof List) {
			List<?> icons = (List<?>) iconsObj;
			for (Object icon : icons) {
				if (icon instanceof String) {
					String iconClass = icon.toString();
					String emoji = getIconEmoji(iconClass);
					html.append("<i class=\"").append(iconClass).append("\">").append(emoji).append("</i>");
				}
			}
		}

		// Title
		if (node.containsKey("title")) {
			Object titleObj = node.get("title");
			if (titleObj instanceof Map) {
				// Expected: title is an object with element/class/text
				html.append(buildHtml(titleObj));
			} else {
				// Fallback: use default <h3> if element not defined
				html.append("<h3>").append(titleObj.toString()).append("</h3>");
			}
		}

		// Description
		if (node.containsKey("description")) {
			Object descObj = node.get("description");
			if (descObj instanceof Map) {
				html.append(buildHtml(descObj));
			} else {
				html.append("<p>").append(descObj.toString()).append("</p>");
			}
		}

		// Handle children
		Object children = node.get("children");
		if (children instanceof List) {
			for (Object child : (List<Object>) children) {
				if (child instanceof Map) {
					html.append(buildHtml((Map<String, Object>) child));
				}
			}
		} else if (children instanceof Map) {
			html.append(buildHtml((Map<String, Object>) children));
		}

		// Handle content (like in 'section' root)
		Object content = node.get("content");
		if (content instanceof List) {
			for (Object child : (List<Object>) content) {
				if (child instanceof Map) {
					html.append(buildHtml((Map<String, Object>) child));
				}
			}
		} else if (content instanceof Map) {
			html.append(buildHtml((Map<String, Object>) content));
		}

		// Close tag
		html.append("</").append(tag).append(">");
		return html.toString();
	}

	private static void appendAttribute(StringBuilder html, Map<String, Object> node, String attr) {
		String val = getString(node, attr, "");
		if (!val.isEmpty()) {
			html.append(" ").append(attr).append("=\"").append(val).append("\"");
		}
	}

	private static String getIconEmoji(String label) {
		label = label.toLowerCase();
		if (label.contains("facebook"))
			return "📘";
		if (label.contains("instagram"))
			return "📷";
		if (label.equals("x"))
			return "𝕏";
		if (label.contains("search"))
			return "🔍";
		if (label.contains("profile"))
			return "👤";
		if (label.contains("twitter"))
			return "🐦";

		if (label.contains("linkedin"))
			return "🔗";
		if (label.contains("youtube"))
			return "▶️";
		if (label.contains("fas fa-cube"))
			return "📦";

		if (label.contains("cube"))
			return "📦";
		return "";
	}

	private static String formatKey(String key) {
		key = key.replaceAll("([a-z])([A-Z])", "$1 $2"); // camelCase to space
		key = key.substring(0, 1).toUpperCase() + key.substring(1); // capitalize first
		return key.replace("-", " ");
	}

	private static void appendStyle(StringBuilder style, String key, Object value) {
		if (value != null) {
			style.append(key).append(":").append(value).append("; ");
		}
	}

	private static String getWidgetIcon(String widget) {
		widget = widget.toLowerCase();
		if (widget.contains("search"))
			return "🔍";
		if (widget.contains("recent"))
			return "📝";
		if (widget.contains("newsletter"))
			return "📬";
		return "📦";
	}

	private static String getString(Map<String, Object> map, String key, String defaultValue) {
		Object val = map.get(key);
		return val instanceof String ? (String) val : defaultValue;
	}

	private static String renderImageDiv(Map<String, Object> data) {
		StringBuilder html = new StringBuilder();

		// Get attributes from JSON
//		String className = getString(data, "class", "placeholder-img");
		String className = "placeholder-img";
		String src = getString(data, "src", "/src/assets/images/place.png");
		String width = getString(data, "width", ""); // optional
		String height = getString(data, "height", ""); // optional

		// Fallback to default image if src is missing or blank
		if (src.isEmpty()) {
			src = "/src/assets/images/place.png";
		}

		// Build style
		StringBuilder style = new StringBuilder();
		style.append("background-image: url('").append(src).append("');");

		// Add inline height/width only if provided, else CSS will handle default
		if (!width.isEmpty()) {
			style.append(" width: ").append(width).append(";");
		}
		if (!height.isEmpty()) {
			style.append(" height: ").append(height).append(";");
		}

		// Return final div
		html.append("<div class=\"").append(className).append("\" style=\"").append(style).append("\"></div>");

		return html.toString();
	}

}
