<?php

/**
 * The file of the EventfulForElementor class.
 *
 * @link       https://themeatelier.net
 * @since      1.0.0
 *
 * @package EventfulForElementor
 */

namespace ThemeAtelier\EventfulForElementor\Includes;
use ThemeAtelier\EventfulForElementor\Admin\Admin;
use ThemeAtelier\EventfulForElementor\Frontend\Frontend;

// don't call the file directly.
if (! defined('ABSPATH')) {
    exit;
}
/**
 * The main class of the plugin.
 *
 * Handle all the class and methods of the plugin.
 *
 * @author     ThemeAtelier <themeatelierbd@gmail.com>
 */
class EventfulForElementor
{
    /**
     * The unique identifier of this plugin.
     *
     * @since    1.0.0
     * @access   protected
     * @var      string    $plugin_name    The string used to uniquely identify this plugin.
     */
    protected $plugin_name = EVENTFUL_ELEMENTOR_BASENAME;

    /**
     * Plugin version
     *
     * @since    1.0.0
     * @access   protected
     * @var string
     */
    protected $version;

    /**
     * Plugin slug
     *
     * @since    1.0.0
     * @access   protected
     * @var string
     */
    protected $plugin_slug;

    /**
     * Main Loader.
     *
     * The loader that's responsible for maintaining and registering all hooks that empowers
     * the plugin.
     *
     * @since    1.0.0
     * @access   protected
     * @var object
     */
    protected $loader;

    protected $suffix;

    /**
     * Constructor for the ChatWhatsappPro class.
     *
     * Sets up all the appropriate hooks and actions within the plugin.
     */
    public function __construct()
    {
        $this->version     = EVENTFUL_ELEMENTOR_VERSION;
        $this->plugin_slug = 'eventful-for-elementor';
        $this->load_dependencies();
        $this->define_admin_hooks();
        $this->define_public_hooks();
        add_action('wp_enqueue_scripts', [$this, 'scripts_styles']);
        add_action('init', [$this, 'i18n']);
        add_action('plugins_loaded', [$this, 'init']);
        add_action('activated_plugin', [$this, 'redirect_to']);

        $this->suffix      = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG || defined('WP_DEBUG') && WP_DEBUG ? '' : '.min';


    }

    private function load_dependencies()
    {
        /**
         * The class responsible for orchestrating the actions and filters of the
         * core plugin.
         */
        $this->loader = new Loader();
    }

    /**
     * Define constant if not already set
     *
     * @since 2.2.0
     *
     * @param string      $name Define constant.
     * @param string|bool $value Define constant.
     */
    public function define($name, $value)
    {
        if (!defined($name)) {
            define($name, $value);
        }
    }

    /**
     * Register all of the hooks related to the admin area functionality
     * of the plugin.
     *
     * @since    1.0.0
     * @access   private
     */
    private function define_admin_hooks()
    {
        new Admin(EVENTFUL_ELEMENTOR_BASENAME, EVENTFUL_ELEMENTOR_VERSION);
    }

    /**
     * Register all of the hooks related to the public-facing functionality
     * of the plugin.
     *
     * @since    1.0.0
     * @access   private
     */
    private function define_public_hooks()
    {
        new Frontend(EVENTFUL_ELEMENTOR_BASENAME, EVENTFUL_ELEMENTOR_VERSION);
    }

    /**
     * Run the loader to execute all of the hooks with WordPress.
     *
     * @since    1.0.0
     */
    public function run()
    {
        $this->loader->run();
    }

