<?php
/**
 * Free Widgets module.
 *
 * @package Noesia_Smart_Waiter
 */

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

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

        public function get_id() {
            return 'widgets';
        }

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

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

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

        public function register_hooks() {
            $this->boot_local_data_service();
            $this->boot_local_ajax_handlers();

            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_widgets_loaded', $this );
        }

        private function boot_local_data_service() {
            if ( class_exists( 'Noesia_SWA_Widgets_Data' ) ) {
                return;
            }

            $data_file = trailingslashit( __DIR__ ) . 'includes/class-noesia-swa-widgets-data.php';
            if ( file_exists( $data_file ) ) {
                require_once $data_file;
            }
        }

        private function boot_local_ajax_handlers() {
            $ajax_file = trailingslashit( __DIR__ ) . 'includes/class-noesia-swa-module-widgets-ajax.php';
            if ( file_exists( $ajax_file ) ) {
                require_once $ajax_file;
                if ( class_exists( 'Noesia_SWA_Module_Widgets_Ajax' ) && class_exists( 'Noesia_SWA_Widgets_Data' ) ) {
                    new Noesia_SWA_Module_Widgets_Ajax( new Noesia_SWA_Widgets_Data() );
                }
            }
        }

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

            $registry->register_section(
                array(
                    'id'          => 'widgets',
                    'label'       => $this->get_label(),
                    'nav_label'   => __( 'Widgets', 'noesia-smart-waiter' ),
                    'nav_sub'     => __( 'Small Tools', 'noesia-smart-waiter' ),
                    'icon'        => 'dashicons-screenoptions',
                    'priority'    => 58,
                    'tier'        => 'free',
                    'owner'       => 'widgets',
                    '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(
                'widgets_refreshing'              => __( 'Refreshing…', 'noesia-smart-waiter' ),
                'widgets_refreshed'               => __( 'Rates refreshed.', 'noesia-smart-waiter' ),
                'widgets_refresh_failed'          => __( 'Unable to refresh rates right now.', 'noesia-smart-waiter' ),
                'widgets_json_valid'              => __( 'Valid JSON', 'noesia-smart-waiter' ),
                'widgets_json_invalid'            => __( 'Invalid JSON', 'noesia-smart-waiter' ),
                'widgets_hash_unsupported'        => __( 'Hashing is not supported in this browser.', 'noesia-smart-waiter' ),
                'widgets_copy'                    => __( 'Copy', 'noesia-smart-waiter' ),
                'widgets_copied'                  => __( 'Copied.', 'noesia-smart-waiter' ),
                'widgets_no_emails'               => __( 'No email addresses found.', 'noesia-smart-waiter' ),
                'widgets_diagnostics_running'     => __( 'Running system diagnostics…', 'noesia-smart-waiter' ),
                'widgets_diagnostics_done'        => __( 'System diagnostics completed.', 'noesia-smart-waiter' ),
                'widgets_diagnostics_partial'     => __( 'System diagnostics completed with limited samples.', 'noesia-smart-waiter' ),
                'widgets_diagnostics_failed'      => __( 'System diagnostics failed.', 'noesia-smart-waiter' ),
                'widgets_diagnostics_copy_ready'  => __( 'Diagnostics copied.', 'noesia-smart-waiter' ),
                'widgets_diagnostics_copy_failed' => __( 'Unable to copy diagnostics right now.', 'noesia-smart-waiter' ),
                'widgets_not_reported'            => __( 'Not reported', 'noesia-smart-waiter' ),
                'widgets_unavailable'             => __( 'Unavailable', 'noesia-smart-waiter' ),
                'widgets_ruler_show'              => __( 'Show Ruler', 'noesia-smart-waiter' ),
                'widgets_ruler_hide'              => __( 'Hide Ruler', 'noesia-smart-waiter' ),
                'widgets_color_invalid'           => __( 'Enter a valid HEX color.', 'noesia-smart-waiter' ),
                'widgets_currency_stale'          => __( 'Showing cached rates.', 'noesia-smart-waiter' ),
                'widgets_currency_live'           => __( 'Live provider data loaded.', 'noesia-smart-waiter' ),
                'widgets_date_unavailable'        => __( 'Update time unavailable', 'noesia-smart-waiter' ),
                'widgets_connection_note'         => __( 'Runs same-installation diagnostics, not ISP bandwidth tests.', 'noesia-smart-waiter' ),
            );

            return array_merge( $i18n, $strings );
        }

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