<?php

namespace forge12\floating_menu {
    if (!defined('ABSPATH')) {
        exit;
    }

    /**
     * Dependencies
     */
    require_once(FORGE12_FLOATING_DIR . 'core/ComponentManager.class.php');
    require_once(FORGE12_FLOATING_DIR . 'core/UIPage.class.php');
    require_once(FORGE12_FLOATING_DIR . 'core/UIPageForm.class.php');
    require_once(FORGE12_FLOATING_DIR . 'core/Support.class.php');

    require_once(FORGE12_FLOATING_DIR . 'core/Ajax.class.php');
    require_once(FORGE12_FLOATING_DIR . 'core/UI.class.php');
    require_once(FORGE12_FLOATING_DIR . 'core/HTMLSelect.class.php');
    require_once(FORGE12_FLOATING_DIR . 'core/Messages.class.php');

    /**
     * Orchestrator for the F12 Floating Menu plugin.
     *
     * Bootstrapped from the main plugin file (f12-floating-menu.php). This class
     * contains no plugin header, constants, or activation hooks — only wiring.
     */
    class Application
    {
        /**
         * @var Application|Null
         */
        private static $_instance = null;

        /**
         * Get the instance of the custom links controller
         * @return Application
         */
        public static function getInstance()
        {
            if (self::$_instance == null) {
                self::$_instance = new Application();
            }

            return self::$_instance;
        }

        /**
         * Locate the Template
         *
         * Search Order:
         * 1. /themes/theme/templates/$template_name
         * 2. /themes/theme/$template_name
         * 3. /plugins/plugin/templates/$template_name
         *
         * @param string $template_name
         * @return string
         */
        private function locateTemplateFile(string $template_name): string
        {
            // Set template path
            $template_path = 'templates/';

            // Set default template path
            $default_path = FORGE12_FLOATING_DIR . 'templates/';

            // Search template file in theme folder
            $template = locate_template(array(
                $template_path . $template_name,
                $template_name
            ));

            if (!$template) {
                $template = $default_path . $template_name;
            }

            return $template;
        }

        /**
         * Render the template file
         * @param $template_name
         * @param array $args
         */
        public function getTemplateFile($template_name, $args = array()): void
        {
            if (is_array($args)) {
                extract($args);
            }

            $template_file = $this->locateTemplateFile($template_name);

            if (!file_exists($template_file)) {
                _doing_it_wrong(__FUNCTION__, sprintf('<code>%s</code> does not exist.', $template_file));
                return;
            }

            include $template_file;
        }


        /**
         * CustomLinks constructor.
         */
        private function __construct()
        {
            add_action( 'init', [ $this, 'load_textdomain' ] );
            add_action( 'rest_api_init', [ $this, 'register_rest_api' ] );

            $UI = new UI(FORGE12_FLOATING_SLUG, 'Forge12 Floating Menu', 'manage_options');
            $UIMenu = new UIMenu(FORGE12_FLOATING_SLUG);

            add_filter('f12-' . FORGE12_FLOATING_SLUG . '-settings', '\forge12\floating_menu\Application::getSettings');

            ComponentManager::getInstance();

            Support::getInstance();
        }

        /**
         * Load the plugin text domain for translations.
         *
         * @return void
         */
        public function load_textdomain(): void
        {
            load_plugin_textdomain(
                'f12-floating-menu',
                false,
                dirname( FORGE12_FLOATING_BASENAME ) . '/languages'
            );
        }

        /**
         * Register the REST API routes.
         *
         * @return void
         */
        public function register_rest_api(): void
        {
            require_once FORGE12_FLOATING_DIR . 'includes/class-rest-api.php';
        }
    }
}
