<?php
// Exit if accessed directly.
defined('ABSPATH') || exit;
class Party_Corp_Dashboard
{
    private static $party_corp_instance;
    /**
     * The pro_status of theme.
     *
     * @var string $party_corp_pro_status The pro_status.
     */
    public $party_corp_pro_status = false;
    public static function instance()
    {
        if (is_null(self::$party_corp_instance)) {
            self::$party_corp_instance = new self();
        }
        return self::$party_corp_instance;
    }
    private function __construct()
    {
        $this->setup_hooks();
    }
    private function setup_hooks()
    {
        add_action('admin_menu', array($this, 'create_menu'));
        add_action('in_admin_header', array($this, 'hide_admin_notices'));
    }
    /**
     * Theme Url
     *
     * @access private
     * @var string $theme_url
     * @since 1.0.0
     */
    private $party_corp_theme_url = 'https://www.themebee.net/products/party-corp/';
    /**
     * Pro Theme Url
     *
     * @access private
     * @var string $party_corp_pro_theme_url
     * @since 1.0.0
     */
    private $party_corp_pro_theme_url = 'https://www.themebee.net/products/ecommerce-wordpress-theme/';
    /**
     * Documentation Url
     *
     * @access private
     * @var string $party_corp_doc_url
     * @since 1.0.0
     */
    private $party_corp_doc_url = 'https://preview.themebee.net/guide/party-corp-free/';
    /**
     * Premium Documentation Url
     *
     * @access private
     * @var string $party_corp_pro_doc_url
     * @since 1.0.0
     */
    private $party_corp_pro_doc_url = 'https://preview.themebee.net/guide/salecraft-ecommerce-pro/';
    /**
     * Preview Url
     *
     * @access private
     * @var string $party_corp_preview_url
     * @since 1.0.0
     */
    private $party_corp_preview_url = 'https://preview.themebee.net/salecraft-ecommerce-pro/';
    /**
     * Bundle Url
     *
     * @access private
     * @var string $party_corp_bundle_url
     * @since 1.0.0
     */
    private $party_corp_bundle_url = 'https://www.themebee.net/products/WordPress-theme-bundle/';
    /**
     * Misc Support Url
     *
     * @access private
     * @since 1.0.0
     */
    private $party_corp_community_url = 'https://WordPress.org/support/theme/party-corp/';
    public function create_menu()
    {
        if (!is_child_theme()) {
            $theme = wp_get_theme();
        } else {
            $theme = wp_get_theme()->parent();
        }
        /* translators: %s: Theme Name. */
        $theme_page_name = sprintf(esc_html__('%s', 'party-corp'), $theme->Name);
        add_theme_page(
            $theme_page_name,
            $theme_page_name,
            'edit_theme_options',
            'party-corp',
            array(
                $this,
                'dashboard_page',
            )
        );
    }
    /**
     * Hide admin notices from BlockArt admin pages.
     *
     * @since 1.0.0
     */
    public function hide_admin_notices()
    {
        // Bail if we're not on a Party Corp screen or page.
        if (empty($_REQUEST['page']) || false === strpos(sanitize_text_field(wp_unslash($_REQUEST['page'])), 'party-corp')) { // phpcs:ignore WordPress.Security.NonceVerification
            return;
        }
        global $wp_filter;
        $ignore_notices = apply_filters('party_corp_ignore_hide_admin_notices', array());
        foreach (array('user_admin_notices', 'admin_notices', 'all_admin_notices') as $wp_notice) {
            if (empty($wp_filter[$wp_notice])) {
                continue;
            }
            $hook_callbacks = $wp_filter[$wp_notice]->callbacks;
            if (empty($hook_callbacks) || !is_array($hook_callbacks)) {
                continue;
            }
            foreach ($hook_callbacks as $priority => $hooks) {
                foreach ($hooks as $name => $callback) {
                    if (!empty($name) && in_array($name, $ignore_notices, true)) {
                        continue;
                    }
                    if (
                        !empty($callback['function']) &&
                        !is_a($callback['function'], '\Closure') &&
                        isset($callback['function'][0], $callback['function'][1]) &&
                        is_object($callback['function'][0]) &&
                        in_array($callback['function'][1], $ignore_notices, true)
                    ) {
                        continue;
                    }
                    unset($wp_filter[$wp_notice]->callbacks[$priority][$name]);
                }
            }
        }
    }
    public function dashboard_page()
    {
        if (!is_child_theme()) {
            $theme = wp_get_theme();
        } else {
            $theme = wp_get_theme()->parent();
        }
        $party_corp_admin_ur = admin_url();
        $tabs = apply_filters(
            'party_corp_dashboard_tabs',
            array(
                'dashboard' => array(
                    'name' => esc_html__('Dashboard', 'party-corp'),
                    'callback' => function () {
                        include __DIR__ . '/info-tabs/dashboard.php';
                    },
                ),
                'free-vs-pro' => array(
                    'name' => esc_html__('Free Vs Pro', 'party-corp'),
                    'callback' => function () {
                        include __DIR__ . '/info-tabs/free-vs-pro.php';
                    },
                ),
                'help' => array(
                    'name' => esc_html__('Help Center', 'party-corp'),
                    'callback' => function () {
                        include __DIR__ . '/info-tabs/help-center.php';
                    },
                ),
            )
        )
        ?>
        <div class="wrap">
            <div class="party-corp-dashboard-area">
                <div class="party-corp-dashboard-header">
                    <a class="party-corp-logo" href="<?php echo esc_url($this->party_corp_theme_url); ?>"
                       target="_blank">
                        <img class="party-corp-premium-badge"
                             src="<?php echo esc_url(get_template_directory_uri() . '/inc/dashboard/images/party-corp-logo.png'); ?>"
                             alt="<?php esc_attr_e('Party Corp', 'party-corp'); ?>">
                        <span class="party-corp-theme-version">
                            <?php
                            echo esc_html($theme->Version);
                            ?>
                        </span>
                    </a>
                    <nav class="dashboard-header-nav">
                        <ul class="dashboard-primary-menu">
                            <?php
                            foreach ($tabs as $id => $tab) :
                                if (!is_callable($tab['callback'])) {
                                    continue;
                                }
                                ?>
                                <li class="menu-item menu-item-<?php echo esc_attr($id); ?>">
                                    <a id="<?php echo esc_attr($id); ?>"
                                       href=<?php echo esc_url("{$party_corp_admin_ur}themes.php?page=party-corp&tab=$id"); ?>><?php echo esc_html($tab['name']); ?></a>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    </nav>
                    <div class="dashboard-header-extras">
                        <div class="dashboard-button-group">
                            <?php if (!$this->party_corp_pro_status) { ?>
                                <a href="<?php echo esc_url($this->party_corp_pro_theme_url); ?>" class="button button-secondary dashboard-button dashboard-button-primary" target="_blank">
                                    <?php esc_html_e('Go Pro', 'party-corp'); ?>
                                </a>
                            <?php } ?>
                            <button type="button" id="dashboard-changelog-button" class="button button-secondary dashboard-button dashboard-button-secondary">
                                <?php esc_html_e('Changelog', 'party-corp'); ?>
                            </button>
                        </div>
                    </div>
                </div>
                <?php
                $current_tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : 'dashboard';
                $current_tab = in_array($current_tab, array_keys($tabs), true) ? $current_tab : 'dashboard';
                $callback = $tabs[$current_tab]['callback'];
                call_user_func($callback);
                ?>
            </div>
        </div>
        <div id="party-corp-dashboard-offcanvas" class="party-corp-dashboard-offcanvas">
            <div class="dialog-head">
                <h3><?php esc_html_e('Latest Updates', 'party-corp'); ?></h3>
                <div id="dialog-close" class="dialog-close">
                    <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none">
                        <path d="M10.052 9.34082L16.277 3.11582C16.577 2.81582 16.577 2.36582 16.277 2.06582C15.977 1.76582 15.5269 1.76582 15.227 2.06582L9.00195 8.29082L2.77695 2.06582C2.47695 1.76582 2.02695 1.76582 1.72695 2.06582C1.42695 2.36582 1.42695 2.81582 1.72695 3.11582L7.95195 9.34082L1.72695 15.5658C1.42695 15.8658 1.42695 16.3158 1.72695 16.6158C1.87695 16.7658 2.02695 16.8408 2.25195 16.8408C2.47695 16.8408 2.62695 16.7658 2.77695 16.6158L9.00195 10.3908L15.227 16.6158C15.377 16.7658 15.602 16.8408 15.752 16.8408C15.902 16.8408 16.127 16.7658 16.277 16.6158C16.577 16.3158 16.577 15.8658 16.277 15.5658L10.052 9.34082Z" fill="#999999"/>
                    </svg>
                </div>
            </div>
            <div class="dialog-content">
                <?php
                $party_corp_changelog = new Party_Corp_Changelog_Parser();
                $party_corp_changelog_data = $party_corp_changelog->get_items();

                if ($party_corp_changelog_data) {
                    echo '<div class="party-corp-changelog">';
                    foreach ($party_corp_changelog_data as $party_corp_entry) {
                        echo '<div class="party-corp-changelog-list-item">';
                        echo '<div class="party-corp-changelog-list-head">';
                        echo '<h4 class="party-corp-changelog-version">Version: ' . esc_html($party_corp_entry['version']) . '</h4>';
                        echo '<p class="party-corp-changelog-date">' . esc_html($party_corp_entry['date']) . '</p>';
                        echo '</div>';
                        echo '<div class="party-corp-changelog-change">';
                        foreach ($party_corp_entry['changes'] as $party_corp_changes) {
                            echo '<div class="party-corp-changelog-change-item">';
                            echo '<div class="party-corp-changelog-change-list">';

                            foreach ($party_corp_changes as $party_corp_change) {
                                echo '<p class="party-corp-changelog-change-desc">' . esc_html($party_corp_change) . '</p>';
                            }

                            echo '</div>';
                            echo '</div>';
                        }
                        echo '</div>';
                        echo '</div>';
                    }
                    echo '</div>';
                } else {
                    echo '<p>No changelog data available.</p>';
                }
                ?>
            </div>
        </div>
        <?php
    }
}
Party_Corp_Dashboard::instance();
