<?php
/**
 * Admin Assets Class
 *
 * @package Easy_Invoice
 * @subpackage Admin
 */

namespace EasyInvoice\Admin;

use EasyInvoice\Constants\PagesSlugs;

/**
 * AdminAssets Class
 *
 * Handles registration and enqueuing of admin CSS and JS assets.
 */
class AdminAssets {
    /**
     * Register hooks
     */
    public function register() {
        add_action('admin_enqueue_scripts', array($this, 'enqueueAdminAssets'));
    }

    /**
     * Enqueue admin assets
     *
     * @param string $hook The current admin page
     */
    public function enqueueAdminAssets($hook) {
        // Only load on our plugin pages
        if (empty($hook) || strpos($hook, 'easy-invoice') === false) {
            return;
        }

        // Enqueue styles
        $this->enqueueStyles();

        // Enqueue scripts
        $this->enqueueScripts($hook);

        // Localize script data
        $this->localizeScripts($hook);
    }

    /**
     * Enqueue stylesheets
     */
    private function enqueueStyles() {

		wp_enqueue_style(
			'easy-invoice-tailwind',
			EASY_INVOICE_PLUGIN_URL . 'assets/lib/tailwind/tailwind.min.css',
			array(),
			EASY_INVOICE_VERSION
		);

		$page = isset($_GET['page']) ? sanitize_text_field($_GET['page']) : '';

		// Main plugin layout + header strip alignment (always on Easy Invoice admin screens).
		wp_enqueue_style(
			'easy-invoice-main',
			EASY_INVOICE_PLUGIN_URL . 'assets/css/easy-invoice.css',
			array(),
			EASY_INVOICE_VERSION
		);

		if($page === 'easy-invoice-builder') {
			// Main admin styles

			wp_enqueue_style(
				'easy-invoice-preview',
				EASY_INVOICE_PLUGIN_URL . 'assets/css/invoice-preview.css',
				array(),
				EASY_INVOICE_VERSION
			);
		}

		wp_enqueue_style(
			'font-awesome',
			EASY_INVOICE_PLUGIN_URL . 'assets/lib/font-awesome/css/all.min.css',
			array(),
			EASY_INVOICE_VERSION
		);


        // Add RTL support
        wp_style_add_data('easy-invoice-admin', 'rtl', 'replace');
    }

