<?php
/**
 * Free Image Tools module.
 *
 * @package Noesia_Smart_Waiter
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( ! class_exists( 'Noesia_Swa_Module_Image_Tools' ) ) {
    class Noesia_Swa_Module_Image_Tools implements Noesia_SWA_Module_Interface {

        public function get_id() {
            return 'image-tools';
        }

        public function get_label() {
            return __( 'Image Tools', 'noesia-smart-waiter' );
        }

        public function get_tier() {
            return 'free';
        }

        public function is_enabled() {
            $enabled = true;
            $enabled = apply_filters( 'noesia_swa_module_image_tools_enabled', $enabled, $this );
            return (bool) $enabled;
        }

        public function register_hooks() {
            add_action( 'noesia_swa_register_admin_sections', array( $this, 'register_admin_section' ) );
            add_filter( 'noesia_swa_admin_i18n', array( $this, 'filter_admin_i18n' ), 10, 2 );

            do_action( 'noesia_swa_module_image_tools_loaded', $this );
        }

        public function register_admin_section( $registry ) {
            if ( ! $registry instanceof Noesia_SWA_Admin_Sections ) {
                return;
            }

            $registry->register_section(
                array(
                    'id'          => 'image-tools',
                    'label'       => $this->get_label(),
                    'nav_label'   => __( 'Image Tools', 'noesia-smart-waiter' ),
                    'nav_sub'     => __( 'Compress & Design', 'noesia-smart-waiter' ),
                    'icon'        => 'dashicons-format-image',
                    'priority'    => 59,
                    'tier'        => 'free',
                    'owner'       => 'image-tools',
                    'is_visible'  => true,
                    'nav_visible' => true,
                    'callback'    => array( $this, 'render_admin' ),
                )
            );
        }

        public function filter_admin_i18n( $i18n, $admin ) {
            if ( ! is_array( $i18n ) ) {
                $i18n = array();
            }

            $strings = array(
                'image_tools_drop_active'      => __( 'Drop images to add them to the queue.', 'noesia-smart-waiter' ),
                'image_tools_drop_idle'        => __( 'Drag PNG, JPG, WEBP, or AVIF files here, or browse from your device.', 'noesia-smart-waiter' ),
                'image_tools_processing'       => __( 'Processing…', 'noesia-smart-waiter' ),
                'image_tools_ready'            => __( 'Ready', 'noesia-smart-waiter' ),
                'image_tools_failed'           => __( 'Unable to process this image in the current browser.', 'noesia-smart-waiter' ),
                'image_tools_empty'            => __( 'Add one or more images to begin.', 'noesia-smart-waiter' ),
                'image_tools_invalid'          => __( 'Only image files are supported here.', 'noesia-smart-waiter' ),
                'image_tools_unsupported_avif' => __( 'AVIF export is not supported in this browser.', 'noesia-smart-waiter' ),
                'image_tools_browser_only'     => __( 'Local browser mode. Files stay on this device.', 'noesia-smart-waiter' ),

                'image_design_ready'          => __( 'Image Design is ready.', 'noesia-smart-waiter' ),
                'image_design_clipboard'      => __( 'Selection copied into the design clipboard.', 'noesia-smart-waiter' ),
                'image_design_no_selection'   => __( 'Select a layer or create a marquee selection first.', 'noesia-smart-waiter' ),
                'image_design_no_clipboard'   => __( 'Nothing is available in the editor clipboard yet.', 'noesia-smart-waiter' ),
            );

            return array_merge( $i18n, $strings );
        }


        /**
         * Enqueue Image Tools admin-local assets through the WordPress script registry.
         *
         * @param string $hook Current admin page hook.
         * @return void
         */
        public function enqueue_admin_assets( $hook ) {
            if ( 'toplevel_page_noesia_swa_settings' !== (string) $hook ) {
                return;
            }

            $assets_url = trailingslashit( NOESIA_SWA_PLUGIN_URL . 'modules/free/image-tools/assets/' );
            $assets_dir = trailingslashit( __DIR__ . '/assets' );
            $vendor_url = trailingslashit( $assets_url . 'vendor' );
            $vendor_dir = trailingslashit( $assets_dir . 'vendor' );

            $this->register_vendor_script( 'noesia-swa-image-tools-jszip', $vendor_url . 'jszip.min.js', $vendor_dir . 'jszip.min.js' );
           $this->register_vendor_script( 'noesia-swa-image-tools-pdf', $vendor_url . 'noesia-pdfjs-bridge.js', $vendor_dir . 'noesia-pdfjs-bridge.js' );
            $this->register_vendor_script( 'noesia-swa-image-tools-heic2any', $vendor_url . 'heic2any.min.js', $vendor_dir . 'heic2any.min.js' );
            $this->register_vendor_script( 'noesia-swa-image-tools-xlsx', $vendor_url . 'xlsx.full.min.js', $vendor_dir . 'xlsx.full.min.js' );
            $this->register_vendor_script( 'noesia-swa-image-tools-jspdf', $vendor_url . 'jspdf.umd.min.js', $vendor_dir . 'jspdf.umd.min.js' );
            $this->register_vendor_script( 'noesia-swa-image-tools-dompurify', $vendor_url . 'purify.min.js', $vendor_dir . 'purify.min.js' );
            $this->register_vendor_script( 'noesia-swa-image-tools-wmfjs', $vendor_url . 'WMFJS.bundle.min.js', $vendor_dir . 'WMFJS.bundle.min.js' );
            $this->register_vendor_script( 'noesia-swa-image-tools-emfjs', $vendor_url . 'EMFJS.bundle.min.js', $vendor_dir . 'EMFJS.bundle.min.js' );
            $this->register_vendor_script(
                'noesia-swa-image-tools-rtfjs',
                $vendor_url . 'RTFJS.bundle.min.js',
                $vendor_dir . 'RTFJS.bundle.min.js',
                array( 'noesia-swa-image-tools-wmfjs', 'noesia-swa-image-tools-emfjs' )
            );
            $this->register_vendor_script( 'noesia-swa-image-tools-pako', $vendor_url . 'pako.min.js', $vendor_dir . 'pako.min.js' );
            $this->register_vendor_script(
                'noesia-swa-image-tools-utif',
                $vendor_url . 'UTIF.js',
                $vendor_dir . 'UTIF.js',
                array( 'noesia-swa-image-tools-pako' )
            );
            $this->register_vendor_script( 'noesia-swa-image-tools-gifuct', $vendor_url . 'gifuct-noesia.bundle.js', $vendor_dir . 'gifuct-noesia.bundle.js' );
            $this->register_vendor_script( 'noesia-swa-image-tools-upng', $vendor_url . 'UPNG.js', $vendor_dir . 'UPNG.js' );
            $this->register_vendor_script(
                'noesia-swa-image-tools-independent-encoders',
                $vendor_url . 'noesia-independent-encoders.js',
                $vendor_dir . 'noesia-independent-encoders.js'
            );
            if ( function_exists( 'wp_script_add_data' ) ) {
                wp_script_add_data( 'noesia-swa-image-tools-independent-encoders', 'type', 'module' );
            }

            $converter_handle = 'noesia-swa-image-tools-converter';
            $converter_rel    = 'js/universal-local-converter.js';
            $converter_file   = $assets_dir . $converter_rel;
            $converter_url    = $assets_url . $converter_rel;
            $converter_ver    = file_exists( $converter_file ) ? (string) filemtime( $converter_file ) : NOESIA_SWA_VERSION;

            wp_register_script(
                $converter_handle,
                $converter_url,
                array(
                    'noesia-swa-image-tools-jszip',
                    'noesia-swa-image-tools-pdf',
                    'noesia-swa-image-tools-heic2any',
                    'noesia-swa-image-tools-xlsx',
                    'noesia-swa-image-tools-jspdf',
                    'noesia-swa-image-tools-dompurify',
                    'noesia-swa-image-tools-rtfjs',
                    'noesia-swa-image-tools-utif',
                    'noesia-swa-image-tools-gifuct',
                    'noesia-swa-image-tools-upng',
                ),
                $converter_ver,
                true
            );

            wp_add_inline_script(
                $converter_handle,
                'window.noesiaSwaImageToolsConverterConfig = window.noesiaSwaImageToolsConverterConfig || ' . wp_json_encode(
                    array(
                        'vendorBaseUrl'            => $vendor_url,
                        'pdfWorkerSrc'             => $vendor_url . 'pdf.worker.min.js',
                        'independentEncoderJs'     => $vendor_url . 'noesia-independent-encoders.js',
                        'independentEncoderModule' => $vendor_url . 'noesia-independent-encoders.mjs',
                    )
                ) . ';',
                'before'
            );

            wp_enqueue_script( 'noesia-swa-image-tools-independent-encoders' );
            wp_enqueue_script( $converter_handle );
        }
        /**
         * Register a module-local vendor script.
         *
         * @param string   $handle Script handle.
         * @param string   $src    Script URL.
         * @param string   $file   Absolute file path.
         * @param string[] $deps   Script dependencies.
         * @return void
         */
        private function register_vendor_script( $handle, $src, $file, $deps = array() ) {
            $version = file_exists( $file ) ? (string) filemtime( $file ) : NOESIA_SWA_VERSION;

            wp_register_script(
                sanitize_key( (string) $handle ),
                esc_url_raw( (string) $src ),
                is_array( $deps ) ? $deps : array(),
                $version,
                true
            );
        }

        public function render_admin() {
            $module_view = trailingslashit( __DIR__ ) . 'views/admin.php';
            if ( file_exists( $module_view ) ) {
                require $module_view;
            }
        }
    }
}
