<?php

namespace MailOptin\Core\Admin\SettingsPage;

use W3Guy\Custom_Settings_Page_Api;

use function MailOptin\Core\get_capability;

class LeadBank extends AbstractSettingsPage
{
    protected $settingsInstance;

    public function __construct()
    {
        add_action('mailoptin_admin_settings_page_pre', function ($active_menu) {

            if ($active_menu === 'leadbank') {

                $this->settingsInstance = Custom_Settings_Page_Api::instance();
                $this->settingsInstance->option_name('mo_leads');
                $this->settingsInstance->page_header(__('Leads (Submissions)', 'mailoptin'));
                $this->settingsInstance->remove_h2_header();
                $this->set_settings_page_instance($this->settingsInstance);
            }
        });

        add_action('mailoptin_register_menu_page', array($this, 'register_settings_page'), 30);
    }

    public function register_settings_page()
    {
        $hook = add_submenu_page(
            MAILOPTIN_SETTINGS_SETTINGS_SLUG,
            __('Leads (Submissions) - MailOptin', 'mailoptin'),
            __('Leads', 'mailoptin'),
            get_capability(),
            MAILOPTIN_LEAD_BANK_SETTINGS_SLUG,
            array($this, 'admin_page_callback')
        );

        add_action("load-$hook", array($this, 'screen_option'));

        add_action('mailoptin_admin_settings_submenu_page_leadbank', [$this, 'settings_admin_page']);
    }

    public function default_header_menu()
    {
        return 'leadbank';
    }

    /**
     * Screen options
     */
    public function screen_option()
    {
        $option = 'per_page';
        $args   = [
            'label'   => __('Leads', 'mailoptin'),
            'default' => 10,
            'option'  => 'conversions_per_page',
        ];

        add_screen_option($option, $args);

        do_action('mailoptin_leadbank_settings_page_screen_option');
    }

    public function settings_admin_page()
    {
        if ( ! apply_filters('mailoptin_enable_leadbank', false)) {
            add_filter('wp_cspa_main_content_area', array($this, 'upsell_settings_page'), 10, 2);
        }

        do_action("mailoptin_leadbank_settings_page");

        $this->settingsInstance->build(true);
    }

    public function upsell_settings_page($content, $option_name)
    {
        if ($option_name != 'mo_leads') {
            return $content;
        }

        $url = 'https://mailoptin.io/pricing/?utm_source=wp_dashboard&utm_medium=upgrade&utm_campaign=leadbank_btn';

        ob_start();
        ?>
        <div class="mo-settings-page-disabled">
            <div class="mo-upgrade-plan">
                <div class="mo-text-center">
                    <div class="mo-lock-icon"></div>
                    <h1><?php _e('Leads are Locked', 'mailoptin'); ?></h1>
                    <p>
                        <?php printf(
                            __('Leads are all subscribers that sign up to your email list on your site.', 'mailoptin'),
                            '<strong>',
                            '</strong>');
                        ?>
                    </p>
                    <p>
                        <?php _e('Your current plan does not include this feature.', 'mailoptin');
                        ?>
                    </p>
                    <div class="moBtncontainer mobtnUpgrade">
                        <a target="_blank" href="<?= $url; ?>" class="mobutton mobtnPush mobtnGreen">
                            <?php _e('Upgrade to Unlock', 'mailoptin'); ?>
                        </a>
                    </div>
                </div>
            </div>
            <img src="<?php echo MAILOPTIN_ASSETS_URL; ?>images/leadbankscreenshot.png">
        </div>
        <?php

        return ob_get_clean();
    }

    /**
     * @return LeadBank
     */
    public static function get_instance()
    {
        static $instance = null;

        if (is_null($instance)) {
            $instance = new self();
        }

        return $instance;
    }
}