package com.realnet.OpenAi.Services;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class HtmlBuilder {

//	public static String buildHtml(Object node) {
//		if (node instanceof Map) {
//			return renderFromMap((Map<String, Object>) node);
//		} else if (node instanceof List) {
//			return renderFromList((List<Object>) node);
//		} else {
//			return String.valueOf(node);
//		}
//	}
//
//	private static String renderFromMap(Map<String, Object> node) {
//		StringBuilder html = new StringBuilder();
//
//		String tag = (String) node.getOrDefault("tag", "div");
//		String content = (String) node.getOrDefault("content", "");
//		String className = (String) node.getOrDefault("class", "");
//		String id = (String) node.getOrDefault("id", "");
//		Object children = node.get("children");
//
//		html.append("<").append(tag);
//		if (!className.isEmpty())
//			html.append(" class=\"").append(className).append("\"");
//		if (!id.isEmpty())
//			html.append(" id=\"").append(id).append("\"");
//		html.append(">");
//
//		if (!content.isEmpty())
//			html.append(content);
//
//		if (children != null) {
//			html.append(buildHtml(children));
//		}
//
//		html.append("</").append(tag).append(">");
//		return html.toString();
//	}
//
//	private static String renderFromList(List<Object> list) {
//		return list.stream().map(HtmlBuilder::buildHtml).collect(Collectors.joining());
//	}
//	
	
	
//	part 2
	
	
	

	    public static String buildHtml(Object node) {
	        if (node instanceof Map) {
	            return renderFromMap((Map<String, Object>) node);
	        } else if (node instanceof List) {
	            return renderFromList((List<Object>) node);
	        } else if (node != null) {
	            return String.valueOf(node);
	        } else {
	            return "";
	        }
	    }

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

//	        String tag = getString(node, "tag", "div");
	        String tag = getString(node, "tag", getString(node, "type", "div"));

	        String className = getString(node, "class", "");
	        String id = getString(node, "id", "");
	        String content = getString(node, "content", "");

	        // Start opening tag
	        html.append("<").append(tag);

	        // Attributes
	        if (!className.isEmpty()) html.append(" class=\"").append(className).append("\"");
	        if (!id.isEmpty()) html.append(" id=\"").append(id).append("\"");

	        // Add any custom attributes
	        for (Map.Entry<String, Object> entry : node.entrySet()) {
	            String key = entry.getKey();
	            if (!key.equals("tag") && !key.equals("class") && !key.equals("id") &&
	                !key.equals("content") && !key.equals("children")) {
	                html.append(" ").append(key).append("=\"").append(entry.getValue()).append("\"");
	            }
	        }

	        html.append(">");

	        // Add content
	        if (!content.isEmpty()) {
	            html.append(content);
	        }

	        // Children (recursive)
	        if (node.containsKey("children")) {
	            Object children = node.get("children");
	            html.append(buildHtml(children));
	        }

	        // Closing tag
	        html.append("</").append(tag).append(">");

	        return html.toString();
	    }

	    private static String renderFromList(List<Object> list) {
	        return list.stream().map(HtmlBuilder::buildHtml).collect(Collectors.joining());
	    }

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