    /**
     * Load Scripts & Styles
     * @since 1.0.0
     */
    public function scripts_styles()
    {
        wp_register_script('swiper-bundle', EVENTFUL_ELEMENTOR_URL_ASSETS . 'js/swiper-bundle' . $this->suffix . '.js', array('jquery'), EVENTFUL_ELEMENTOR_VERSION, true);
        wp_register_script('bx-slider', EVENTFUL_ELEMENTOR_URL_ASSETS . 'js/jquery.bxslider' . $this->suffix . '.js', array('jquery'), EVENTFUL_ELEMENTOR_VERSION, true);
        wp_register_script('efpe-lazyload', EVENTFUL_ELEMENTOR_URL_ASSETS . 'js/efpe-lazyload' . $this->suffix . '.js', array('swiper-bundle', 'bx-slider', 'jquery'), EVENTFUL_ELEMENTOR_VERSION, true);
        wp_register_script('grid_scripts', EVENTFUL_ELEMENTOR_URL_ASSETS . 'js/grid-scripts' . $this->suffix . '.js', array('swiper-bundle', 'bx-slider', 'jquery'), EVENTFUL_ELEMENTOR_VERSION, true);
        wp_register_script('carousel_scripts', EVENTFUL_ELEMENTOR_URL_ASSETS . 'js/carousel-scripts' . $this->suffix . '.js', array('swiper-bundle', 'bx-slider', 'jquery'), EVENTFUL_ELEMENTOR_VERSION, true);

        wp_register_style('fontawesome', EVENTFUL_ELEMENTOR_URL_ASSETS . 'css/fontawesome' . $this->suffix . '.css', array(), EVENTFUL_ELEMENTOR_VERSION, 'all');
        wp_register_style('bx-slider', EVENTFUL_ELEMENTOR_URL_ASSETS . 'css/jquery.bxslider' . $this->suffix . '.css', array(), EVENTFUL_ELEMENTOR_VERSION, 'all');
        wp_register_style('swiper-bundle', EVENTFUL_ELEMENTOR_URL_ASSETS . 'css/swiper-bundle' . $this->suffix . '.css', array(), EVENTFUL_ELEMENTOR_VERSION, 'all');
        if (!is_rtl()) {
            wp_register_style('eventful-main', EVENTFUL_ELEMENTOR_URL_ASSETS . 'css/eventful.main' . $this->suffix . '.css', [], EVENTFUL_ELEMENTOR_VERSION, 'all');
        }
        wp_register_style('eventful-main', EVENTFUL_ELEMENTOR_URL_ASSETS . 'css/eventful.main' . $this->suffix . '.css', [], EVENTFUL_ELEMENTOR_VERSION, 'all');

        wp_register_style('carousel-custom', EVENTFUL_ELEMENTOR_URL_ASSETS . 'css/carousel-custom' . $this->suffix . '.css', [], EVENTFUL_ELEMENTOR_VERSION, 'all');


        if (class_exists('Elementor')) {
            \Elementor\Plugin::instance()->frontend->register_styles();
        }
        wp_localize_script(
            'grid_scripts',
            'ta_efpe',
            array(
                'ajaxurl' => admin_url('admin-ajax.php'),
                'nonce'   => wp_create_nonce('ta_efpe_nonce'),
            )
        );
        wp_localize_script(
            'carousel_scripts',
            'ta_efpe',
            array(
                'ajaxurl' => admin_url('admin-ajax.php'),
                'nonce'   => wp_create_nonce('ta_efpe_nonce'),
            )
        );
    }

    /**
     * Load Text Domain
     * @since 1.0.0
     */
    public function i18n()
    {
        load_plugin_textdomain('eventful-for-elementor', false, dirname(plugin_basename(__FILE__)) . '/languages');
    }

    /**
     * Initialize the plugin
     * @since 1.0.0
     */
    public function init()
    {
        // Check if the ELementor installed and activated
        if (!did_action('elementor/loaded')) {
            add_action('admin_notices', [$this, 'admin_notice_missing_main_plugin']);
            return;
        }

        if (!version_compare(ELEMENTOR_VERSION, MINIMUM_ELEMENTOR_VERSION, '>=')) {
            add_action('admin_notices', [$this, 'admin_notice_minimum_elementor_version']);
            return;
        }

        if (!version_compare(PHP_VERSION, MINIMUM_PHP_VERSION, '>=')) {
            add_action('admin_notices', [$this, 'admin_notice_minimum_php_version']);
            return;
        }

        if (!class_exists('Tribe__Events__Main')) {
            add_action('admin_notices', [$this, 'tribe_event_main_fail_load']);

            return;
        }
    }

    /**
     * Admin Notice
     * Warning when the site doesn't have Elementor installed or activated
     * @since 1.0.0
     */
    public function admin_notice_missing_main_plugin()
    {
        $message = sprintf(
            /* translators: 1: Plugin name, 2: Required plugin name */
            esc_html__('"%1$s" requires "%2$s" to be installed and activated', 'eventful-for-elementor'),
            '<strong>' . esc_html__('Eventful For Elementor', 'eventful-for-elementor') . '</strong>',
            '<strong>' . esc_html__('Elementor', 'eventful-for-elementor') . '</strong>'
        );

        printf('<div class="notice notice-warning is-dimissible"><p>%1$s</p></div>', wp_kses_post($message));
    }

