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

class STRB_Controllers_Menu extends STRB_Classes_FrontController
{

    /**
     * Hook the Admin load
     */
    public function hookInit()
    {
        $value=0;
        /* add the plugin menu in admin */
        if (STRB_Classes_Helpers_Tools::userCan('manage_options')) {
            try {
                //check if activated
                if (get_transient('strb_activate') == 1) {
                    // Delete the redirect transient
                    delete_transient('strb_activate');

                    if (STRB_Classes_Helpers_Tools::getOption('strb_api') == '') {
                         wp_redirect(STRB_Classes_Helpers_Tools::getAdminUrl('all_themes'));
                        die();
                    }
                   
                }

                //Add the Squirrly Class in all Squirrly SEO Pages
                add_filter('admin_body_class', array($this, 'addSettingsClass'));

            } catch (Exception $e) {
            }

        }

    }


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

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

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

            //Get current accessed page
            $page = apply_filters('strb_page', STRB_Classes_Helpers_Tools::getValue('page'));

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

            }
        }

        //Check the Dev Kit settings
        STRB_Classes_ObjController::getClass('STRB_Classes_Helpers_DevKit');
    
        //////////////
        
        $this->model->addMenu(
            array(
            apply_filters('strb_menu_name', 'some name'),
            apply_filters('strb_menu_name', 'Starbox') ,
            'manage_options',
            'all_themes',
            null,
            apply_filters('strb_logo', 'dashicons-star-filled')
            )
        );
    

        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'],
                    )
                );

            }

            //Update the external links in the menu
            global $submenu;
            if (!empty($submenu['strb_settings'])) {
                foreach ($submenu['strb_settings'] as &$item) {
                    if (isset($mainmenu[$item[2]]['href']) && $mainmenu[$item[2]]['href']) {
                        if (parse_url($mainmenu[$item[2]]['href'], PHP_URL_HOST) !== parse_url(home_url(), PHP_URL_HOST)) {
                            $item[0] .= '<i class="dashicons dashicons-external" style="font-size:12px;vertical-align:-2px;height:10px;"></i>';
                        }
                        $item[2] = $mainmenu[$item[2]]['href'];
                    }
                }
            }
        }
    }

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

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

        return $classes;
    }

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



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

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

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

        }

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

    }



}
