<?php
/**
 * Author: Rymera Web Co.
 *
 * @package SAAI\Classes
 */

namespace SAAI\Classes;

use SAAI\Abstracts\Admin_Page;
use SAAI\Helpers\Connect;
use SAAI\Traits\Singleton_Trait;
use SAAI\Classes\Admin_Menu_Handler;

/**
 * Settings_Page class.
 *
 * @since 1.0.0
 */
class Settings_Page extends Admin_Page {

    use Singleton_Trait;

    /**
     * Holds the class instance object.
     *
     * @since 1.0.2
     * @var Settings_Page $instance
     */
    protected static $instance;

    /**
     * Admin menu page slug.
     *
     * @since 1.0.0
     */
    protected function init() {

        $this->page_title  = __( 'StoreAgent AI Settings', 'storeagent-ai-for-woocommerce' );
        $this->menu_title  = __( 'Settings', 'storeagent-ai-for-woocommerce' );
        $this->capability  = 'manage_options';
        $this->menu_slug   = 'storeagent-settings';
        $this->template    = 'settings-page.php';
        $this->parent_slug = Admin_Menu_Handler::PARENT_SLUG;
    }

    /**
     * Highlight active admin menu.
     *
     * @param string $parent_file Parent file.
     *
     * @return string
     */
    public function highlight_active_admin_menu( $parent_file ) {

        global $submenu_file, $pagenow;

        $definition = array(
            'page' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
        );
        $qs         = filter_input_array( INPUT_GET, $definition );

        // This is the Settings page.
        if ( 'admin.php' === $pagenow && 'storeagent-settings' === $qs['page'] ) {
            $submenu_file = 'storeagent-settings'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
            $parent_file  = Admin_Menu_Handler::PARENT_SLUG;
        }

        return $parent_file;
    }

    /**
     * Add hook handlers for rendering the admin page.
     *
     * @since 1.0.0
     * @return void
     */
    public function run() {

        if ( ! Connect::is_connected() ) {
            /***************************************************************************
             * Bail early
             ***************************************************************************
             *
             * We don't want to run the admin page if the store is not connected to
             * the StoreAgent API service.
             */
            return;
        }

        parent::run();
        add_filter( 'parent_file', array( $this, 'highlight_active_admin_menu' ) );
    }
}
