<?php
namespace Wpupgrader\Libraries\WordPress\Plugins\Core\Settings;

use Wpupgrader\Libraries\WordPress\Plugins\Core\Settings;

abstract class Page extends Settings
{
    abstract protected function page_body_init();

    public function addSettingsPage()
    {
        \add_action('admin_menu', [$this, 'add_plugin_page']);
        \add_action('admin_init', [$this, 'page_init']);
    }

    public function page_init()
    {
        \register_setting($this->getSettingsKey(), $this->getSettingsKey(), [$this, 'sanitize']);
        \add_settings_section($this->getSettingsKey(), '', '', $this->getSettingsKey());
        $this->page_body_init();
    }

    /**
     * Add options page
     */
    public function sanitize($value)
    {
        return $value;
    }

    public function getSettingsDescription()
    {
    }

    /**
     * Options page callback
     */
    public function create_admin_page()
    {
        ?>
		<div class="wrap">
			<h2><?php echo $this->getSettingsName(); ?></h2>
		</div>
        <?php if (!\gdprConsentPluginIsProEdition()): ?>
		<div class="wrap">
			<link href="https://fonts.googleapis.com/css2?family=Heebo:wght@400&display=swap" rel="stylesheet">
			<div class="gdprc-upgrade-to-pro">
				<h3>Upgrade to GDPR Consent Pro!</h3>
				<p>Upgrade to GDPR Consent Pro and get full access to all these features:</p>
				<ul>
					<li>Direct activation of Google Analytics when user consents without page reload!</li>
					<li>Full support for multilingual (WPML) and multi-site (WPMS)!</li>
					<li>Add custom scripts to your consents, like tracking codes and Facebook pixels!</li>
					<li>Advanced configuration, custom CSS rules and more...</li>
				</ul>
				<a class="gdprc-upgrade-to-pro--button" href="https://www.wpupgrader.com/wordpress-plugins/gdpr-consent/#pricing" target='_blank'>Get PRO Now!</a>
				<a class="gdprc-upgrade-to-pro--button" href="https://www.wpupgrader.com/wordpress-plugins/gdpr-consent/" target='_blank' style="background: #6EC3D0;">Learn more</a>
			</div>
		</div>
    <?php endif; ?>
		<div class="wrap">
            <?php $this->getSettingsDescription(); ?>
		</div>
		<div class="wrap">
			<form method="post" action="options.php" class="gdprc_consent_tabs" autocomplete="off">
                <?php
                \settings_fields($this->getSettingsKey());
                $this->do_tabbed_settings_sections($this->getSettingsKey());
                \submit_button();
                ?>
			</form>
		</div>
        <?php
    }

    public function do_tabbed_settings_sections($page)
    {
        global $wp_settings_sections, $wp_settings_fields;

        if (!isset($wp_settings_sections[$page])) {
            return;
        }

        $firstTab = true;
        foreach ((array)$wp_settings_sections[$page] as $section) {
            echo "<input id='{$section['id']}' type='radio' name='gdprc_consent_tabs' ".($firstTab ? 'checked' : '')." />\n";

            if ($section['title']) {
                echo "<label for='{$section['id']}' class='tab'>{$section['title']}</label>\n";
            }

            if ($section['callback']) {
                \call_user_func($section['callback'], $section);
            }

            if (!isset($wp_settings_fields) || !isset($wp_settings_fields[$page]) || !isset($wp_settings_fields[$page][$section['id']])) {
                continue;
            }
            echo '<div class="tab-container tab-'.$section['id'].'"><table class="form-table">';
            \do_settings_fields($page, $section['id']);
            echo '</table></div>';

            $firstTab = false;
        }
    }

    public function callback($args)
    {
        $classname = '\\Wpupgrader\\Libraries\\WordPress\\Plugins\\Core\\Settings\\Page\\Callback\\'.\ucfirst($args['type']);
        unset($args['type']);
        $callback = new $classname($this, $args);
        return $callback->callbackAction();
    }
}