<?php
/**
 * @package edoobox
 */

namespace Edoobox\Base;

use Edoobox\Api\ShortcodeApi;
use Edoobox\Helpers\Arr;
use Edoobox\Plugin;
use Edoobox\Requests\Template;

class Enqueue extends BaseController {

    public function register() {
        global $post;
        // Enqueue scripts that only are available in the admin panel.
        add_action('admin_enqueue_scripts', [$this, 'enqueueAdmin']);
        // Enqueue scripts that only are available on the website.
        add_action('wp_footer', [$this, 'enqueueGuest']);
    }

    /**
     * Enqueue all scripts and styles that are required by the
     * plugin's admin panel.
     *
     * @return void
     */
    public function enqueueAdmin() {
        if ( Auth::isAdmin() && Auth::isEdooboxPage() ) {
            wp_enqueue_script('moment.js', $this->pluginURL . 'js/libraries/moment/moment.min.all.js', ['jquery'], $this->enqueueVersion, true);
            wp_enqueue_script('ajax-script', $this->pluginURL . 'js/dist/admin.js', ['jquery'], $this->enqueueVersion, true);
            wp_enqueue_style("backend-styles", $this->pluginURL . 'css/edoobox-backend.css', [], $this->enqueueVersion, 'all');
            wp_localize_script('ajax-script', 'ajax_object', ['ajax_url' => admin_url('admin-ajax.php')]);
        }
    }

    /**
     * Enqueue all scripts and styles that are required by the
     * wordpress website.
     *
     * @return void
     */
    public function enqueueGuest() {
        global $post;

        if ( ! is_singular() || ! ShortcodeApi::hasShortcode($post) ) {
            return;
        }

        if ( Auth::isGuest() || Auth::isEdooboxPage() ) {
            $attributes = ShortcodeApi::getAttributes($post);
            $template = new Template($attributes);
            $queryLanguage = Arr::get($_GET, 'edlanguage', 'de');
            $language = $queryLanguage ?? Arr::get($attributes, 'language', 'de');
            $apiData = $template->fetch();
            $bootstrapVersion = Bootstrap::getVersion($apiData);
            Plugin::setBootstrapVersion($bootstrapVersion);

            wp_enqueue_script('languages', $this->pluginURL . "js/dist/languages/$language.js", ['jquery'], $this->enqueueVersion, true);
            wp_enqueue_style('fullcalendar-custom', $this->pluginURL . 'js/libraries/fullcalendar/main.css', [], $this->enqueueVersion, 'all');
            wp_enqueue_script('stacktable', $this->pluginURL . 'js/libraries/stacktable/stacktable.js', ['jquery'], $this->enqueueVersion, true);
            wp_enqueue_style('frontend-styles', $this->pluginURL . 'css/edoobox-frontend.css', [], $this->enqueueVersion, 'all');

            if ( $bootstrapVersion >= 5 ) {
                wp_enqueue_style('bootstrap-icons', $this->pluginURL . 'js/libraries/bootstrap/bootstrap_icon_1.10/bootstrap-icons.min.css', [], $this->enqueueVersion, 'all');
                wp_enqueue_script('bootstrap', $this->pluginURL . 'js/libraries/bootstrap/bootstrap.5.3.bundle.min.js', ['jquery'], $this->enqueueVersion, true);
                wp_enqueue_script('moment', $this->pluginURL . 'js/libraries/moment/moment.min.all.2.js', ['jquery'], $this->enqueueVersion, true);
                wp_enqueue_script('fullcalendar', $this->pluginURL . 'js/libraries/fullcalendar/fullcalendar_3.9.0.js', ['jquery'], $this->enqueueVersion, true);
                wp_enqueue_script('languageFix', $this->pluginURL . 'js/languageFix.js', ['jquery'], $this->enqueueVersion, true);
               $this->addGlobalVariables();
                wp_enqueue_script('edoobox-plugin', $this->pluginURL . 'js/main-bootstrap-5.js', ['jquery'], $this->enqueueVersion, true); // <-- main bootstrap 5
                $this->addGlobalVariables();
                wp_enqueue_style('frontend-styles-1', $this->pluginURL . 'css/edoobox.iframe-v2.0.css', [], $this->enqueueVersion, 'all');
            } else {
                // Bootstrap 3.4
                wp_enqueue_script('bootstrap.js', $this->pluginURL . 'js/libraries/bootstrap/bootstrap.min.js', ['jquery'], $this->enqueueVersion);
                $this->addGlobalVariables();
                wp_enqueue_script('edoobox-plugin', $this->pluginURL . 'js/dist/main.js', ['jquery'], $this->enqueueVersion, true); // <-- main bootstrap 3
                $this->addGlobalVariables();
                wp_enqueue_style('frontend-styles-1', $this->pluginURL . 'css/edoobox.iframe-v2.0.css', [], $this->enqueueVersion, 'all');
            }

            $this->enqueueCompatibilityScripts();
        }
    }

    private function enqueueCompatibilityScripts() {
        if ( PluginCollection::drawAttentionIsInstalled() ) {
            wp_enqueue_script('draw-attention-leaflet', $this->pluginURL . 'js/libraries/leaflet/leaflet@1.9.3.js');
            wp_enqueue_script('draw-attention-compatibility', $this->pluginURL . 'js/dist/compatibility.js', ['jquery'], $this->enqueueVersion, true);
        }
    }

    private function addGlobalVariables() {
        // note: used jquery as handle, since edoobox-plugin doesn't work in some cases
        wp_localize_script('jquery', 'placeholderImageURL', [$this->placeholderImageURL]);
        wp_localize_script('edoobox-plugin', 'placeholderImageURL', [$this->placeholderImageURL]);
        wp_localize_script('jquery', 'googleMapsApiKey', [get_option('maps_google_api_key', '')]);
        wp_localize_script('edoobox-plugin', 'googleMapsApiKey', [get_option('maps_google_api_key', '')]);
    }

}
