<?php

namespace Blazemedia\DriveKIntegrator\Render;

class WidgetMarkup {

    public static function getMarkup(string $makeUrlCode, string $selectedModelId, string $modalTemplate = '') : string {

        // If no modalTemplate provided, use default
        if (empty($modalTemplate)) {
            $domain = preg_replace('/^.*?\.(.*?\..*?)$/', '$1', \home_url());
            $modalTemplate = "blazemedia_" . $domain;
        }

        $markup = [
            'desktop' => ' 
                        <script>
                            document.addEventListener("DOMContentLoaded", function() {
                                let modalLoaded = false;
                                
                                window.addEventListener("scroll", function() {
                                    if (window.pageYOffset > 1000 && !modalLoaded) {
                                        modalLoaded = true;
                                        
                                        // Crea il container
                                        const modalContainer = document.createElement("div");
                                        modalContainer.id = "dk-modal-container";
                                        document.body.appendChild(modalContainer);
                                        
                                        // Imposta la base URL
                                        window.widgetDataBaseURL = "https://www.drivek.it";
                                        
                                        // Crea e aggiungi il JSON con i dati
                                        const jsonScript = document.createElement("script");
                                        jsonScript.type = "application/json";
                                        jsonScript.id = "dk-popup-vehicle-specs";
                                        jsonScript.textContent = JSON.stringify({
                                            "makeUrlCode": "'.$makeUrlCode.'",
                                            "selectedModelId": "'.$selectedModelId.'",
                                            "cta": "Scopri il prezzo",
                                            "modalTemplate": "'.$modalTemplate.'"
                                        });
                                        document.body.appendChild(jsonScript);
                                        
                                        // Carica il CSS
                                        const cssLink = document.createElement("link");
                                        cssLink.rel = "stylesheet";
                                        cssLink.crossOrigin = "anonymous";
                                        cssLink.href = "https://cdn.km77.com/drivek-modal.css";
                                        document.head.appendChild(cssLink);
                                        
                                        // Carica lo script principale
                                        const mainScript = document.createElement("script");
                                        mainScript.type = "module";
                                        mainScript.defer = true;
                                        mainScript.crossOrigin = "anonymous";
                                        mainScript.src = "https://cdn.km77.com/drivek-modal.js";
                                        document.body.appendChild(mainScript);
                                    }
                                });
                            });
                        </script>
                  ',
            'mobile' => ''
        ];

        $markup = self::isMobile() ? $markup['desktop'] : $markup['desktop'];

        if ($modalTemplate == "blazemedia_hdmotori.it") {
            $markupHideNewsletterModal = '
                <style>
                    #overlay.visible, #modal.visible {
                        display: none !important;
                        z-index: -1 !important;
                        transform: scale(0) !important;
                    }
                </style>
            ';

            $markup .= $markupHideNewsletterModal;
        }

        return $markup;
    }


    public static function isMobile() : bool {

        return function_exists('\wp_is_mobile') && \wp_is_mobile();
    }

}