<?php
/**
 *
 * @package     productive-forms
 * @author      productiveminds.com
 * @copyright   productiveminds.com
 */

if ( !defined('ABSPATH') ) {
	die();
}

if ( !function_exists( 'productive_elementor_add_widget_category' ) ) {
    /**
     * Register a new Elementor Category for listing ProductiveMinds' Elementor Widgets
     * @param type $elements_manager
     */
    function productive_elementor_add_widget_category( $elements_manager ) {
        $elements_manager->add_category(
            'productiveminds',
            [
                    'title' => esc_html__( 'ProductiveMinds', 'productive-forms' ),
                    'icon' => 'fa fa-heart',
            ]
        );
    }
    add_action( 'elementor/elements/categories_registered', 'productive_elementor_add_widget_category' );
}

if ( !function_exists( 'productive_elementor_elementor_dynamic_tag_group' ) ) {
    // Start:: Dynamic Tags
    /**
     * Dynamic Tags Groups
     * @param type $dynamic_tags_manager
     */
    function productive_elementor_elementor_dynamic_tag_group( $dynamic_tags_manager ) {
        $dynamic_tags_manager->register_group(
            'productive_dynamic_tags',
            [
                'title' => esc_html__( 'Productive Dynamic Tags', 'productive-forms' )
            ]
        );
    }
    add_action( 'elementor/dynamic_tags/register', 'productive_elementor_elementor_dynamic_tag_group' );
}

/**
 * Widgets
 * @param type $widgets_manager
 */
function productive_forms_elementor_register_widgets( $widgets_manager ) {
    
    if( productive_global_is_pass_addons_active() ) {
        if( class_exists( 'Pass_Addons_Common_Settings' ) && pass_addons_performance_is_active_page_builder_elementor() ) {
            // Contact Us Page
            if ( 'checked' == Pass_Addons_Common_Settings::pass_addons_get_a_elementor_widget_status( 'contact_us_page', 'checked' ) ) {
                require_once( plugin_dir_path( __FILE__ ) . 'widgets/contact-us-page/productive-elementor-widget-contact-us-page.php' );
                $widgets_manager->register( new \Productive_Elementor_Widget_Contact_Us_Page() );
            }
            // Google Maps
            if ( 'checked' == Pass_Addons_Common_Settings::pass_addons_get_a_elementor_widget_status( 'google_maps', 'checked' ) ) {
                require_once( plugin_dir_path( __FILE__ ) . 'widgets/google-map/productive-elementor-widget-google-map.php' );
                $widgets_manager->register( new \Productive_Elementor_Widget_Google_Maps() );
            }
            // Newsletter Element
            if ( 'checked' == Pass_Addons_Common_Settings::pass_addons_get_a_elementor_widget_status( 'newsletter', 'checked' ) ) {
                require_once( plugin_dir_path( __FILE__ ) . 'widgets/newsletter-element/productive-elementor-widget-newsletter-element.php' );
                $widgets_manager->register( new \Productive_Elementor_Widget_Newsletter_Element() );
            }
        }
    } else if( productive_global_is_ravolat_addons_active() ) {
        if( class_exists( 'Ravolat_Addons_Common_Settings' ) && ravolat_addons_performance_is_active_page_builder_elementor() ) {
            // Contact Us Page
            if ( 'checked' == Ravolat_Addons_Common_Settings::ravolat_addons_get_a_elementor_widget_status( 'contact_us_page', 'checked' ) ) {
                require_once( plugin_dir_path( __FILE__ ) . 'widgets/contact-us-page/productive-elementor-widget-contact-us-page.php' );
                $widgets_manager->register( new \Productive_Elementor_Widget_Contact_Us_Page() );
            }
            // Google Maps
            if ( 'checked' == Ravolat_Addons_Common_Settings::ravolat_addons_get_a_elementor_widget_status( 'google_maps', 'checked' ) ) {
                require_once( plugin_dir_path( __FILE__ ) . 'widgets/google-map/productive-elementor-widget-google-map.php' );
                $widgets_manager->register( new \Productive_Elementor_Widget_Google_Maps() );
            }
            // Newsletter Element
            if ( 'checked' == Ravolat_Addons_Common_Settings::ravolat_addons_get_a_elementor_widget_status( 'newsletter', 'checked' ) ) {
                require_once( plugin_dir_path( __FILE__ ) . 'widgets/newsletter-element/productive-elementor-widget-newsletter-element.php' );
                $widgets_manager->register( new \Productive_Elementor_Widget_Newsletter_Element() );
            }
        }
    } else {
        // Contact Us Page
        require_once( plugin_dir_path( __FILE__ ) . 'widgets/contact-us-page/productive-elementor-widget-contact-us-page.php' );
        $widgets_manager->register( new \Productive_Elementor_Widget_Contact_Us_Page() );
        
        // Google Maps
        require_once( plugin_dir_path( __FILE__ ) . 'widgets/google-map/productive-elementor-widget-google-map.php' );
        $widgets_manager->register( new \Productive_Elementor_Widget_Google_Maps() );
        
        // Newsletter Element
        require_once( plugin_dir_path( __FILE__ ) . 'widgets/newsletter-element/productive-elementor-widget-newsletter-element.php' );
        $widgets_manager->register( new \Productive_Elementor_Widget_Newsletter_Element() );
    }
    
}
add_action( 'elementor/widgets/register', 'productive_forms_elementor_register_widgets' );


/**
 * Use hook to register JS
 */
function productive_forms_elementor_register_widget_dependencies() {
    global $productive_forms_plugin_version;
    $productiveminds_global_localize_script_vars = array();
    wp_register_script( 'productiveminds_lib_elementor_productive_forms_js_handle', PRODUCTIVE_FORMS_PLUGIN_URI . 'standard/elementor/assets/js/productiveminds-library-support-elementor-js.min.js', array( 'elementor-frontend' ), $productive_forms_plugin_version, true );

    productive_global_get_common_swiper_localize_script( $productiveminds_global_localize_script_vars );
    
    wp_localize_script(
        'productiveminds_lib_elementor_productive_forms_js_handle',
        'productiveminds_lib_elementor_productive_forms_js_name',
        $productiveminds_global_localize_script_vars
        );
}
add_action( 'wp_enqueue_scripts', 'productive_forms_elementor_register_widget_dependencies', 85 );
