<?php

namespace Lknwp\CrowdfundingExtension\PublicView;

use Lknwp\CrowdfundingExtension\Includes\LknwpCrowdfundingExtensionHelper;

final class LknwpCrowdfundingExtensionPublic
{
    /**
     * The ID of this plugin.
     *
     * @since    1.0.0
     * @access   private
     * @var      string    $plugin_name    The ID of this plugin.
     */
    private $plugin_name;

    /**
     * The version of this plugin.
     *
     * @since    1.0.0
     * @access   private
     * @var      string    $version    The current version of this plugin.
     */
    private $version;

    /**
     * Initialize the class and set its properties.
     *
     * @since    1.0.0
     * @param      string    $plugin_name       The name of the plugin.
     * @param      string    $version    The version of this plugin.
     */
    public function __construct($plugin_name, $version)
    {
        $this->plugin_name = $plugin_name;
        $this->version = $version;
    }

    /**
     * Register the stylesheets for the public-facing side of the site.
     *
     * @since    1.0.0
     */
    public function enqueue_styles(): void
    {
        /**
         * This function is provided for demonstration purposes only.
         *
         * An instance of this class should be passed to the run() function
         * defined in Lkn_Wp_Crowdfunding_Extension_Loader as all of the hooks are defined
         * in that particular class.
         *
         * The Lkn_Wp_Crowdfunding_Extension_Loader will then create the relationship
         * between the defined hooks and the functions defined in this
         * class.
         */

        if (class_exists('Elementor\Plugin') && did_action('elementor/loaded')) {
            wp_enqueue_style($this->plugin_name . "-listing-card", plugin_dir_url(__FILE__) . 'css/lknWPCrowdfundingExtensionListingCard.css', array(), $this->version, 'all');

            wp_register_style(
                'lknWpCrowdfundingElementor',
                LKNWP_CROWDFUNDING_EXTENSION_URL . 'Admin/css/lknwpCrowdfundingExtensionElementor.css',
                array(),
                $this->version,
                'all'
            );

            if (! \Elementor\Plugin::$instance->editor->is_edit_mode()) {
                wp_enqueue_style('lknWpCrowdfundingElementor');
            }
        }

        if (has_shortcode(get_post()->post_content, 'lkn_wpcf_simple_registration')) {
            wp_enqueue_style('lkn-registration-styles', plugin_dir_url(__FILE__) . 'css/lknWPCrowdfundingExtensionListingSimpleRegistration.css', array(), $this->version, 'all');
        }
    }

    /**
     * Register the JavaScript for the public-facing side of the site.
     *
     * @since    1.0.0
     */
    public function enqueue_scripts(): void
    {
        /**
         * This function is provided for demonstration purposes only.
         *
         * An instance of this class should be passed to the run() function
         * defined in Lkn_Wp_Crowdfunding_Extension_Loader as all of the hooks are defined
         * in that particular class.
         *
         * The Lkn_Wp_Crowdfunding_Extension_Loader will then create the relationship
         * between the defined hooks and the functions defined in this
         * class.
         */

        if (class_exists('LknwpCrowdfundingExtensionSimpleRegistration') || isset($GLOBALS['lknwp_simple_registration'])) {
            wp_enqueue_script('lkn-wp-crowdfunding-public', plugin_dir_url(__FILE__) . 'js/lknwpCrowdfundingExtensionPublic.js', array('jquery'), $this->version, true);
        }

        if (has_shortcode(get_post()->post_content, 'lkn_wpcf_simple_registration')) {
            wp_enqueue_script(
                'lknwp-wc-simple-registration',
                plugin_dir_url(__FILE__) . 'js/lknwpCrowdfundingExtensionSimpleRegistration.js',
                array(),
                $this->version,
                false
            );

            wp_localize_script(
                'lknwp-wc-simple-registration',
                'lknwpAjax',
                array(
                    'ajax_url' => admin_url('admin-ajax.php'),
                )
            );
        }

        if (class_exists('Elementor\Plugin') && did_action('elementor/loaded')) {
            wp_register_script(
                'lknwp-wc-listing-public',
                plugin_dir_url(__FILE__) . 'js/lknwpCrowdfundingExtensionListing.js',
                array('jquery'),
                $this->version,
                false
            );

            wp_enqueue_script('lknwp-wc-listing-public');

            wp_register_script(
                'lknwp-wc-listing-public-load-more',
                plugin_dir_url(__FILE__) . 'js/lknwpCrowdfundingExtensionListingLoadMore.js',
                array('jquery'),
                $this->version,
                false
            );

            wp_enqueue_script('lknwp-wc-listing-public-load-more');

            $settings = LknwpCrowdfundingExtensionHelper::get_messages();

            wp_localize_script('lknwp-wc-listing-public-load-more', 'lknwp_settings', $settings);

            wp_enqueue_script(
                'lknwp-wc-payment',
                plugin_dir_url(__FILE__) . 'js/lknwpCrowdfundingExtensionPayment.js',
                array('jquery'),
                $this->version,
                false
            );

            wp_localize_script(
                'lknwp-wc-payment',
                'lknWpCrowdfundingParams',
                LknwpCrowdfundingExtensionHelper::get_translations()
            );

            $finalization_url = wc_get_checkout_url();

            wp_localize_script(
                'lknwp-wc-payment',
                'lknwpGlobal',
                array(
                    'finalization_url' => $finalization_url
                )
            );

            wp_enqueue_script(
                'lknwp-elevate-zoom',
                LKNWP_CROWDFUNDING_EXTENSION_URL . 'Admin/js/ElevateZoom-3.0.8.min.js',
                array('jquery'),
                $this->version,
                false
            );

            wp_register_script(
                'lknWpCrowdfundingElementor',
                LKNWP_CROWDFUNDING_EXTENSION_URL . 'Admin/js/lknwpCrowdfundingExtensionElementor.js',
                array('jquery', 'lknwp-elevate-zoom'),
                $this->version,
                false
            );

            wp_localize_script(
                'lknWpCrowdfundingElementor',
                'lknWpCrowdfundingParams',
                LknwpCrowdfundingExtensionHelper::get_translations()
            );

            if (! \Elementor\Plugin::$instance->editor->is_edit_mode()) {
                wp_enqueue_script('lknWpCrowdfundingElementor');
            }
        }
    }
}
