<?php

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

if (!class_exists('Bikys_Elementor')) :

    /**
     * The Bikys Elementor Integration class
     */
    class Bikys_Elementor {
        private $suffix = '';

        public function __construct() {
            $this->suffix = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min';

            add_action('wp', [$this, 'register_auto_scripts_frontend']);
            add_action('elementor/init', array($this, 'add_category'));
            add_action('wp_enqueue_scripts', [$this, 'add_scripts'], 15);
            add_action('elementor/widgets/register', array($this, 'customs_widgets'));
            add_action('elementor/widgets/register', array($this, 'include_widgets'));
            add_action('elementor/frontend/after_enqueue_scripts', [$this, 'add_js']);

            // Custom Animation Scroll
            add_filter('elementor/controls/animations/additional_animations', [$this, 'add_animations_scroll']);

            // Elementor Fix Noitice WooCommerce
            add_action('elementor/editor/before_enqueue_scripts', array($this, 'woocommerce_fix_notice'));

            // Backend
            add_action('elementor/editor/after_enqueue_styles', [$this, 'add_style_editor'], 99);

            // Add Icon Custom
            add_action('elementor/icons_manager/native', [$this, 'add_icons_native']);
            add_action('elementor/controls/controls_registered', [$this, 'add_icons']);

            // Add Breakpoints
            add_action('wp_enqueue_scripts', 'bikys_elementor_breakpoints', 9999);

            if (!bikys_is_elementor_pro_activated()) {
                require trailingslashit(get_template_directory()) . 'inc/elementor/custom-css.php';
                require trailingslashit(get_template_directory()) . 'inc/elementor/sticky-section.php';
                if (is_admin()) {
                    add_action('manage_elementor_library_posts_columns', [$this, 'admin_columns_headers']);
                    add_action('manage_elementor_library_posts_custom_column', [$this, 'admin_columns_content'], 10, 2);
                }
            }

            add_filter('elementor/fonts/additional_fonts', [$this, 'additional_fonts']);
            add_action('wp_enqueue_scripts', [$this, 'elementor_kit']);
        }

        public function elementor_kit() {
            $active_kit_id = Elementor\Plugin::$instance->kits_manager->get_active_id();
            Elementor\Plugin::$instance->kits_manager->frontend_before_enqueue_styles();
            $myvals = get_post_meta($active_kit_id, '_elementor_page_settings', true);
            if (!empty($myvals)) {
                $css = '';
                foreach ($myvals['system_colors'] as $key => $value) {
                    $css .= $value['color'] !== '' ? '--' . $value['_id'] . ':' . $value['color'] . ';' : '';
                }

                $var = "body{{$css}}";
                wp_add_inline_style('bikys-style', $var);
            }
        }

        public function additional_fonts($fonts) {
            $fonts["Bikys Heading"]     = 'system';
            return $fonts;
        }

        public function admin_columns_headers($defaults) {
            $defaults['shortcode'] = esc_html__('Shortcode', 'bikys');

            return $defaults;
        }

        public function admin_columns_content($column_name, $post_id) {
            if ('shortcode' === $column_name) {
                ob_start();
                ?>
                <input class="elementor-shortcode-input" type="text" readonly onfocus="this.select()" value="[hfe_template id='<?php echo esc_attr($post_id); ?>']"/>
                <?php
                ob_get_contents();
            }
        }

        public function add_js() {
            global $bikys_version;
            wp_enqueue_script('bikys-elementor-frontend', get_theme_file_uri('/assets/js/elementor-frontend.js'), [], $bikys_version);
        }

        public function add_style_editor() {
            global $bikys_version;
            wp_enqueue_style('bikys-elementor-editor-icon', get_theme_file_uri('/assets/css/admin/elementor/icons.css'), [], $bikys_version);
        }

        public function add_scripts() {
            global $bikys_version;
            $suffix = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min';
            wp_enqueue_style('bikys-elementor', get_template_directory_uri() . '/assets/css/base/elementor.css', '', $bikys_version);
            wp_style_add_data('bikys-elementor', 'rtl', 'replace');

            // Add Scripts
            wp_register_script('tweenmax', get_theme_file_uri('/assets/js/vendor/TweenMax.min.js'), array('jquery'), '1.11.1');
            wp_register_script('parallaxmouse', get_theme_file_uri('/assets/js/vendor/jquery-parallax.js'), array('jquery'), $bikys_version);
            wp_register_script('chart', get_theme_file_uri('/assets/js/vendor/chart.js'), array('jquery'), $bikys_version);

            if (bikys_elementor_check_type('animated-bg-parallax')) {
                wp_enqueue_script('tweenmax');
                wp_enqueue_script('jquery-panr', get_theme_file_uri('/assets/js/vendor/jquery-panr' . $suffix . '.js'), array('jquery'), '0.0.1');
            }
        }


        public function register_auto_scripts_frontend() {
            global $bikys_version;
            wp_register_script('bikys-elementor-box-icon', get_theme_file_uri('/assets/js/elementor/box-icon.js'), array('jquery','elementor-frontend'), $bikys_version, true);
            wp_register_script('bikys-elementor-brand', get_theme_file_uri('/assets/js/elementor/brand.js'), array('jquery','elementor-frontend'), $bikys_version, true);
            wp_register_script('bikys-elementor-button-popup', get_theme_file_uri('/assets/js/elementor/button-popup.js'), array('jquery','elementor-frontend'), $bikys_version, true);
            wp_register_script('bikys-elementor-chart', get_theme_file_uri('/assets/js/elementor/chart.js'), array('jquery','elementor-frontend'), $bikys_version, true);
            wp_register_script('bikys-elementor-countdown', get_theme_file_uri('/assets/js/elementor/countdown.js'), array('jquery','elementor-frontend'), $bikys_version, true);
            wp_register_script('bikys-elementor-image-gallery', get_theme_file_uri('/assets/js/elementor/image-gallery.js'), array('jquery','elementor-frontend'), $bikys_version, true);
            wp_register_script('bikys-elementor-image-location', get_theme_file_uri('/assets/js/elementor/image-location.js'), array('jquery','elementor-frontend'), $bikys_version, true);
            wp_register_script('bikys-elementor-language-switcher', get_theme_file_uri('/assets/js/elementor/language-switcher.js'), array('jquery','elementor-frontend'), $bikys_version, true);
            wp_register_script('bikys-elementor-posts-grid', get_theme_file_uri('/assets/js/elementor/posts-grid.js'), array('jquery','elementor-frontend'), $bikys_version, true);
            wp_register_script('bikys-elementor-product-categories', get_theme_file_uri('/assets/js/elementor/product-categories.js'), array('jquery','elementor-frontend'), $bikys_version, true);
            wp_register_script('bikys-elementor-product-currency', get_theme_file_uri('/assets/js/elementor/product-currency.js'), array('jquery','elementor-frontend'), $bikys_version, true);
            wp_register_script('bikys-elementor-product-tab', get_theme_file_uri('/assets/js/elementor/product-tab.js'), array('jquery','elementor-frontend'), $bikys_version, true);
            wp_register_script('bikys-elementor-products', get_theme_file_uri('/assets/js/elementor/products.js'), array('jquery','elementor-frontend'), $bikys_version, true);
            wp_register_script('bikys-elementor-slide-scrolling', get_theme_file_uri('/assets/js/elementor/slide-scrolling.js'), array('jquery','elementor-frontend'), $bikys_version, true);
            wp_register_script('bikys-elementor-slider-template', get_theme_file_uri('/assets/js/elementor/slider-template.js'), array('jquery','elementor-frontend'), $bikys_version, true);
            wp_register_script('bikys-elementor-tabs', get_theme_file_uri('/assets/js/elementor/tabs.js'), array('jquery','elementor-frontend'), $bikys_version, true);
            wp_register_script('bikys-elementor-team-box', get_theme_file_uri('/assets/js/elementor/team-box.js'), array('jquery','elementor-frontend'), $bikys_version, true);
            wp_register_script('bikys-elementor-testimonial', get_theme_file_uri('/assets/js/elementor/testimonial.js'), array('jquery','elementor-frontend'), $bikys_version, true);
            wp_register_script('bikys-elementor-text-carousel', get_theme_file_uri('/assets/js/elementor/text-carousel.js'), array('jquery','elementor-frontend'), $bikys_version, true);
            wp_register_script('bikys-elementor-video', get_theme_file_uri('/assets/js/elementor/video.js'), array('jquery','elementor-frontend'), $bikys_version, true);
           
        }

        public function add_category() {
            Elementor\Plugin::instance()->elements_manager->add_category(
                'bikys-addons',
                array(
                    'title' => esc_html__('Bikys Addons', 'bikys'),
                    'icon'  => 'fa fa-plug',
                ),
                1);
        }

        public function add_animations_scroll($animations) {
            $animations['Bikys Animation'] = [
                'opal-move-up'    => 'Move Up',
                'opal-move-down'  => 'Move Down',
                'opal-move-left'  => 'Move Left',
                'opal-move-right' => 'Move Right',
                'opal-flip'       => 'Flip',
                'opal-helix'      => 'Helix',
                'opal-scale-up'   => 'Scale',
                'opal-am-popup'   => 'Popup',
            ];

            return $animations;
        }

        public function customs_widgets() {
            $files = glob(get_theme_file_path('/inc/elementor/custom-widgets/*.php'));
            foreach ($files as $file) {
                if (file_exists($file)) {
                    require_once $file;
                }
            }
        }

        /**
         * @param $widgets_manager Elementor\Widgets_Manager
         */
        public function include_widgets($widgets_manager) {
            require 'base-carousel-widget.php';
            $files = glob(get_theme_file_path('/inc/elementor/widgets/*.php'));
            foreach ($files as $file) {
                if (file_exists($file)) {
                    require_once $file;
                }
            }
        }

        public function woocommerce_fix_notice() {
            if (bikys_is_woocommerce_activated()) {
                remove_action('woocommerce_cart_is_empty', 'woocommerce_output_all_notices', 5);
                remove_action('woocommerce_shortcode_before_product_cat_loop', 'woocommerce_output_all_notices', 10);
                remove_action('woocommerce_before_shop_loop', 'woocommerce_output_all_notices', 10);
                remove_action('woocommerce_before_single_product', 'woocommerce_output_all_notices', 10);
                remove_action('woocommerce_before_cart', 'woocommerce_output_all_notices', 10);
                remove_action('woocommerce_before_checkout_form', 'woocommerce_output_all_notices', 10);
                remove_action('woocommerce_account_content', 'woocommerce_output_all_notices', 10);
                remove_action('woocommerce_before_customer_login_form', 'woocommerce_output_all_notices', 10);
            }
        }

        public function add_icons( $manager ) {
            $new_icons = json_decode( '{"bikys-icon-address":"address","bikys-icon-angle-down":"angle-down","bikys-icon-angle-left":"angle-left","bikys-icon-angle-right":"angle-right","bikys-icon-angle-up":"angle-up","bikys-icon-arrow-down":"arrow-down","bikys-icon-arrow-drop-down-fill":"arrow-drop-down-fill","bikys-icon-arrow-left":"arrow-left","bikys-icon-arrow-right":"arrow-right","bikys-icon-arrow-up":"arrow-up","bikys-icon-bicycle":"bicycle","bikys-icon-calendar":"calendar","bikys-icon-check-verified":"check-verified","bikys-icon-checked":"checked","bikys-icon-chevron-double-left":"chevron-double-left","bikys-icon-chevron-double-right":"chevron-double-right","bikys-icon-clock":"clock","bikys-icon-close":"close","bikys-icon-compare":"compare","bikys-icon-config":"config","bikys-icon-copy":"copy","bikys-icon-delivery-truck":"delivery-truck","bikys-icon-delivery":"delivery","bikys-icon-desktop":"desktop","bikys-icon-disc-brake":"disc-brake","bikys-icon-expert":"expert","bikys-icon-facebook-f":"facebook-f","bikys-icon-fast":"fast","bikys-icon-featured":"featured","bikys-icon-filter":"filter","bikys-icon-fork":"fork","bikys-icon-frame":"frame","bikys-icon-gear-fix":"gear-fix","bikys-icon-globe":"globe","bikys-icon-google-plus-g":"google-plus-g","bikys-icon-grid-2":"grid-2","bikys-icon-heart":"heart","bikys-icon-help":"help","bikys-icon-import":"import","bikys-icon-instructions":"instructions","bikys-icon-language-1":"language-1","bikys-icon-linkedin-in":"linkedin-in","bikys-icon-list-ul":"list-ul","bikys-icon-locator":"locator","bikys-icon-long-arrow-left":"long-arrow-left","bikys-icon-long-arrow-right":"long-arrow-right","bikys-icon-mail":"mail","bikys-icon-map-marker-alt":"map-marker-alt","bikys-icon-mechanic-2":"mechanic-2","bikys-icon-mechanic":"mechanic","bikys-icon-message-square":"message-square","bikys-icon-money-back":"money-back","bikys-icon-money":"money","bikys-icon-noise":"noise","bikys-icon-one-click":"one-click","bikys-icon-opinion":"opinion","bikys-icon-payment":"payment","bikys-icon-pen":"pen","bikys-icon-phone-1":"phone-1","bikys-icon-phone-2":"phone-2","bikys-icon-phone":"phone","bikys-icon-play-1":"play-1","bikys-icon-play-circle":"play-circle","bikys-icon-plus-2":"plus-2","bikys-icon-plus-circle":"plus-circle","bikys-icon-popular":"popular","bikys-icon-product":"product","bikys-icon-question1":"question1","bikys-icon-quote-1":"quote-1","bikys-icon-quote":"quote","bikys-icon-refresh":"refresh","bikys-icon-return":"return","bikys-icon-right-arrow-cicrle":"right-arrow-cicrle","bikys-icon-search2":"search2","bikys-icon-seat":"seat","bikys-icon-seo":"seo","bikys-icon-service":"service","bikys-icon-shipping":"shipping","bikys-icon-shopping-bag":"shopping-bag","bikys-icon-sliders-v":"sliders-v","bikys-icon-star2":"star2","bikys-icon-supermarket":"supermarket","bikys-icon-support-1":"support-1","bikys-icon-support-2":"support-2","bikys-icon-support":"support","bikys-icon-top-brand":"top-brand","bikys-icon-touch-controls":"touch-controls","bikys-icon-twitte-1":"twitte-1","bikys-icon-typography":"typography","bikys-icon-user":"user","bikys-icon-warranty":"warranty","bikys-icon-wheels":"wheels","bikys-icon-360":"360","bikys-icon-bars":"bars","bikys-icon-cart-empty":"cart-empty","bikys-icon-check-square":"check-square","bikys-icon-circle":"circle","bikys-icon-cloud-download-alt":"cloud-download-alt","bikys-icon-comment":"comment","bikys-icon-comments":"comments","bikys-icon-contact":"contact","bikys-icon-credit-card":"credit-card","bikys-icon-dot-circle":"dot-circle","bikys-icon-edit":"edit","bikys-icon-envelope":"envelope","bikys-icon-expand-alt":"expand-alt","bikys-icon-external-link-alt":"external-link-alt","bikys-icon-file-alt":"file-alt","bikys-icon-file-archive":"file-archive","bikys-icon-folder-open":"folder-open","bikys-icon-folder":"folder","bikys-icon-frown":"frown","bikys-icon-gift":"gift","bikys-icon-grid":"grid","bikys-icon-grip-horizontal":"grip-horizontal","bikys-icon-heart-fill":"heart-fill","bikys-icon-history":"history","bikys-icon-home":"home","bikys-icon-info-circle":"info-circle","bikys-icon-instagram":"instagram","bikys-icon-level-up-alt":"level-up-alt","bikys-icon-list":"list","bikys-icon-map-marker-check":"map-marker-check","bikys-icon-meh":"meh","bikys-icon-minus-circle":"minus-circle","bikys-icon-minus":"minus","bikys-icon-mobile-android-alt":"mobile-android-alt","bikys-icon-money-bill":"money-bill","bikys-icon-pencil-alt":"pencil-alt","bikys-icon-plus":"plus","bikys-icon-random":"random","bikys-icon-reply-all":"reply-all","bikys-icon-reply":"reply","bikys-icon-search-plus":"search-plus","bikys-icon-search":"search","bikys-icon-shield-check":"shield-check","bikys-icon-shopping-basket":"shopping-basket","bikys-icon-shopping-cart":"shopping-cart","bikys-icon-sign-out-alt":"sign-out-alt","bikys-icon-smile":"smile","bikys-icon-spinner":"spinner","bikys-icon-square":"square","bikys-icon-star":"star","bikys-icon-store":"store","bikys-icon-sync":"sync","bikys-icon-tachometer-alt":"tachometer-alt","bikys-icon-thumbtack":"thumbtack","bikys-icon-ticket":"ticket","bikys-icon-times-circle":"times-circle","bikys-icon-times-square":"times-square","bikys-icon-times":"times","bikys-icon-trophy-alt":"trophy-alt","bikys-icon-truck":"truck","bikys-icon-video":"video","bikys-icon-wishlist-empty":"wishlist-empty","bikys-icon-adobe":"adobe","bikys-icon-amazon":"amazon","bikys-icon-android":"android","bikys-icon-angular":"angular","bikys-icon-apper":"apper","bikys-icon-apple":"apple","bikys-icon-atlassian":"atlassian","bikys-icon-behance":"behance","bikys-icon-bitbucket":"bitbucket","bikys-icon-bitcoin":"bitcoin","bikys-icon-bity":"bity","bikys-icon-bluetooth":"bluetooth","bikys-icon-btc":"btc","bikys-icon-centos":"centos","bikys-icon-chrome":"chrome","bikys-icon-codepen":"codepen","bikys-icon-cpanel":"cpanel","bikys-icon-discord":"discord","bikys-icon-dochub":"dochub","bikys-icon-docker":"docker","bikys-icon-dribbble":"dribbble","bikys-icon-dropbox":"dropbox","bikys-icon-drupal":"drupal","bikys-icon-ebay":"ebay","bikys-icon-facebook":"facebook","bikys-icon-figma":"figma","bikys-icon-firefox":"firefox","bikys-icon-google-plus":"google-plus","bikys-icon-google":"google","bikys-icon-grunt":"grunt","bikys-icon-gulp":"gulp","bikys-icon-html5":"html5","bikys-icon-joomla":"joomla","bikys-icon-link-brand":"link-brand","bikys-icon-linkedin":"linkedin","bikys-icon-mailchimp":"mailchimp","bikys-icon-opencart":"opencart","bikys-icon-paypal":"paypal","bikys-icon-pinterest-p":"pinterest-p","bikys-icon-reddit":"reddit","bikys-icon-skype":"skype","bikys-icon-slack":"slack","bikys-icon-snapchat":"snapchat","bikys-icon-spotify":"spotify","bikys-icon-trello":"trello","bikys-icon-twitter":"twitter","bikys-icon-vimeo":"vimeo","bikys-icon-whatsapp":"whatsapp","bikys-icon-wordpress":"wordpress","bikys-icon-yoast":"yoast","bikys-icon-youtube":"youtube"}', true );
			$icons     = $manager->get_control( 'icon' )->get_settings( 'options' );
			$new_icons = array_merge(
				$new_icons,
				$icons
			);
			// Then we set a new list of icons as the options of the icon control
			$manager->get_control( 'icon' )->set_settings( 'options', $new_icons ); 
        }

        public function add_icons_native($tabs) {
            global $bikys_version;
            $tabs['opal-custom'] = [
                'name'          => 'bikys-icon',
                'label'         => esc_html__('Bikys Icon', 'bikys'),
                'prefix'        => 'bikys-icon-',
                'displayPrefix' => 'bikys-icon-',
                'labelIcon'     => 'fab fa-font-awesome-alt',
                'ver'           => $bikys_version,
                'fetchJson'     => get_theme_file_uri('/inc/elementor/icons.json'),
                'native'        => true,
            ];

            return $tabs;
        }
    }

endif;

return new Bikys_Elementor();
