<?php
defined('ABSPATH') || die('Cheatin\' uh?');

class PROD_Controllers_Menu extends PROD_Classes_FrontController
{



    public function maybe_redirect_after_activation() {
        if (!is_admin()) {
            return;
        }

        if (get_transient('prod_activation_redirect')) {
            delete_transient('prod_activation_redirect');

            if (PROD_Classes_Helpers_Tools::getOption('PROD_api') == '') {
                wp_safe_redirect(PROD_Classes_Helpers_Tools::getAdminUrl('prod_activation_page'));
                exit;
            }
        }
    }

    /**
     * Hook the Admin load
     */
    
    public function hookInit()
    {
           // Add this line to register the redirect
    add_action('admin_init', array($this, 'maybe_redirect_after_activation'));

    // The rest of your hookInit logic...
    if (PROD_Classes_Helpers_Tools::userCan('manage_options')) {
        try {
            // Add other logic here if needed...
            add_filter('admin_body_class', array($this, 'addSettingsClass'));
        } catch (Exception $e) {
        }
    }

    }


    /**
     * Creates the Setting menu in Wordpress
     */
    public function hookMenu()
    {
        global $PROD_setting_page;

        //Load the Squirrly Menu
        $mainmenu = $this->model->getMainMenu();

        //run compatibility check on Squirrly settings
        if (PROD_Classes_Helpers_Tools::getIsset('page')) {

            //Get current accessed page
            $page = apply_filters('PROD_page', PROD_Classes_Helpers_Tools::getValue('page'));

            if (in_array($page, array_keys($mainmenu))) {
                //Set if it's a plugin Settings Page
                $PROD_setting_page = true;

            }
        }


        //Check the Dev Kit settings
        PROD_Classes_ObjController::getClass('PROD_Classes_Helpers_DevKit');
    
        //////////////
        
        $this->model->addMenu(
            array(
            apply_filters('PROD_menu_name', 'some name'),
            apply_filters('PROD_menu_name', 'Product Perfect Feed') ,
            'manage_options',
            'prod_activation_page',
            '__return_null',
            apply_filters('strb_logo', 'https://storage.googleapis.com/prod_perf/logo2%20(2).png')
            )
        );
        add_action('admin_menu', function() {
            remove_submenu_page('prod_activation_page', 'prod_activation_page');
        }, 999);

        if (!empty($mainmenu)) {
            foreach ($mainmenu as $name => $item) {

                //Add the page
                $this->model->addSubmenu(
                    array(
                    $item['parent'],
                    $item['title'],
                    $item['title'],
                    $item['capability'],
                    $name,
                    $item['function'],
                    )
                );

            }

          
        }
    }
   
 

    /**
     * Add the Squirrly Setttings Class in all Squirrly SEO Pages
     * Used for personal layout
     *
     * @param  $classes
     * @return string
     */
    public function addSettingsClass($classes)
    {
         global $PROD_setting_page;

        // if (isset($PROD_setting_page) && $PROD_setting_page) {
        //     $classes = "$classes starbox-settings";
        // }

         return $classes;
    }

    /**
     * Hook the Head
     */
    public function hookHead()
    {
        global $PROD_setting_page;



        //Load settings only in the Squirrly Menu
        if (isset($PROD_setting_page) && $PROD_setting_page) {

            PROD_Classes_ObjController::getClass('PROD_Classes_DisplayController')->loadMedia('bootstrap-reboot', array('dependencies' => 'jquery'));
            if (is_rtl()) {
                PROD_Classes_ObjController::getClass('PROD_Classes_DisplayController')->loadMedia('popper', array('dependencies' => 'jquery'));
                PROD_Classes_ObjController::getClass('PROD_Classes_DisplayController')->loadMedia('bootstrap.rtl', array('dependencies' => 'jquery'));
                PROD_Classes_ObjController::getClass('PROD_Classes_DisplayController')->loadMedia('rtl', array('dependencies' => 'jquery'));
            } else {
                PROD_Classes_ObjController::getClass('PROD_Classes_DisplayController')->loadMedia('popper', array('dependencies' => 'jquery'));
                PROD_Classes_ObjController::getClass('PROD_Classes_DisplayController')->loadMedia('bootstrap', array('dependencies' => 'jquery'));
            }
            PROD_Classes_ObjController::getClass('PROD_Classes_DisplayController')->loadMedia('bootstrap-select', array('dependencies' => 'jquery'));

            PROD_Classes_ObjController::getClass('PROD_Classes_DisplayController')->loadMedia('switchery', array('dependencies' => 'jquery'));
            PROD_Classes_ObjController::getClass('PROD_Classes_DisplayController')->loadMedia('fontawesome', array('dependencies' => 'jquery'));
            PROD_Classes_ObjController::getClass('PROD_Classes_DisplayController')->loadMedia('global', array('dependencies' => 'jquery'));
            PROD_Classes_ObjController::getClass('PROD_Classes_DisplayController')->loadMedia('navbar', array('dependencies' => 'jquery'));
	        PROD_Classes_ObjController::getClass('PROD_Classes_DisplayController')->loadMedia('settings', array('dependencies' => 'jquery'));

        }

        //Load the Squirrly Logo on all Dashboard
        PROD_Classes_ObjController::getClass('PROD_Classes_DisplayController')->loadMedia('logo');

    }

}
