<?php

if (!class_exists('WizartArVisualizerAdmin')) {

    class WizartArVisualizerAdmin {
        private static $integration_version = '1.1';
        private $entry_point_button;
        private $floating_button;

        private function get_integration_version() {
            return self::$integration_version;
        }

        public function __construct() {
            $this->entry_point_button = "https://d35so7k19vd0fx.cloudfront.net/production/integration/v{$this->get_integration_version()}/entry-point.min.js";
            $this->floating_button = "https://d35so7k19vd0fx.cloudfront.net/production/integration/v{$this->get_integration_version()}/floating-button.min.js";

            add_action('admin_menu', array($this, 'add_admin_menu_page'));
            add_action('woocommerce_before_single_product', array($this, 'add_wizart_entry_point_button'));
            add_action('woocommerce_before_single_product', array($this, 'add_wizart_iframe_integration'));
            $that = $this;
            add_action('wp_body_open', function() use ($that) {
                if (is_front_page()) {
                    $that->add_wizart_floating_button();
                }
            });
            add_action('admin_init', array($this, 'wizart_register_settings'));
            add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_styles'));
        }

        public function add_admin_menu_page() {
            add_menu_page(
                'Wizart Home',
                'Wizart Home',
                'manage_options',
                'wizart-admin-panel',
                array($this, 'render_admin_page'),
                'dashicons-layout',
                20
            );
        }

        public function render_admin_page() {
            require_once plugin_dir_path( dirname( __FILE__, 2 ) ) . 'admin/admin-page.php';
        }

        public function enqueue_public_scripts_once() {
            if (!did_action('wp_enqueue_scripts_once')) {
                add_action('wp_enqueue_scripts_once', '__return_true');
                wp_enqueue_script(
                    'wizart-entry-point-external',
                    $this->entry_point_button,
                    [],
                    self::$integration_version,
                    true
                );
                wp_enqueue_script('vendor-code-checker', plugins_url('/assets/js/vendorCodeChecker.js', dirname(__FILE__, 2)), [], $this->get_plugin_version(), true);
            }
        }

        public function add_wizart_entry_point_button() {
            $show_button = get_option('wizartEntryPointButtonDisplay');
            $show_iframe = get_option('wizartIframeIntegrationDisplay');

            if ($show_button && !$show_iframe) {
                require_once plugin_dir_path(dirname(__FILE__, 2)) . 'public/display-entry-point-button.php';
                $this->enqueue_public_scripts_once();
            }
        }

        public function add_wizart_iframe_integration() {
            $show_button = get_option('wizartEntryPointButtonDisplay');
            $show_iframe = get_option('wizartIframeIntegrationDisplay');

            if ($show_iframe && !$show_button) {
                require_once plugin_dir_path(dirname(__FILE__, 2)) . 'public/display-iframe-integration.php';
                $this->enqueue_public_scripts_once();
            }
        }

        public function add_wizart_floating_button() {
            $show_button = get_option('wizartFloatingButtonDisplay');
            if ($show_button) {
                wp_enqueue_script(
                    'wizart-floating-button-external',
                    $this->floating_button,
                    [],
                    self::$integration_version,
                    true
                );

                require_once plugin_dir_path( dirname( __FILE__, 2 ) ) . 'public/display-floating-button.php';
            }
        }

        public function wizart_register_settings() {
            $sanitize_callback = function ($value) {
                return sanitize_text_field($value);
            };

            $options = [
                'wizartFloatingButtonDisplay',
                'wizartFloatingElementSelector',
                'wizartFloatingInsertElementPosition',
                'wizartFloatingFontSize',
                'wizartFloatingTitle',
                'wizartFloatingTooltipTitle',
                'wizartFloatingTooltipPosition',
                'wizartFloatingTooltipDisable',
                'wizartFloatingGlitterDisable',
                'wizartFloatingClassName',
                'wizartFloatingBorderRadius',
                'wizartFloatingParameters',
                'wizartFloatingBackground',
                'wizartFloatingColor',
                'wizartFloatingWidth',
                'wizartFloatingHeight',
                'wizartFloatingBorder',
                'wizartFloatingImage',
                'wizartFloatingOnloadCallback',
                'wizartFloatingOnCloseCallback',
                'wizartFloatingOnButtonClick',
                'wizartFloatingIframeElementSelector',
                'wizartFloatingInsertIframeElementPosition',
                'wizartOnAddShoppingCartItem',
                'wizartOnRemoveShoppingCartItem',
                'token',
                'useProductNameAsSku',
                'context',
                'locale',
                'roomUuid',
                'deactivateCustomPhoto',
                'openFirstRoom',

                'wizartEntryPointButtonDisplay',
                'wizartEntryElementSelector',
                'wizartEntryElementsSelector',
                'wizartEntryPointTitle',
                'wizartEntryPointTooltipTitle',
                'wizartEntryPointTooltipPosition',
                'wizartEntryPointTooltipDisable',
                'wizartEntryPointGlitterDisable',
                'wizartEntryPointClassName',
                'wizartEntryPointBorderRadius',
                'wizartEntryPointBackground',
                'wizartEntryPointColor',
                'wizartEntryPointWidth',
                'wizartEntryPointHeight',
                'wizartEntryPointBorder',
                'wizartEntryPointImage',
                'wizartEntryPointOnloadCallback',
                'wizartEntryPointOnCloseCallback',
                'wizartEntryPointOnButtonClick',
                'wizartEntryPointInsertElementPosition',
                'wizartEntryPointIframeElementSelector',
                'wizartEntryPointInsertIframeElementPosition',

                'wizartIframeIntegrationDisplay',
                'wizartIframeIntegrationSelector',
                'wizartIframeIntegrationInsertElementPosition'
            ];

            foreach ($options as $option) {
                register_setting('wizart_button_settings_group', $option, [
                    'sanitize_callback' => $sanitize_callback
                ]);
            }
        }

        public function enqueue_admin_styles() {
            wp_enqueue_style( 'wizart-ar-visualizer-admin-style', plugins_url( '/assets/css/admin-panel-style.css', dirname(__FILE__, 2) ), [], $this->get_plugin_version() );
        }

        /**
         * Get plugin version from main plugin file
         */
        private function get_plugin_version() {
            if (!function_exists('get_plugin_data')) {
                require_once(ABSPATH . 'wp-admin/includes/plugin.php');
            }

            $plugin_data = get_plugin_data(plugin_dir_path(dirname(__FILE__, 2)) . 'wizart-ar-visualizer.php');
            return $plugin_data['Version'] ?? '1.0.0';
        }
    }
}