<?php

if (! defined('ABSPATH')) exit;

if (! class_exists('Gridpress_Admin')) {

    class Gridpress_Admin
    {
        public function __construct()
        {

            add_action(
                'admin_menu',
                array($this, 'gridpress_admin_menu_pages'),
                10
            );

            $this->gridpress_includes();

            add_action(
                'admin_footer',
                array($this, 'gridpress_popup_modal_init'),
                20
            );
        }

        /**
         * Include admin files
         */
        private function gridpress_includes()
        {

            if (is_admin()) {

                include_once GRIDPRESS_ABSPATH . 'admin/admin-ajax.php';

                include_once GRIDPRESS_ABSPATH . 'admin/settings/field-functions.php';

                include_once GRIDPRESS_ABSPATH . 'admin/settings/general-settings.php';
            }
        }

        /**
         * Register admin menu
         */
        public function gridpress_admin_menu_pages()
        {

            add_menu_page(
                esc_html_x('Gridpress Options Page', 'Admin title', 'gridpress-grid-layout-design'),
                esc_html_x('Gridpress', 'Main menu title', 'gridpress-grid-layout-design'),
                'gridpress_settings',
                'gridpress-options',
                array($this, 'gridpress_create_options_page'),
                'dashicons-grid-view',
                50
            );
        }

        /**
         * Options page
         */
        public function gridpress_create_options_page()
        {

            $gridpress_page_url      = admin_url('admin.php?page=gridpress-options');
            $gridpress_options_menu  = self::gridpress_options_menu();
            $gridpress_menu_tab      = sanitize_key(gridpress_get_query_var('tab'));
            $gridpress_menu_section  = sanitize_key(gridpress_get_query_var('section'));
            ?>
            <form method="post" action="<?php echo esc_url(admin_url('options.php')); ?>">
                <?php wp_nonce_field('gridpress_admin_nonce', 'gridpress_nonce'); ?>

                <div class="gridpress-admin-options-wrap">
                    <?php
                    if (! empty($gridpress_options_menu[$gridpress_menu_tab]['sub_menu'])) {
                        $gridpress_sub_menu       = $gridpress_options_menu[$gridpress_menu_tab]['sub_menu'];
                        $gridpress_menu_tab_clean = sanitize_key($gridpress_menu_tab);

                        foreach ($gridpress_sub_menu as $gridpress_section_key => $gridpress_section_item) {
                            $gridpress_section_key  = sanitize_key($gridpress_section_key);
                            $gridpress_section_item = sanitize_text_field($gridpress_section_item);
                            $gridpress_section_tab_url = add_query_arg(
                                array(
                                    'tab'     => $gridpress_menu_tab_clean,
                                    'section' => $gridpress_section_key,
                                ),
                                $gridpress_page_url
                            );
                            // Build, heavily escape, and output the anchor tag safely inside PHP context
                            echo wp_kses(
                                sprintf(
                                    /* translators: 1: SVG icon or target URL, 2: Sub-menu section title text. */
                                    __('<a href="%1$s">%2$s</a>', 'gridpress-grid-layout-design'),
                                    esc_url($gridpress_section_tab_url),
                                    esc_html($gridpress_section_item)
                                ),
                                array(
                                    'a' => array(
                                        'href' => array(),
                                    ),
                                )
                            );
                        }
                    }

                    if (
                        ! empty($gridpress_menu_tab) &&
                        isset($gridpress_options_menu[$gridpress_menu_tab])
                    ) {

                        if (
                            is_array($gridpress_options_menu[$gridpress_menu_tab]) &&
                            isset($gridpress_options_menu[$gridpress_menu_tab]['sub_menu'])
                        ) {

                            $gridpress_menu_sections = $gridpress_options_menu[$gridpress_menu_tab]['sub_menu'];

                            $gridpress_section_key = array_key_first($gridpress_menu_sections);

                            if (
                                ! empty($gridpress_menu_section) &&
                                isset($gridpress_menu_sections[$gridpress_menu_section])
                            ) {
                                $gridpress_section_key = $gridpress_menu_section;
                            }

                            settings_fields(
                                'gridpress_' . $gridpress_menu_tab . '_' . $gridpress_section_key . '_options_group'
                            );

                            do_settings_sections(
                                'gridpress_' . $gridpress_menu_tab . '_' . $gridpress_section_key . '_options_page'
                            );
                        } else {

                            settings_fields('gridpress_' . $gridpress_menu_tab . '_options_group');

                            do_settings_sections('gridpress_' . $gridpress_menu_tab . '_options_page');
                        }
                    } else {

                        settings_fields('gridpress_general_options_group');

                        do_settings_sections('gridpress_general_options_page');
                    }
                    ?>

                    <button type="submit" class="button button-primary">
                        <?php echo esc_html_x('Save Settings', 'Admin options', 'gridpress-grid-layout-design'); ?>
                    </button>

                </div>
            </form>
            <?php
        }

        /**
         * Menu structure
         */
        public static function gridpress_options_menu()
        {

            return array(
                'general' => esc_html_x('General', 'Admin options menu', 'gridpress-grid-layout-design'),
            );
        }

        /**
         * Popup modal
         */
        public function gridpress_popup_modal_init()
        {
            ?>
            <div id="gridpress-admin-popup" class="gridpress-modal-popup gridpress-popup-hide">
                <div class="gridpress-modal-container">
                    <div class="gridpress-modal-inner-area">
                        <div class="gridpress-modal-content-area">
                            <a href="javascript:;" class="gridpress-popup-closebtn">
                                <i class="gridpress-icon-close"></i>
                            </a>
                            <div class="gridpress-modalinit-con"></div>
                        </div>
                    </div>
                </div>
            </div>
            <?php
        }

        /**
         * Selected post option helper
         */
        public static function gridpress_selected_post_option_arr(
            $gridpress_select_options_list,
            $gridpress_post_id,
            $gridpress_post_type = 'page'
        ) {

            if (
                $gridpress_post_id > 0 &&
                get_post_type($gridpress_post_id) === $gridpress_post_type
            ) {

                $gridpress_select_options_list = array(
                    $gridpress_post_id =>
                    get_the_title($gridpress_post_id) .
                        ' (' .
                        sprintf(
                            /* translators: %d: post ID. */
                            esc_html__('id: %d', 'gridpress-grid-layout-design'),
                            $gridpress_post_id
                        ) .
                        ')',
                );
            }

            return $gridpress_select_options_list;
        }
    }
    new Gridpress_Admin();
}
