(function() {
    function chatBot() {
        var containerDiv = document.createElement('div');
        document.body.appendChild(containerDiv);

        var shadowRoot = containerDiv.attachShadow({ mode: 'open' });
        var container = document.createElement('div');

        var iframe = document.createElement('iframe');

        iframe.src = 'https://bot.myessaywriter.net/v1/widget/1';
        iframe.title = 'chat support'; 
        iframe.width = '150px'; 
        iframe.height = '150px';
        iframe.style.position = 'fixed';
        iframe.style.bottom = '0';
        iframe.style.right = '0';
        iframe.style.border = 'none';
        iframe.style.zIndex = '9999999';


        container.appendChild(iframe);
        shadowRoot.appendChild(container);
        shadowRoot.host.setAttribute('developed-by', 'Gaude');

        window.addEventListener('message', function (event) {
            if (event.origin !== 'https://bot.myessaywriter.net') {
                return;
            }

            var data = event.data;
            console.log('Received message from iframe:', data);

            if (data && data.action) {

                switch (data.action) {
                    case 'chatbox_opened':
                        if (window.innerWidth <= 600) { // Adjust the breakpoint as needed
                            iframe.width = '100%';
                        } else {
                            iframe.width = '600px';
                        }

                        iframe.height = '100%';
                        break;

                    case 'chatbox_closed':
                        iframe.width = '150px'; 
                        iframe.height = '150px';
                        break;

                    case 'redirect':
                        this.window.location.href = data.link;
                        break;
		    case 'livechat':
                        iframe.style.display = 'none'; 
                        window.Tawk_API.start({
          showWidget : true
       });
window.Tawk_API.maximize();
document.querySelectorAll('iframe').forEach(iframe => {
  iframe.style.display = "block";
});

                        break;


                    case 'scroll_to':
                        if (data.id.startsWith("##")) {
                            // Special case: Scroll to the top
                            $("html, body").animate({ scrollTop: 0 }, "slow");
                        } else if (data.id.includes("https://")) {
                            // Check if it's a URL
                            var urlParts = data.id.split("#");
                            var urlWithoutHash = urlParts[0];
                            var currentUrlWithoutHash = window.location.href.split("#")[0];
                            var currentUrlWithoutSlash = currentUrlWithoutHash.replace(/\/$/, ''); // Remove trailing slash
                    
                            if (urlWithoutHash === currentUrlWithoutSlash) {
                                // URL matches, scroll to the specified element by ID
                                var targetId = urlParts[1].replace("#", ""); // Remove the "#" if present
                                var targetElement = $("#" + targetId);
                    
                                if (targetElement.length) {
                                    $("html, body").animate({ scrollTop: targetElement.offset().top }, "slow");
                                } else {
                                    console.error("Element with ID '" + targetId + "' not found.");
                                }
                            } else {
                                // URL doesn't match, redirect
                                window.location.href = data.id;
                            }
                        } else {
                            // Scroll to the specified element by ID
                            var targetId = data.id.replace("#", ""); // Remove the "#" if present
                            var targetElement = $("#" + targetId);
                    
                            if (targetElement.length) {
                                $("html, body").animate({ scrollTop: targetElement.offset().top }, "slow");
                            } else {
                                console.error("Element with ID '" + targetId + "' not found.");
                            }
                        }
                        break;
                        
                        
                        

                    case 'reload':
                        location.reload(true);
                        break;

                    default:
                        break;
                }
            }
        }, false);
    }

    document.addEventListener('DOMContentLoaded', chatBot);
})();

window.onload = function() {

    window.Tawk_API.endChat();
    window.Tawk_API.hideWidget();
};


window.addEventListener("load", function() {
  const iframes = document.querySelectorAll("iframe");
  iframes.forEach(iframe => {
    iframe.style.display = "none";
  });
});