<?php

namespace CouponsPlus\App\Handlers;

use CouponsPlus\App\Components\DashboardDevelopmentScripts;
use CouponsPlus\App\Data\Export\DashboardData;
use CouponsPlus\Original\Characters\StringManager;
use CouponsPlus\Original\Collections\Collection;
use CouponsPlus\Original\Environment\Env;
use CouponsPlus\Original\Events\Handler\EventHandler;

#[\AllowDynamicProperties]
Class DashboardScriptsHandler extends EventHandler
{
    protected $numberOfArguments = 1;
    protected $priority = 10;

    public function execute(string $hook)
    {
        $this->registerGlobalStylesAndScripts();
        if (!in_array($hook, ['post.php', 'post-new.php']) || !(($screen = get_current_screen()) && $screen->post_type === 'shop_coupon')) {
            return;
        }

        wp_enqueue_style(
            Env::getwithPrefix('dashboard-styles'), 
            Env::directoryURI().'/app/scripts/dashboard/styles/build/dashboard.css',
            null,
            $version = Env::settings()->environment === 'development'? time() : '1.1.2'
        );

        /**
         * Only used to force loading production on dev environments. Uncomment to use when on development.
         */
        (boolean) $forceProduction = false/*sanitize_text_field(wp_unslash($_GET['production'] ?? ''))*/;
        (boolean) $loadProduction = Env::settings()->environment === 'production' || ($forceProduction === 'true');
        (string) $dashboardID = Env::getwithPrefix('dashboard');

        if ($loadProduction) {
            (object) $assetsData = static::getAssetsData();
            (object) $mainEntry = $assetsData->get('src/main.tsx');

            // Enqueue main CSS from Vite build
            if ($mainEntry && property_exists($mainEntry, 'css') && !empty($mainEntry->css)) {
                wp_enqueue_style(
                    Env::getwithPrefix('dashboard-main'),
                    Env::directoryURI()."app/scripts/dashboard/dist/{$mainEntry->css[0]}",
                    null,
                    '1.0.0'
                );
            }

            wp_enqueue_script(
                $id = $dashboardID, 
                $source = Env::directoryURI()."app/scripts/dashboard/dist/{$mainEntry->file}",
                $dependencies = array_filter([
                    'jquery', 
                    function_exists('wp_set_script_translations') ? 'wp-i18n' : '',
                ]),
                $version = '1.1.1',
                $inFooter = true
            );

            // Add type="module" attribute for Vite ES module output
            add_filter('script_loader_tag', function($tag, $handle, $src) use ($dashboardID) {
                if ($handle === $dashboardID) {
                    $tag = str_replace(' src=', ' type="module" src=', $tag);
                }
                return $tag;
            }, 10, 3);

            wp_add_inline_script($dashboardID, 'window.lodash = _.noConflict();', 'after');

            if (function_exists('wp_set_script_translations')) {
                wp_set_script_translations(
                    $id, 
                    Env::settings()->app->textDomain,
                    Env::directory() . 'international/'
                );
            }
        } else {
            // ONLY USED FOR THE DEVELOPMENT SCRIPTS, 
            // THIS IS IGNORED ON LIVE SITES
            if (Env::settings()->environment === 'development') {
                (object) $dashboardDevelopmentScripts = new DashboardDevelopmentScripts($dashboardID);

                $dashboardDevelopmentScripts->render();
            }
        }

        (object) $dashboardData = new DashboardData;

        wp_localize_script(
            $dashboardID, 
            'CouponsPlus', 
            $dashboardData->export('CouponsPlus')->asArray()
        );
        

        wp_enqueue_style(
            Env::getwithPrefix('dashboard-font'), 
            'https://fonts.googleapis.com/css2?family=Host+Grotesk:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap',
            //'https://fonts.googleapis.com/css2?family=Epilogue:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap',
            null,
            '1.1.2'
        );
    }

    protected function registerGlobalStylesAndScripts()
    {
        wp_enqueue_style(
            Env::getwithPrefix('global'), 
            Env::directoryURI().'app/styles/dashboard-global.css',
            null,
            '1.0.0'
        );
    }

    public static function getAssetsData() : Collection
    {
        return new Collection((array) json_decode(
            file_get_contents(Env::getAppDirectory('dashboard').'dist/manifest.json')
        ));;
    }
}