<?php

if (!class_exists('Pakrco_Theme_Elementor')):
    class Pakrco_Theme_Elementor {
        public function __construct() {
            add_action('wp', [$this, 'register_auto_scripts_frontend']);
            add_filter('elementor/controls/animations/additional_animations', [$this, 'add_animations_scroll']);
            add_action('elementor/theme/register_locations', [$this, 'register_locations']);
            add_action('elementor/frontend/after_enqueue_scripts', [$this, 'scripts']);
            add_action('elementor/widgets/widgets_registered', [$this, 'customs_widgets']);
            add_action('elementor/widgets/widgets_registered', [$this, 'include_widgets']);
            add_filter('pakrco_page_title', [$this, 'hide_title']);

            add_action('elementor/icons_manager/native', [$this, 'add_icons_native']);
            add_action('elementor/controls/controls_registered', [$this, 'add_icons']);
            add_action('elementor/editor/after_enqueue_styles', [$this, 'add_style_editor'], 99);

            if (!pakrco_is_elementor_pro_activated()) {
                require get_theme_file_path('includes/elementor/custom-css.php');
                require get_theme_file_path('includes/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);
                }
                require get_theme_file_path('includes/elementor/modules/motion-fx/controls-group.php');
                require get_theme_file_path('includes/elementor/modules/motion-fx/module.php');
            }

            add_filter('elementor/fonts/additional_fonts', [$this, 'additional_fonts']);

            // Column Custom Width
            require get_theme_file_path('includes/elementor/modules/column-width.php');
            require get_theme_file_path('includes/elementor/modules/column-aspect-ratio.php');
            require get_theme_file_path('includes/elementor/modules/parallax/parallax.php');
            require get_theme_file_path('includes/elementor/modules/css-filters.php');
            require get_theme_file_path('includes/elementor/modules/colors-schema.php');
            require get_theme_file_path('includes/elementor/modules/page-settings.php');
            require get_theme_file_path('includes/elementor/modules/media-custom-field.php');

            // Layload Background
            add_action('elementor/frontend/the_content', [$this, 'lazy_background']);

            // Theme Settings
            require get_theme_file_path('includes/elementor/modules/settings.php');

            add_filter('pakrco_google_font_families', '__return_empty_array');
        }

        public function lazy_background($content){
            return preg_replace(['/(elementor-section\s)/m', '/(elementor-widget-wrap)/m','/(elementor-background-overlay)/m'], ' $1 lazyelementorbackgroundimages ', $content);
        }

        public function add_style_editor() {
            wp_enqueue_style('pakrco-elementor-editor-icon', get_theme_file_uri('/elementor-icons.css'), [], PAKRCO_VERSION);
            wp_enqueue_style('pakrco-elementor-editor', get_theme_file_uri('/elementor-editor.css'), [], PAKRCO_VERSION);
        }

        public function additional_fonts($fonts){

            return $fonts;
        }

        public function add_icons( $manager ) {
            $new_icons = json_decode( '{"pakrco-icon-arrow":"arrow","pakrco-icon-awarded":"awarded","pakrco-icon-blueprint":"blueprint","pakrco-icon-building-1":"building-1","pakrco-icon-building-2":"building-2","pakrco-icon-building-3":"building-3","pakrco-icon-call-calling":"call-calling","pakrco-icon-card":"card","pakrco-icon-documents":"documents","pakrco-icon-duplex":"duplex","pakrco-icon-gift-2":"gift-2","pakrco-icon-gym":"gym","pakrco-icon-hotel":"hotel","pakrco-icon-house-1":"house-1","pakrco-icon-house-2":"house-2","pakrco-icon-house-3":"house-3","pakrco-icon-house-4":"house-4","pakrco-icon-house-5":"house-5","pakrco-icon-house-6":"house-6","pakrco-icon-house-eco":"house-eco","pakrco-icon-house-location":"house-location","pakrco-icon-house-smart":"house-smart","pakrco-icon-house-user":"house-user","pakrco-icon-ipod":"ipod","pakrco-icon-leaf":"leaf","pakrco-icon-man":"man","pakrco-icon-map":"map","pakrco-icon-office":"office","pakrco-icon-parking":"parking","pakrco-icon-people":"people","pakrco-icon-plan":"plan","pakrco-icon-play":"play","pakrco-icon-quote-2":"quote-2","pakrco-icon-quote-custom":"quote-custom","pakrco-icon-real-estate-1":"real-estate-1","pakrco-icon-real-estate-2":"real-estate-2","pakrco-icon-route-square":"route-square","pakrco-icon-search-home":"search-home","pakrco-icon-search":"search","pakrco-icon-towers":"towers","pakrco-icon-user":"user","pakrco-icon-web-page":"web-page","pakrco-icon-360":"360","pakrco-icon-angle-down":"angle-down","pakrco-icon-angle-left":"angle-left","pakrco-icon-angle-right":"angle-right","pakrco-icon-angle-up":"angle-up","pakrco-icon-arrow-left":"arrow-left","pakrco-icon-arrow-right":"arrow-right","pakrco-icon-bars":"bars","pakrco-icon-calendar":"calendar","pakrco-icon-cart-empty":"cart-empty","pakrco-icon-check":"check","pakrco-icon-chevron-down":"chevron-down","pakrco-icon-chevron-left":"chevron-left","pakrco-icon-chevron-right":"chevron-right","pakrco-icon-chevron-up":"chevron-up","pakrco-icon-cloud-download-alt":"cloud-download-alt","pakrco-icon-comment":"comment","pakrco-icon-comments":"comments","pakrco-icon-credit-card":"credit-card","pakrco-icon-edit":"edit","pakrco-icon-envelope":"envelope","pakrco-icon-expand-alt":"expand-alt","pakrco-icon-external-link-alt":"external-link-alt","pakrco-icon-eye":"eye","pakrco-icon-file-alt":"file-alt","pakrco-icon-file-archive":"file-archive","pakrco-icon-folder-open":"folder-open","pakrco-icon-folder":"folder","pakrco-icon-frown":"frown","pakrco-icon-gift":"gift","pakrco-icon-heart-fill":"heart-fill","pakrco-icon-heart":"heart","pakrco-icon-history":"history","pakrco-icon-home":"home","pakrco-icon-info-circle":"info-circle","pakrco-icon-instagram":"instagram","pakrco-icon-map-marker-check":"map-marker-check","pakrco-icon-meh":"meh","pakrco-icon-minus":"minus","pakrco-icon-money-bill":"money-bill","pakrco-icon-moon":"moon","pakrco-icon-pencil-alt":"pencil-alt","pakrco-icon-plus":"plus","pakrco-icon-quote":"quote","pakrco-icon-random":"random","pakrco-icon-reply-all":"reply-all","pakrco-icon-reply":"reply","pakrco-icon-router":"router","pakrco-icon-shopping-cart":"shopping-cart","pakrco-icon-sign-out-alt":"sign-out-alt","pakrco-icon-smile":"smile","pakrco-icon-spinner":"spinner","pakrco-icon-square":"square","pakrco-icon-star":"star","pakrco-icon-store":"store","pakrco-icon-sun":"sun","pakrco-icon-sync":"sync","pakrco-icon-tachometer-alt":"tachometer-alt","pakrco-icon-tag":"tag","pakrco-icon-thumbtack":"thumbtack","pakrco-icon-times-circle":"times-circle","pakrco-icon-times":"times","pakrco-icon-truck":"truck","pakrco-icon-video":"video"}', 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 admin_columns_headers($defaults) {
            $defaults['shortcode'] = esc_html__('Shortcode', 'pakrco');
            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_icons_native($tabs) {
            $tabs['pakrco-custom'] = [
                'name'          => 'pakrco-icon',
                'label'         => esc_html__('Pakrco Icon', 'pakrco'),
                'prefix'        => 'pakrco-icon-',
                'displayPrefix' => 'pakrco-icon-',
                'labelIcon'     => 'fab fa-font-awesome-alt',
                'ver'           => PAKRCO_VERSION,
                'fetchJson'     => get_theme_file_uri('/includes/elementor/icons.json'),
                'native'        => true,
            ];

            return $tabs;
        }

        public function scripts() {
            wp_enqueue_script('pakrco-elementor-bglazyload');
        }

        public function register_auto_scripts_frontend() {
            // Register auto scripts frontend
            $files = glob(get_theme_file_path('/assets/js/elementor/*.js'));
            foreach ($files as $file) {
                $file_name = wp_basename($file);
                $handle    = str_replace(".js", '', $file_name);
                $scr       = get_theme_file_uri('/assets/js/elementor/' . $file_name);
                if (file_exists($file)) {
                    wp_register_script('pakrco-elementor-' . $handle, $scr, ['jquery', 'elementor-frontend'], PAKRCO_VERSION, true);
                }
            }
        }

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

        public function include_widgets($widgets_manager) {
            $files = glob(get_theme_file_path('/includes/elementor/widgets/*.php'));
            foreach ($files as $file) {
                $file = get_theme_file_path('includes/elementor/widgets/' . wp_basename($file));
                if (file_exists($file)) {
                    require_once $file;
                }
            }
        }

        public function add_animations_scroll($animations) {
            $animations['HP Animation'] = [
                'hp-move-up'    => 'Move Up',
                'hp-move-down'  => 'Move Down',
                'hp-move-left'  => 'Move Left',
                'hp-move-right' => 'Move Right',
                'hp-scale-up'   => 'Scale',
            ];

            return $animations;
        }

        public function register_locations($elementor_theme_manager) {
            $hook_result = apply_filters_deprecated('pakrco_register_elementor_locations', [true], '2.0', 'pakrco_register_elementor_locations');
            if (apply_filters('pakrco_register_elementor_locations', $hook_result)) {
                $elementor_theme_manager->register_all_core_location();
            }
        }

        public function hide_title($val) {
            $current_doc = \Elementor\Plugin::instance()->documents->get(get_the_ID());
            if ($current_doc && 'yes' === $current_doc->get_settings('hide_title')) {
                $val = false;
            }

            return $val;
        }
    }
endif;

return new Pakrco_Theme_Elementor();