    /**
     * Enqueue scripts
     *
     * @param string $hook The current admin page
     */
    private function enqueueScripts($hook) {
        // Only load on Easy Invoice pages
        if (empty($hook) || strpos($hook, 'easy-invoice') === false) {
            return;
        }

        // Common admin scripts
        wp_enqueue_script('jquery');
        wp_enqueue_script('jquery-ui-core');
        wp_enqueue_script('jquery-ui-datepicker');
        wp_enqueue_script('wp-util');

        // Add WordPress core dependencies that provide the 'wp' object
        wp_enqueue_script('wp-api-fetch');
        wp_enqueue_script('wp-i18n');
        wp_enqueue_script('wp-a11y');
        wp_enqueue_script('wp-hooks');

        // Settings page script
        if (isset($_GET['page']) && $_GET['page'] === 'easy-invoice-settings') {
            wp_enqueue_script(
                'easy-invoice-settings',
                EASY_INVOICE_PLUGIN_URL . 'assets/js/settings.js',
                array('jquery', 'wp-util', 'wp-api-fetch', 'wp-i18n'),
                EASY_INVOICE_VERSION,
                true
            );

            // Localize the script with necessary data
            wp_localize_script('easy-invoice-settings', 'easyInvoiceSettings', array(
                'nonce' => wp_create_nonce('easy_invoice_settings'),
                'ajaxurl' => admin_url('admin-ajax.php')
            ));
            return; // Don't load other scripts on settings page
        }

        // Load dependencies
        wp_enqueue_script('jquery-ui-sortable');

        // Main admin script
        wp_enqueue_script('jquery');

        // Register and enqueue our scripts
        wp_register_script('easy-invoice-scripts', EASY_INVOICE_PLUGIN_URL . 'assets/js/easy-invoice.js', array('jquery', 'wp-api-fetch', 'wp-i18n', 'wp-a11y'), EASY_INVOICE_VERSION, true);
        wp_enqueue_script('easy-invoice-scripts');

        // Conditionally load client manager only on invoice pages (not quote pages or invoice builder)
        if (!(isset($_GET['page']) && ($_GET['page'] === 'easy-invoice-quote-builder' || $_GET['page'] === 'easy-invoice-builder'))) {
            wp_register_script('easy-invoice-client-manager', EASY_INVOICE_PLUGIN_URL . 'assets/js/client-manager.js', array('jquery', 'easy-invoice-scripts', 'wp-api-fetch', 'wp-i18n'), EASY_INVOICE_VERSION, true);
            wp_enqueue_script('easy-invoice-client-manager');
        }

        // Load clients.js on the clients page
        if (isset($_GET['page']) && $_GET['page'] === PagesSlugs::CLIENTS) {
            wp_register_script('easy-invoice-clients', EASY_INVOICE_PLUGIN_URL . 'assets/js/clients.js', array('jquery', 'easy-invoice-scripts', 'easy-invoice-confirmation-modal', 'easy-invoice-toast', 'wp-api-fetch', 'wp-i18n'), EASY_INVOICE_VERSION, true);
            wp_enqueue_script('easy-invoice-clients');
        }

        wp_register_script('easy-invoice-payment-manager', EASY_INVOICE_PLUGIN_URL . 'assets/js/payment-manager.js', array('jquery', 'easy-invoice-scripts', 'wp-api-fetch', 'wp-i18n'), EASY_INVOICE_VERSION, true);
        wp_enqueue_script('easy-invoice-payment-manager');

        // Tooltip manager - reusable across the plugin
        wp_register_script('easy-invoice-tooltip', EASY_INVOICE_PLUGIN_URL . 'assets/js/tooltip-manager.js', array('jquery', 'wp-api-fetch', 'wp-i18n'), EASY_INVOICE_VERSION, true);
        wp_enqueue_script('easy-invoice-tooltip');

        // Confirmation modal - reusable across the plugin
        wp_register_script('easy-invoice-confirmation-modal', EASY_INVOICE_PLUGIN_URL . 'assets/js/confirmation-modal.js', array('jquery', 'wp-api-fetch', 'wp-i18n'), EASY_INVOICE_VERSION, true);
        wp_enqueue_script('easy-invoice-confirmation-modal');

        // Toast notification system - global across the plugin
        wp_register_script('easy-invoice-toast', EASY_INVOICE_PLUGIN_URL . 'assets/js/easy-invoice-toast.js', array('jquery', 'wp-api-fetch', 'wp-i18n'), EASY_INVOICE_VERSION, true);
        wp_enqueue_script('easy-invoice-toast');

        // jsPDF for PDF generation - available on all Easy Invoice pages
        wp_register_script('jspdf', 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js', array(), '2.5.1', true);
        wp_enqueue_script('jspdf');


        // Conditionally load invoice-specific scripts only on invoice pages
        if (isset($_GET['page']) && ($_GET['page'] === 'easy-invoice-new' || $_GET['page'] === 'easy-invoice-builder')) {
            // Invoice builder scripts
            wp_register_script('easy-invoice-builder', EASY_INVOICE_PLUGIN_URL . 'assets/js/invoice-builder.js', array('jquery', 'easy-invoice-scripts', 'easy-invoice-payment-manager', 'wp-api-fetch', 'wp-i18n'), EASY_INVOICE_VERSION, true);
            wp_enqueue_script('easy-invoice-builder');

            // Use invoice-save.js for comprehensive save functionality
            wp_register_script('easy-invoice-save', EASY_INVOICE_PLUGIN_URL . 'assets/js/invoice-save.js', array('jquery', 'easy-invoice-scripts', 'easy-invoice-payment-manager', 'wp-api-fetch', 'wp-i18n'), EASY_INVOICE_VERSION, true);
            wp_enqueue_script('easy-invoice-save');
        }

        // Quote builder (full-screen editor)
        if (isset($_GET['page']) && $_GET['page'] === 'easy-invoice-quote-builder') {
            wp_register_script(
                'easy-quote-save',
                EASY_INVOICE_PLUGIN_URL . 'assets/js/quote-save.js',
                array('jquery', 'easy-invoice-scripts', 'easy-invoice-payment-manager', 'wp-api-fetch', 'wp-i18n'),
                EASY_INVOICE_VERSION,
                true
            );
            wp_enqueue_script('easy-quote-save');
        }

        if (isset($_GET['page']) && in_array($_GET['page'], array('easy-invoice-builder', 'easy-invoice-quote-builder'), true)) {
            wp_enqueue_script(
                'easy-invoice-builder-mobile-tabs',
                EASY_INVOICE_PLUGIN_URL . 'assets/js/builder-mobile-tabs.js',
                array('jquery'),
                EASY_INVOICE_VERSION,
                true
            );
            wp_enqueue_script(
                'easy-invoice-builder-ux',
                EASY_INVOICE_PLUGIN_URL . 'assets/js/builder-ux.js',
                array('jquery', 'easy-invoice-scripts'),
                EASY_INVOICE_VERSION,
                true
            );
        }
    }

    /**
     * Localize script data
     *
     * @param string $hook The current admin page
     */
    private function localizeScripts($hook) {
        global $pagenow, $post;

        // Get currency settings
        $settings_controller = new \EasyInvoice\Controllers\SettingsController();
        $settings = $settings_controller->getSettings();
        $currency_code = $settings['easy_invoice_currency_code'] ?? 'USD';
        $currency_position = $settings['easy_invoice_currency_position'] ?? 'left';
        $currency_symbol = easy_invoice_get_currency_symbol();

        $current_admin_page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : '';

        // Default data
        $script_data = array(
            'ajaxUrl' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('easy_invoice_nonce'),
            'i18n' => array(
                'confirm_delete' => __('Are you sure you want to delete this invoice?', 'easy-invoice'),
                'invoice_deleted' => __('Invoice deleted successfully', 'easy-invoice'),
                'client_deleted' => __('Client deleted successfully', 'easy-invoice'),
                'confirm_delete_client' => __('Are you sure you want to delete this client?', 'easy-invoice'),
                'edit_invoice' => __('Edit Invoice', 'easy-invoice'),
                'create_invoice' => __('Create Invoice', 'easy-invoice'),
                'save' => __('Save', 'easy-invoice'),
                'cancel' => __('Cancel', 'easy-invoice'),
                'add_item' => __('Add Item', 'easy-invoice'),
                'delete_item' => __('Delete Item', 'easy-invoice'),
                'error' => __('An error occurred', 'easy-invoice'),
            ),
        );

        if (in_array($current_admin_page, array('easy-invoice-builder', 'easy-invoice-quote-builder'), true)) {
            $script_data['i18n'] = array_merge(
                $script_data['i18n'],
                array(
                    'saving'            => __('Saving…', 'easy-invoice'),
                    'sending'           => __('Sending…', 'easy-invoice'),
                    'save_invoice'      => __('Save Invoice', 'easy-invoice'),
                    'update_invoice'    => __('Update Invoice', 'easy-invoice'),
                    'save_quote'        => __('Save Quote', 'easy-invoice'),
                    'update_quote'      => __('Update Quote', 'easy-invoice'),
                    'send_invoice'      => __('Send Invoice', 'easy-invoice'),
                    'send_quote'        => __('Send Quote', 'easy-invoice'),
                    'save_first_invoice' => __('Please save the invoice before sending.', 'easy-invoice'),
                    'save_first_quote'   => __('Please save the quote before sending.', 'easy-invoice'),
                    'email_sent_invoice' => __('Invoice email sent successfully.', 'easy-invoice'),
                    'email_sent_quote'   => __('Quote email sent successfully.', 'easy-invoice'),
                    'email_error'        => __('Error sending email.', 'easy-invoice'),
                    'network_error'      => __('Error connecting to server.', 'easy-invoice'),
                    'tab_editor'         => __('Editor', 'easy-invoice'),
                    'tab_preview'        => __('Preview', 'easy-invoice'),
                    'confirm_send_invoice_title' => __('Send invoice by email?', 'easy-invoice'),
                    'confirm_send_invoice_message' => __('This will email the invoice to the client using your configured template.', 'easy-invoice'),
                    'confirm_send_quote_title' => __('Send quote by email?', 'easy-invoice'),
                    'confirm_send_quote_message' => __('This will email the quote to the client using your configured template.', 'easy-invoice'),
                    'confirm_send_confirm' => __('Send', 'easy-invoice'),
                    'confirm_cancel'     => __('Cancel', 'easy-invoice'),
                    'builder_unsaved_warning' => __('You have unsaved changes. If you leave this page, your changes may be lost.', 'easy-invoice'),
                    'builder_title_hint' => __('The title above matches the document title field in the editor.', 'easy-invoice'),
                    'builder_shell_hint' => __('On smaller screens, use the tabs to switch between the editor and the live preview.', 'easy-invoice'),
                    'builder_shell_hint_dismiss' => __('Got it', 'easy-invoice'),
                    'send_invoice_confirm_browser' => __('Send this invoice by email?', 'easy-invoice'),
                    'send_quote_confirm_browser' => __('Send this quote by email?', 'easy-invoice'),
                )
            );
        }

        $script_data = array_merge(
            $script_data,
            array(
            'urls' => array(
                'preview' => admin_url('admin.php?action=easy_invoice_preview&invoice_id='),
                'edit' => admin_url('admin.php?page=easy-invoice-new&id='),
            ),
            'settings' => array(
                'currency_symbol' => $currency_symbol,
                'currency_position' => $currency_position,
                'currency_code' => $currency_code,
                'decimal_separator' => $settings['easy_invoice_decimal_separator'] ?? '.',
                'thousand_separator' => $settings['easy_invoice_thousands_separator'] ?? ',',
                'decimal_places' => $settings['easy_invoice_decimal_precision'] ?? 2,
                'date_format' => get_option('date_format', 'F j, Y'),
            ),
            'showAdjustField' => \EasyInvoice\Controllers\SettingsController::shouldShowInvoiceAdjustField(),
            )
        );

        // Localize common scripts
        wp_localize_script('easy-invoice-scripts', 'easyInvoice', $script_data);

        // Conditionally localize client manager only when it's loaded
        if (!(isset($_GET['page']) && ($_GET['page'] === 'easy-invoice-quote-builder' || $_GET['page'] === 'easy-invoice-builder'))) {
            wp_localize_script('easy-invoice-client-manager', 'easyInvoice', $script_data);
        }

        wp_localize_script('easy-invoice-payment-manager', 'easyInvoice', $script_data);
        wp_localize_script('easy-invoice-tooltip', 'easyInvoice', $script_data);

        // Conditionally localize invoice-specific scripts
        if (isset($_GET['page']) && ($_GET['page'] === 'easy-invoice-new' || $_GET['page'] === 'easy-invoice-builder')) {
            // Add invoice-specific field configuration
            $form_manager = new \EasyInvoice\Forms\Invoice\InvoiceFormManager();
            $script_data['fieldConfig'] = $form_manager->getFieldConfigForJavaScript();

            // Add additional data for the invoice edit page
            $invoice_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
            $invoice_items_json = '';

            if ($invoice_id > 0) {
                // Existing invoice - get its data
                $repository = \EasyInvoice\Providers\InvoiceServiceProvider::getInvoiceRepository();
                $invoice = $repository->find($invoice_id);

                if ($invoice) {
                    $script_data['invoice_id'] = $invoice_id;
                    $script_data['editMode'] = true;

                    // Get invoice data for JavaScript
                    $invoice_data = $invoice->toArray();
                    $script_data['invoiceData'] = $invoice_data;

                    // Get invoice items
                    $items = $invoice->getItems();
                    $items_data = [];
                    foreach ($items as $item) {
                        $items_data[] = [
                            'id' => $item->getId(),
                            'name' => $item->getName(),
                            'description' => $item->getDescription(),
                            'quantity' => $item->getQuantity(),
                            'price' => $item->getPrice(),
                            'taxable' => $item->isTaxable(),
                            'total' => $item->getAmount()
                        ];
                    }
                    $script_data['invoiceItems'] = $items_data;

                    // Get client data if available
                    if ($invoice->getClientId()) {
                        $client_repository = \EasyInvoice\Providers\ClientServiceProvider::getClientRepository();
                        $client = $client_repository->find($invoice->getClientId());
                        if ($client) {
                            $script_data['clientData'] = [
                                'id' => $client->getId(),
                                'name' => $client->getBusinessClientName() ?: ($client->getFirstName() . ' ' . $client->getLastName()),
                                'email' => $client->getEmail(),
                                'phone' => $client->getExtraInfo(),
                                'address' => $client->getAddress(),
                                'company' => $client->getBusinessClientName()
                            ];
                        }
                    }
                }
            } else {
                // New invoice - set default items
                $script_data['editMode'] = false;
                $script_data['invoice_id'] = 0;
                $default_items = [
                    [
                        'name' => '',
                        'description' => '',
                        'quantity' => 0,
                        'price' => 0,
                        'taxable' => true
                    ]
                ];
                $script_data['default_items'] = $default_items;
            }

            // Localize invoice-specific scripts
            wp_localize_script('easy-invoice-builder', 'easyInvoice', $script_data);
            wp_localize_script('easy-invoice-save', 'easyInvoice', $script_data);
        }

        if (isset($_GET['page']) && $_GET['page'] === 'easy-invoice-quote-builder') {
            // Override showAdjustField for quote pages
            $script_data['showAdjustField'] = \EasyInvoice\Controllers\SettingsController::shouldShowQuoteAdjustField();
            wp_localize_script('easy-quote-save', 'easyInvoice', $script_data);
        }
    }
}
