package com.realnet.dlf.Services;

import java.util.Collections;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.realnet.dlf.Entity.Design_lbrary;
import com.realnet.dlf.Repository.Design_lbraryRepository;

@Service
public class DlfService {

	@Autowired
	private Design_lbraryRepository designLibraryRepository;

	public String generateHtmlWithCss(Integer id) {
		Design_lbrary design = designLibraryRepository.findById(id)
				.orElseThrow(() -> new RuntimeException("Design not found with id: " + id));

		String htmlJson = design.getHtmljson();
		String javacode = design.getJavacode();
		String css = design.getCss();

		Map<String, Object> dataMap = parseJsonToMap(htmlJson);

		String generatedHtml = replacePlaceholders(javacode, dataMap);

		String finalHtml = "<html><head><style>" + css + "</style></head><body>" + generatedHtml + "</body></html>";

		return finalHtml;
	}

	private Map<String, Object> parseJsonToMap(String json) {
		try {
			ObjectMapper mapper = new ObjectMapper();
			return mapper.readValue(json, new TypeReference<Map<String, Object>>() {
			});
		} catch (Exception e) {
			e.printStackTrace();
			return Collections.emptyMap();
		}
	}

	private String replacePlaceholders(String template, Map<String, Object> dataMap) {
		if (template == null || dataMap == null) {
			return template;
		}

		String result = template;

		for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
			String placeholder = "{{" + entry.getKey() + "}}";
			String value = entry.getValue() != null ? entry.getValue().toString() : "";
			result = result.replace(placeholder, value);
		}

		return result;
	}

