<?php

/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://themeatelier.net
 * @since      1.0.0
 *
 * @package    eventful-for-elementor
 * @subpackage eventful-for-elementor/src/Admin
 */

namespace ThemeAtelier\EventfulForElementor\Admin;

use ThemeAtelier\EventfulForElementor\Admin\Helpers\ReviewNotice;
use ThemeAtelier\EventfulForElementor\Admin\Helpers\ThemeAtelier_Offer_Banner;
use ThemeAtelier\EventfulForElementor\Admin\HelpPage\Help;

if (! defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}
class Admin
{

    protected $suffix;
    private $plugin_name;
    private $version;

    private $help_page;

    /**
     * Initialize the class and set its properties.
     *
     * @since    1.0.0
     * @param      string    $plugin_name       The name of this plugin.
     * @param      string    $version    The version of this plugin.
     */
    public function __construct($plugin_name, $version)
    {
        $this->suffix      = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG || defined('WP_DEBUG') && WP_DEBUG ? '' : '.min';
        $this->plugin_name = $plugin_name;
        $this->version = $version;
        add_action('elementor/init', [$this, 'init_category']);
        add_action('elementor/widgets/widgets_registered', [$this, 'init_widgets']);
        add_action('admin_menu', array($this, 'eventful_for_elementor_support_submenu'));
        Help::instance();

        $this->help_page = new Help();

        if (! defined('THEMEATELIER_OFFER_BANNER_LOADED')) {
            define('THEMEATELIER_OFFER_BANNER_LOADED', true);
            ThemeAtelier_Offer_Banner::instance();
        }

        new ReviewNotice();

        $active_plugins = get_option('active_plugins');
        foreach ($active_plugins as $active_plugin) {
            $_temp = strpos($active_plugin, 'eventful-for-elementor.php');
            if (false != $_temp) {
                add_filter('plugin_action_links_' . $active_plugin, array($this, 'eventful_for_elementor_action_links'));
                add_filter('plugin_row_meta', array($this, 'after_eventful_for_elementor_row_meta'), 10, 4);
            }
        }
    }

    /**
     * Init Category Section
     * @since 1.0.0
     */
    public function init_category()
    {
        \Elementor\Plugin::instance()->elements_manager->add_category(
            'ppfe-for-elementor',
            [
                'title' => esc_html__('Eventful', 'eventful-for-elementor'),
            ],
            1
        );
    }

    /**
     * Init Widgets
     * @since 1.0.0
     */
    public function init_widgets()
    {
        $options = get_option('efe_ea_addons');
        $options_grid = isset($options['efe_ea_grid_layout']) ? $options['efe_ea_grid_layout'] : 'on';
        $options_carousel = isset($options['efe_ea_carousel_layout']) ? $options['efe_ea_carousel_layout'] : 'on';
        if ('on' == $options_grid) {
            require_once EVENTFUL_ELEMENTOR_PLUGIN_PATH . 'src/Admin/Widgets/eventful-event-grid.php';
        }
        if ('on' == $options_carousel) {
            require_once EVENTFUL_ELEMENTOR_PLUGIN_PATH . 'src/Admin/Widgets/eventful-event-carousel.php';
        }
    }

    public function eventful_for_elementor_support_submenu()
    {
        add_menu_page(
            __('Eventful For Elementor', 'eventful-for-elementor'),
            'Eventful For Elementor',
            'manage_options',
            'eventful-for-elementor',
            [$this->help_page, 'help_page_callback'],
            'dashicons-calendar',
            6
        );
        do_action('eventful_for_elementor_before_upgrade_pro_menu');
        add_submenu_page(
            'eventful-for-elementor',
            __(
                'Upgrade To Pro!',
                'eventful-for-elementor'
            ),
            sprintf('<span style="color: #35b747;font-weight:bold;" class="eventful-for-elementor-get-pro-text">%s</span>', __('Upgrade To Pro! 👑', 'eventful-for-elementor')),
            'manage_options',
            'https://themeatelier.net/downloads/eventful-for-elementor/?utm_source=eventful_for_elementor_plugin&utm_medium=submenu_page&utm_campaign=regular'
        );
        do_action('eventful_for_elementor_after_upgrade_pro_menu');
    }

    public function eventful_for_elementor_action_links(array $links)
    {
        $url = get_admin_url() . "admin.php?page=eventful-for-elementor";
        $new_links = array(
            sprintf('<a href="' . esc_url($url) . '">' . esc_html__('Settings', 'eventful-for-elementor') . '</a>'),
            sprintf('<a target="_blank" href="https://wordpress.org/support/plugin/eventful-for-elementor/">' . esc_html__('Support', 'eventful-for-elementor') . '</a>'),
            // sprintf('<a style="font-weight: bold;color:rgb(53, 183, 71)" target="_blank" href="https://themeatelier.net/downloads/eventful-for-elementor/?ref=1">' . esc_html__('Go Pro', 'eventful-for-elementor') . '</a>'),
        );
        $links[] = sprintf('<a style="font-weight: bold;color:#35b747" target="_blank" href="https://themeatelier.net/downloads/eventful-for-elementor/?utm_source=eventful_for_elementor_plugin&utm_medium=action_link&utm_campaign=regular">%s</a>', esc_html__('Go Pro!', 'eventful-for-elementor'));
        return array_merge($new_links, $links);
    }

        /**
     * Add plugin row meta link.
     *
     * @since 2.0
     *
     * @param array  $plugin_meta .
     * @param string $file .
     *
     * @return array
     */
    public function after_eventful_for_elementor_row_meta($plugin_meta, $file)
    {

        if (EVENTFUL_ELEMENTOR_BASENAME === $file) {
            $plugin_meta[] = '<a href="' . EVENTFUL_ELEMENTOR_DEMO_URL . '" target="_blank">' . __('Live Demo', 'eventful-for-elementor') . '</a>';
        }

        return $plugin_meta;
    }
}