    /**
     * Admin Notice
     * Warning when the site doesn't have a minimum required Elementor version.
     * @since 1.0.0
     */
    public function admin_notice_minimum_elementor_version()
    {
        $message = sprintf(
            /* translators: 1: Plugin name, 2: Required plugin name, 3: Required version number */
            esc_html__('"%1$s" requires "%2$s" version %3$s or greater', 'eventful-for-elementor'),
            '<strong>' . esc_html__('Eventful For Elementor', 'eventful-for-elementor') . '</strong>',
            '<strong>' . esc_html__('Elementor', 'eventful-for-elementor') . '</strong>',
            MINIMUM_ELEMENTOR_VERSION
        );
        printf('<div class="notice notice-warning is-dimissible"><p>%1$s</p></div>', wp_kses_post($message));
    }

    /**
     * Admin Notice
     * Warning when the site doesn't have a minimum required PHP version.
     * @since 1.0.0
     */
    public function admin_notice_minimum_php_version()
    {
        $message = sprintf(
            /* translators: 1: Plugin name, 2: Required software name (PHP), 3: Required version number */
            esc_html__('"%1$s" requires "%2$s" version %3$s or greater', 'eventful-for-elementor'),
            '<strong>' . esc_html__('Eventful For Elementor', 'eventful-for-elementor') . '</strong>',
            '<strong>' . esc_html__('PHP', 'eventful-for-elementor') . '</strong>',
            MINIMUM_PHP_VERSION
        );
        printf('<div class="notice notice-warning is-dimissible"><p>%1$s</p></div>', wp_kses_post($message));
    }

    public function print_error($message)
    {
        if (!$message) {
            return;
        }
        // PHPCS - $message should not be escaped
        echo '<div class="error">' . wp_kses_post($message) . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    }

    public function tribe_event_main_fail_load()
    {
        $screen = get_current_screen();
        if (isset($screen->parent_file) && 'plugins.php' === $screen->parent_file && 'update' === $screen->id) {
            return;
        }

        $plugin = 'the-events-calendar/the-events-calendar.php';

        if (efpe_is_the_events_calendar_installed()) {
            if (!current_user_can('activate_plugins')) {
                return;
            }

            $activation_url = wp_nonce_url('plugins.php?action=activate&amp;plugin=' . $plugin . '&amp;plugin_status=all&amp;paged=1&amp;s', 'activate-plugin_' . $plugin);

            $message = '<h3>' . esc_html__('Eventful For Elementor plugin requires activate The Events Calendar plugin', 'eventful-for-elementor') . '</h3>';
            $message .= '<p>' . esc_html__('Activate The Events Calendar plugin to start using all of Eventful For Elementor plugin’s features.', 'eventful-for-elementor') . '</p>';
            $message .= '<p>' . sprintf('<a href="%s" class="button-primary">%s</a>', $activation_url, esc_html__('Activate Now', 'eventful-for-elementor')) . '</p>';
        } else {
            if (!current_user_can('install_plugins')) {
                return;
            }

            $install_url = wp_nonce_url(self_admin_url('update.php?action=install-plugin&plugin=the-events-calendar'), 'install-plugin_the-events-calendar');

            $message = '<h3>' . esc_html__('Eventful For Elementor plugin requires installing The Events Calendar plugin', 'eventful-for-elementor') . '</h3>';
            $message .= '<p>' . esc_html__('Install and activate The Events Calendar plugin to start using all of Eventful For Elementor plugin’s features.', 'eventful-for-elementor') . '</p>';
            $message .= '<p>' . sprintf('<a href="%s" class="button-primary">%s</a>', $install_url, esc_html__('Install Now', 'eventful-for-elementor')) . '</p>';
        }

        $this->print_error($message);
    }

    public function redirect_to($plugin)
    {
        if (EVENTFUL_ELEMENTOR_BASENAME === $plugin) {
            $redirect_url = admin_url('admin.php?page=eventful-for-elementor');
            exit(wp_kses_post(wp_safe_redirect($redirect_url)));
        }
    }
}