//	public static String buildHtml(Object node) {
//		if (node instanceof Map) {
//			Map<String, Object> map = (Map<String, Object>) node;
//
//			// Handle raw section container
//			if (!map.containsKey("type") && !map.containsKey("tag")) {
//				StringBuilder html = new StringBuilder();
//				for (Map.Entry<String, Object> entry : map.entrySet()) {
//					String sectionName = entry.getKey();
//					Object sectionContent = entry.getValue();
//
//					if ("teamSlider".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderTeamSlider((Map<String, Object>) sectionContent));
//						continue;
//					}
//
//					if ("teamSectionTemplate".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderTeamSectionTemplate((Map<String, Object>) sectionContent));
//						continue;
//					}
//
//					if ("teamSection".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderTeamSection((Map<String, Object>) sectionContent));
//						continue;
//					}
//
//					if ("logoGuidelines".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderLogoGuidelines((Map<String, Object>) sectionContent));
//						continue;
//					}
//
//					if ("logoGrid".equalsIgnoreCase(sectionName) && sectionContent instanceof List) {
//						html.append(renderLogoGrid((List<Map<String, Object>>) sectionContent));
//						continue;
//					}
//
//					if ("brandGuideline".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderBrandGuideline((Map<String, Object>) sectionContent));
//						continue;
//					}
//
//					if ("logoShowcase".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderLogoShowcase((Map<String, Object>) sectionContent));
//						continue;
//					}
//
//					if ("logoCloud".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderLogoCloud((Map<String, Object>) sectionContent));
//						continue;
//					}
//
//					if ("ecommerceFAQs".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderEcommerceFAQs((Map<String, Object>) sectionContent));
//						continue;
//					}
//
//					if ("dashboardLayout".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderDashboardLayout((Map<String, Object>) sectionContent));
//						continue;
//					}
//
//					if ("FAQIllustratedSection".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderFaqIllustrated((Map<String, Object>) sectionContent));
//						continue;
//					}
//
//					if ("FAQSection".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderFaqSection((Map<String, Object>) sectionContent));
//						continue;
//					}
//					if ("FAQTwoColumn".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderFaqTwoColumn((Map<String, Object>) sectionContent));
//						continue;
//					}
//
//					if ("TestimonialLanding".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderTestimonialLanding((Map<String, Object>) sectionContent));
//						continue;
//					}
//
//					if ("ClientTestimonialSection".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderClientTestimonial((Map<String, Object>) sectionContent));
//						continue;
//					}
//					if ("TestimonialSection".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderSimpleTestimonial((Map<String, Object>) sectionContent));
//						continue;
//					}
//
//					if ("SubSection".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderSubSectionCentered((Map<String, Object>) sectionContent));
//						continue;
//					}
//					if ("HeroSection".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						Map<String, Object> heroMap = (Map<String, Object>) sectionContent;
//
//						if ("centered".equalsIgnoreCase(getString(heroMap, "layout", ""))
//								&& heroMap.containsKey("artwork")) {
//							html.append(renderHeroCenteredWithArt(heroMap));
//							continue;
//						}
//						if ("left-text-right-cards".equalsIgnoreCase(getString(heroMap, "layout", ""))) {
//							html.append(renderHeroLeftCards(heroMap));
//							continue;
//						}
//						// Smart internal layout-based routing to correct renderer
//						String layout = getString(heroMap, "layout", "").toLowerCase();
//
//						if (layout.equals("two-column") || heroMap.containsKey("leftColumn")) {
//							html.append(renderTwoColumnHeroSection(heroMap));
//						} else if (layout.equals("centered") || heroMap.containsKey("heading")) {
//							html.append(renderCenteredHeroSection(heroMap));
//						} else {
//							// fallback to older generic renderHeroSection
//							html.append(renderHeroSection(heroMap));
//						}
//						continue;
//					}
//
//					if ("Blog".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderBlogCardSection((Map<String, Object>) sectionContent));
//						continue;
//					}
//
//					if ("blogPage".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderBlogPage((Map<String, Object>) sectionContent));
//						continue;
//					}
//
//					if ("blogPreviewSection".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderBlogPreviewSection((Map<String, Object>) sectionContent));
//						continue;
//					}
//					if ("blogHomePage".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderBlogHomePage((Map<String, Object>) sectionContent));
//						continue;
//					}
//
//					if ("blogListSection".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderBlogListSection((Map<String, Object>) sectionContent));
//						continue;
//					}
//
//					if ("ContactSection".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderContactSection((Map<String, Object>) sectionContent));
//						continue;
//					}
//
//					if ("Navbar".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderNavbarBlock((Map<String, Object>) sectionContent));
//						continue;
//					}
//					if ("Footer".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						Map<String, Object> footerMap = (Map<String, Object>) sectionContent;
//
//						// Smart switch based on keys like 'columns', 'topBar', 'bottom'
//						if (footerMap.containsKey("columns") || footerMap.containsKey("topBar")
//								|| footerMap.containsKey("bottom")) {
//							html.append(renderMdBootstrapFooter(footerMap));
//						} else if (footerMap.containsKey("linkColumns") || footerMap.containsKey("socialIcons")) {
//							html.append(renderCompactFooter(footerMap));
//						} else {
//							html.append(renderFooterBlock(footerMap));
//						}
//						continue;
//					}
//					if ("how-to-blog-post".equalsIgnoreCase(getString(map, "type", ""))) {
//						html.append(renderHowToBlogPost(map));
//						return html.toString();
//					}
//
//					if ("blogThumbnailGrid".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderBlogThumbnailGrid((Map<String, Object>) sectionContent));
//						continue;
//					}
//					if ("blogSection".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderBlogSection((Map<String, Object>) sectionContent));
//						continue;
//					}
//					if ("articlePage".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderArticlePage((Map<String, Object>) sectionContent));
//						continue;
//					}
//					if ("templateLibrary".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderTemplateLibrary((Map<String, Object>) sectionContent));
//						continue;
//					}
//					if ("blogHeader".equalsIgnoreCase(sectionName) && sectionContent instanceof Map) {
//						html.append(renderBlogHeader((Map<String, Object>) sectionContent));
//						continue;
//					}
//
//					// Wrap each section in a <section> tag with class derived from key
//					html.append("<section class=\"").append(sectionName.toLowerCase().replace(" ", "-")).append("\">");
//
//					html.append(buildHtml(sectionContent));
//
//					html.append("</section>");
//				}
//				return html.toString();
//			}
//			// Check if a universal TYPE_RENDERER exists for this block
//			String type = getString(map, "type", "");
//			if (TYPE_RENDERERS.containsKey(type.toLowerCase())) {
//				return TYPE_RENDERERS.get(type.toLowerCase()).apply(map);
//			}
//			// Normal map block
//			return renderFromMap(map);
//		} else if (node instanceof List) {
//			return renderFromList((List<Object>) node);
//		} else if (node != null) {
//			return node.toString();
//		} else {
//			return "";
//		}
//	}

}
