<?php

namespace AdvancedBlockControls;

// Exit if the file is accessed directly
if ( !defined('ABSPATH') ) {
    exit; 
}

class MenuManager
{
    public static $menu_slug = 'advanced-block-controls-settings';

    public function __construct()
    {
        add_action('admin_menu', [$this, 'register']);
        //add_action('admin_head', [$this, 'styles']);
    }

    public function register()
    {
        $menu_slug = 'advanced-block-controls-settings';
        $icon_url = plugins_url('images/menu-logo.svg', dirname(__FILE__));
    
        // hook:Allow users to modify or hide the menu by using the hook
        $show_menu = apply_filters('abcon/settings/show_admin', true);
    
        if ($show_menu) {
            // hook: Allow specifying a parent menu
            // Returns null/false for top-level menu, or a parent menu slug
            $parent_menu = apply_filters('abcon/settings/menu_parent', null);
            
            if ($parent_menu) {
                // Add as submenu under specified parent
                add_submenu_page(
                    $parent_menu, // Parent menu slug
                    'Block Controls',
                    'Block Controls',
                    'manage_options',
                    $menu_slug,
                    [$this, 'settings_menu_markup']
                );
            } else {
                // Add as top-level menu (default behavior)
                add_menu_page(
                    'Block Controls',
                    'Block Controls',
                    'manage_options',
                    $menu_slug,
                    [$this, 'settings_menu_markup'],
                    $icon_url,
                    81
                );
    
                add_submenu_page(
                    $menu_slug,
                    'Settings',
                    'Settings',
                    'manage_options',
                    $menu_slug,
                    [$this, 'settings_menu_markup']
                );
            }
    
            // Uncomment this if you want to add the Upgrade submenu back
            // add_submenu_page(
            //     $menu_slug,
            //     'Upgrade',
            //     'Upgrade',
            //     'manage_options',
            //     'advanced-block-controls-upgrade',
            //     [$this, 'upgrade_menu_markup']
            // );
        }
    }

    // public function upgrade_menu_markup()
    // {
    //     echo esc_html("Upgrade Page");
    // }

    public function settings_menu_markup()
    {
        echo '<div id="' . esc_attr('abc-settings') . '"></div>';
    }

    // public function styles()
    // {
    //     echo '<style>' . wp_strip_all_tags('
    //         #adminmenu .toplevel_page_advanced-block-controls-settings a[href*="advanced-block-controls-upgrade"] {
    //             background-color: #A72509;
    //             color: white !important;
    //             width: 135px;
    //             margin: 10px 0 5px 5px;
    //             padding: 6px;
    //             text-align: center;
    //             font-weight: bold;
    //             border-radius: 2px;
    //             box-shadow: 0 0 !important;
    //         }
    //         #adminmenu .toplevel_page_advanced-block-controls-settings a[href*="advanced-block-controls-upgrade"]:hover {
    //             background-color: #BF2F10; 
    //         }
    //     ') . '</style>';
    // }
}
