<?php

namespace ThemesflatAddons;

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

if (!class_exists('Themesflat_Elementor_Addons')) {
    /**
     * Main Themesflat_Elementor_Addons Class
     *
     */
    final class Themesflat_Elementor_Addons
    {
        /** Singleton *************************************************************/
        private static $instance;
        /**
         * Main Themesflat_Elementor_Addons Instance
         *
         * Insures that only one instance of Themesflat_Elementor_Addons exists in memory at any one
         * time. Also prevents needing to define globals all over the place.
         */
        public static function instance()
        {

            if (!isset(self::$instance) && !self::$instance instanceof Themesflat_Elementor_Addons) {
                self::$instance = new Themesflat_Elementor_Addons();
                self::$instance->setup_debug_constants();
                self::$instance->includes();
                self::$instance->hooks();
                self::$instance->template_hooks();
            }

            return self::$instance;
        }

        /**
         * Throw error on object clone
         *
         * The whole idea of the singleton design pattern is that there is a single
         * object therefore, we don't want the object to be cloned.
         */
        public function __clone()
        {
            // Cloning instances of the class is forbidden
            _doing_it_wrong(__FUNCTION__, __('Cheatin&#8217; huh?', 'themesflat'), '6.9');
        }

        /**
         * Disable unserializing of the class
         *
         */
        public function __wakeup()
        {
            // Unserializing instances of the class is forbidden
            _doing_it_wrong(__FUNCTION__, __('Cheatin&#8217; huh?', 'themesflat'), '6.9');
        }

        private function setup_debug_constants()
        {
            $enable_debug = false;
            $settings     = get_option('tfl_settings');
            if ($settings && isset($settings['tfl_enable_debug']) && $settings['tfl_enable_debug'] == "true") {
                $enable_debug = true;
            }
            // Enable script debugging
            if (!defined('TFL_SCRIPT_DEBUG')) {
                define('TFL_SCRIPT_DEBUG', $enable_debug);
            }
            // Minified JS file name suffix
            if (!defined('TFL_JS_SUFFIX')) {

                if ($enable_debug) {
                    define('TFL_JS_SUFFIX', '');
                } else {
                    define('TFL_JS_SUFFIX', '.min');
                }

            }
        }

        /**
         * Include required files
         *
         */
        private function includes()
        {
            require_once TFL_PLUGIN_DIR . 'includes/helper-functions.php';
            require_once TFL_PLUGIN_DIR . 'includes/query-functions.php';
//            if (is_admin()) {
//                require_once TFL_PLUGIN_DIR . 'admin/admin-init.php';
//            }
            if (!function_exists('is_plugin_active')) {
                include_once ABSPATH . 'wp-admin/includes/plugin.php';
            }
            /* Ensure WPML String Translation plugin is active */
            if (is_plugin_active('wpml-string-translation/plugin.php')) {
                require_once TFL_PLUGIN_DIR . 'i18n/wpml-compatibility-init.php';
            }
            /* Initialize the theme builder templates - Requires elementor pro plugin */
            if (is_plugin_active('elementor-pro/elementor-pro.php')) {
                require_once TFL_PLUGIN_DIR . 'includes/theme-builder/init.php';
            }
        }

        /**
         * Load Plugin Text Domain
         *
         * Looks for the plugin translation files in certain directories and loads
         * them to allow the plugin to be localised
         */
        public function load_plugin_textdomain()
        {
            $lang_dir = apply_filters('tfl_el_addons_lang_dir', trailingslashit(TFL_PLUGIN_DIR . 'languages'));
            // Traditional WordPress plugin locale filter
            $locale = apply_filters('plugin_locale', get_locale(), 'themesflat');
            $mofile = sprintf('%1$s-%2$s.mo', 'themesflat', $locale);
            // Setup paths to current locale file
            $mofile_local = $lang_dir . $mofile;

            if (file_exists($mofile_local)) {
                // Look in the /wp-content/plugins/themesflat/languages/ folder
                load_textdomain('themesflat', $mofile_local);
            } else {
                // Load the default language files
                load_plugin_textdomain('themesflat', false, $lang_dir);
            }

            return false;
        }

        /**
         * Setup the default hooks and actions
         */
        private function hooks()
        {
            add_action('plugins_loaded', array($this, 'load_plugin_textdomain'));
            // Initialize string translation of plugin elements after String Translation plugin is loaded
            add_action('wpml_st_loaded', array($this, 'init_wpml_compatibility'));
            // Filter to exclude images from lazy load using https://wordpress.org/plugins/sg-cachepress/
            add_filter('sgo_lazy_load_exclude_classes', array($this, 'exclude_images_with_specific_class'));
            add_action('elementor/widgets/widgets_registered', array($this, 'include_widgets'));
            add_action('elementor/editor/after_enqueue_styles', array($this, 'enqueue_editor_styles'));
            add_action('elementor/frontend/after_register_scripts', array($this, 'register_frontend_scripts'));
            add_action('elementor/frontend/after_register_styles', array($this, 'register_frontend_styles'), 10);
            add_action('elementor/init', array($this, 'add_elementor_category'));
        }

        public function exclude_images_with_specific_class($classes)
        {
            // Add the class name that you want to exclude from lazy load.
            $classes[] = 'skip-lazy';
            return $classes;
        }

        public function init_wpml_compatibility()
        {
            // Run WPML String Translation dependent actions
            new \ThemesflatAddons\i18n\TFL_WPML_Compatibility_Init();
        }

        private function template_hooks()
        {
            $addons = array(
                'clients',
                'carousel',
                'heading',
                'odometers',
                'piecharts',
                'posts_grid',
                'posts_carousel',
                'posts_multislider',
                'posts_slider',
                'posts_gridbox_slider',
                'pricing_table',
                'services',
                'stats_bars',
                'team_members',
                'testimonials',
                'testimonials_slider',
            );
            foreach ($addons as $addon) {
                add_filter(
                    'tfl_' . $addon . '_output',
                    function ($default_output, $settings) use ($addon) {
                        // Replace underscores with dashes for template file names
                        $template_name = str_replace('_', '-', $addon);
                        $output        = tfl_get_template_part($template_name, array(
                            'settings' => $settings,
                        ));
                        if ($output !== null) {
                            return $output;
                        }
                        return $default_output;
                    },
                    10,
                    2
                );
            }
        }

        public function add_elementor_category()
        {
            \Elementor\Plugin::instance()->elements_manager->add_category('themesflat-addons', array(
                'title' => __('Themesflat Addons', 'themesflat'),
                'icon'  => 'fa fa-plug',
            ), 1);
        }

        public function localize_array($array = array())
        {
            $array['custom_css'] = tfl_get_option('tfl_custom_css', '');
            return $array;
        }

        /**
         * Load Frontend Scripts
         *
         */
        public function register_frontend_scripts()
        {
            // Use minified libraries if TFL_SCRIPT_DEBUG is turned off
            $suffix = (defined('TFL_SCRIPT_DEBUG') && TFL_SCRIPT_DEBUG ? '' : '.min');
            wp_register_script(
                'tfl-waypoints',
                TFL_PLUGIN_URL . 'assets/js/jquery.waypoints' . $suffix . '.js',
                array('jquery'),
                TFL_VERSION,
                true
            );
            wp_register_script(
                'isotope.pkgd',
                TFL_PLUGIN_URL . 'assets/js/isotope.pkgd' . $suffix . '.js',
                array('jquery'),
                TFL_VERSION,
                true
            );
            wp_register_script(
                'imagesloaded.pkgd',
                TFL_PLUGIN_URL . 'assets/js/imagesloaded.pkgd' . $suffix . '.js',
                array('jquery'),
                TFL_VERSION,
                true
            );
            wp_register_script(
                'jquery-stats',
                TFL_PLUGIN_URL . 'assets/js/jquery.stats' . $suffix . '.js',
                array('jquery'),
                TFL_VERSION,
                true
            );
            wp_register_script(
                'jquery-flexslider',
                TFL_PLUGIN_URL . 'assets/js/jquery.flexslider' . $suffix . '.js',
                array('jquery'),
                TFL_VERSION,
                true
            );
            wp_register_script(
                'tfl-jquery-slick',
                TFL_PLUGIN_URL . 'assets/js/slick' . $suffix . '.js',
                array('jquery'),
                TFL_VERSION,
                true
            );
            wp_register_script(
                'tfl-frontend-scripts',
                TFL_PLUGIN_URL . 'assets/js/tfl-frontend' . $suffix . '.js',
                array('jquery'),
                TFL_VERSION,
                true
            );
            wp_register_script(
                'tfl-carousel-helper-scripts',
                TFL_PLUGIN_URL . 'assets/js/tfl-carousel-helper' . $suffix . '.js',
                array('tfl-jquery-slick', 'jquery'),
                TFL_VERSION,
                true
            );
            wp_register_script(
                'tfl-carousel-scripts',
                TFL_PLUGIN_URL . 'assets/js/widgets/carousel' . $suffix . '.js',
                array('tfl-carousel-helper-scripts', 'elementor-frontend'),
                TFL_VERSION,
                true
            );
            wp_register_script(
                'tfl-odometers-scripts',
                TFL_PLUGIN_URL . 'assets/js/widgets/odometers' . $suffix . '.js',
                array('tfl-waypoints', 'elementor-frontend'),
                TFL_VERSION,
                true
            );
            wp_register_script(
                'tfl-piecharts-scripts',
                TFL_PLUGIN_URL . 'assets/js/widgets/piecharts' . $suffix . '.js',
                array('tfl-waypoints', 'elementor-frontend'),
                TFL_VERSION,
                true
            );
            wp_register_script(
                'tfl-stats-bars-scripts',
                TFL_PLUGIN_URL . 'assets/js/widgets/stats-bars' . $suffix . '.js',
                array('tfl-waypoints', 'elementor-frontend'),
                TFL_VERSION,
                true
            );
            wp_register_script(
                'tfl-posts-carousel-scripts',
                TFL_PLUGIN_URL . 'assets/js/widgets/posts-carousel' . $suffix . '.js',
                array('tfl-carousel-helper-scripts', 'elementor-frontend'),
                TFL_VERSION,
                true
            );
            wp_register_script(
                'tfl-posts-gridbox-slider-scripts',
                TFL_PLUGIN_URL . 'assets/js/widgets/posts-gridbox-slider' . $suffix . '.js',
                array('elementor-frontend'),
                TFL_VERSION,
                true
            );
            wp_register_script(
                'tfl-posts-multislider-scripts',
                TFL_PLUGIN_URL . 'assets/js/widgets/posts-multislider' . $suffix . '.js',
                array('tfl-carousel-helper-scripts', 'elementor-frontend'),
                TFL_VERSION,
                true
            );
            wp_register_script(
                'tfl-posts-slider-scripts',
                TFL_PLUGIN_URL . 'assets/js/widgets/posts-slider' . $suffix . '.js',
                array('elementor-frontend'),
                TFL_VERSION,
                true
            );
            wp_register_script(
                'tfl-tab-slider-scripts',
                TFL_PLUGIN_URL . 'assets/js/widgets/tab-slider' . $suffix . '.js',
                array('tfl-jquery-slick', 'elementor-frontend'),
                TFL_VERSION,
                true
            );
            wp_register_script(
                'tfl-testimonials-slider-scripts',
                TFL_PLUGIN_URL . 'assets/js/widgets/testimonials-slider' . $suffix . '.js',
                array('jquery-flexslider', 'jquery-flexslider', 'elementor-frontend'),
                TFL_VERSION,
                true
            );
            wp_register_script(
                'tfl-portfolio-scripts',
                TFL_PLUGIN_URL . 'assets/js/widgets/portfolio' . $suffix . '.js',
                array('elementor-frontend'),
                TFL_VERSION,
                true
            );

            wp_register_script(
                'tfl-tabs-scripts',
                TFL_PLUGIN_URL . 'assets/js/widgets/tabs.js',
                array('elementor-frontend'),
                TFL_VERSION,
                true
            );

            $array = $this->localize_array();
            wp_localize_script('tfl-frontend-scripts', 'tfl_js_vars', $array);
        }

        /**
         * Load Frontend Styles
         *
         */
        public function enqueue_editor_styles()
        {
            wp_enqueue_style(
                'tfl-icomoon-styles',
                TFL_PLUGIN_URL . 'assets/css/icomoon.css',
                array(),
                TFL_VERSION
            );
        }

        /**
         * Load Frontend Styles
         *
         */
        public function register_frontend_styles()
        {
            wp_register_style(
                'tfl-animate-styles',
                TFL_PLUGIN_URL . 'assets/css/lib/animate.css',
                array(),
                TFL_VERSION
            );
            wp_register_style(
                'tfl-flexslider-styles',
                TFL_PLUGIN_URL . 'assets/css/lib/flexslider.css',
                array(),
                TFL_VERSION
            );
            wp_register_style(
                'tfl-slick-carousel-styles',
                TFL_PLUGIN_URL . 'assets/css/lib/slick.css',
                array(),
                TFL_VERSION
            );
            wp_register_style(
                'tfl-icomoon-styles',
                TFL_PLUGIN_URL . 'assets/css/icomoon.css',
                array(),
                TFL_VERSION
            );
            wp_register_style(
                'tfl-frontend-styles',
                TFL_PLUGIN_URL . 'assets/css/tfl-frontend.css',
                array(),
                TFL_VERSION
            );
            wp_register_style(
                'tfl-grid-styles',
                TFL_PLUGIN_URL . 'assets/css/tfl-grid.css',
                array(),
                TFL_VERSION
            );
            wp_register_style(
                'tfl-flexslider-custom-styles',
                TFL_PLUGIN_URL . 'assets/css/flexslider-custom.css',
                array('tfl-frontend-styles'),
                TFL_VERSION
            );
            wp_register_style(
                'tfl-slick-carousel-custom-styles',
                TFL_PLUGIN_URL . 'assets/css/slick-custom.css',
                array('tfl-frontend-styles'),
                TFL_VERSION
            );
            /* Free widgets styles */
            wp_register_style(
                'tfl-carousel-styles',
                TFL_PLUGIN_URL . 'assets/css/widgets/carousel.css',
                array('tfl-frontend-styles'),
                TFL_VERSION
            );
            wp_register_style(
                'tfl-clients-styles',
                TFL_PLUGIN_URL . 'assets/css/widgets/clients.css',
                array('tfl-frontend-styles'),
                TFL_VERSION
            );
            wp_register_style(
                'tfl-heading-styles',
                TFL_PLUGIN_URL . 'assets/css/widgets/heading.css',
                array('tfl-frontend-styles'),
                TFL_VERSION
            );
            wp_register_style(
                'tfl-odometers-styles',
                TFL_PLUGIN_URL . 'assets/css/widgets/odometers.css',
                array('tfl-frontend-styles'),
                TFL_VERSION
            );
            wp_register_style(
                'tfl-piecharts-styles',
                TFL_PLUGIN_URL . 'assets/css/widgets/piecharts.css',
                array('tfl-frontend-styles'),
                TFL_VERSION
            );
            wp_register_style(
                'tfl-portfolio-styles',
                TFL_PLUGIN_URL . 'assets/css/widgets/portfolio.css',
                array('tfl-frontend-styles'),
                TFL_VERSION
            );
            wp_register_style(
                'tfl-posts-carousel-styles',
                TFL_PLUGIN_URL . 'assets/css/widgets/posts-carousel.css',
                array('tfl-frontend-styles'),
                TFL_VERSION
            );
            wp_register_style(
                'tfl-posts-sliders-styles',
                TFL_PLUGIN_URL . 'assets/css/widgets/posts-sliders.css',
                array('tfl-frontend-styles'),
                TFL_VERSION
            );
            wp_register_style(
                'tfl-pricing-plans-styles',
                TFL_PLUGIN_URL . 'assets/css/widgets/pricing-plans.css',
                array('tfl-frontend-styles'),
                TFL_VERSION
            );
            wp_register_style(
                'tfl-services-styles',
                TFL_PLUGIN_URL . 'assets/css/widgets/services.css',
                array('tfl-frontend-styles'),
                TFL_VERSION
            );
            wp_register_style(
                'tfl-stats-bars-styles',
                TFL_PLUGIN_URL . 'assets/css/widgets/stats-bars.css',
                array('tfl-frontend-styles'),
                TFL_VERSION
            );
            wp_register_style(
                'tfl-tabs-slider-styles',
                TFL_PLUGIN_URL . 'assets/css/widgets/tabs-slider.css',
                array('tfl-frontend-styles'),
                TFL_VERSION
            );
            wp_register_style(
                'tfl-team-members-styles',
                TFL_PLUGIN_URL . 'assets/css/widgets/team-members.css',
                array('tfl-frontend-styles'),
                TFL_VERSION
            );
            wp_register_style(
                'tfl-testimonials-styles',
                TFL_PLUGIN_URL . 'assets/css/widgets/testimonials.css',
                array('tfl-frontend-styles'),
                TFL_VERSION
            );
            wp_register_style(
                'tfl-testimonials-slider-styles',
                TFL_PLUGIN_URL . 'assets/css/widgets/testimonials-slider.css',
                array('tfl-frontend-styles'),
                TFL_VERSION
            );
        }

        /**
         * Include required files
         *
         */
        public function include_widgets($widgets_manager)
        {
            require_once TFL_PLUGIN_DIR . 'includes/base/widget-base.php';
            /* Load Elementor Addon Elements */

            require_once TFL_ADDONS_DIR . 'team-members.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_Team_Widget());

            require_once TFL_ADDONS_DIR . 'team-members-v2.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_Team_V2_Widget());

            require_once TFL_ADDONS_DIR . 'blur-box.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_Blur_Box_Widget());

            require_once TFL_ADDONS_DIR . 'testimonials-slider.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_Testimonials_Slider_Widget());

            require_once TFL_ADDONS_DIR . 'heading.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_Heading_Widget());

            require_once TFL_ADDONS_DIR . 'heading-v2.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_Heading_V2_Widget());

            require_once TFL_ADDONS_DIR . 'tabs.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_Tabs_Widget());






            require_once TFL_ADDONS_DIR . 'counter-v2.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_Counter_V2_Widget());

            require_once TFL_ADDONS_DIR . 'icon-box.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_IconBox_Widget());

            require_once TFL_ADDONS_DIR . 'icon-box-v2.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_IconBox_V2_Widget());

            require_once TFL_ADDONS_DIR . 'box-service.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_Box_Service_Widget());


            require_once TFL_ADDONS_DIR . 'circle-box.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_CircleBox_Widget());

            require_once TFL_ADDONS_DIR . 'quote.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_Quote_Widget());

            require_once TFL_ADDONS_DIR . 'button.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_Button_Widget());

            require_once TFL_ADDONS_DIR . 'gallery.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_Gallery_Widget());

            require_once TFL_ADDONS_DIR . 'products-grid.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_ProductsGrid_Widget());

            require_once TFL_ADDONS_DIR . 'accordion.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_Accordion_Widget());

            require_once TFL_ADDONS_DIR . 'case.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_Case_Widget());

            require_once TFL_ADDONS_DIR . 'service.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_Service_Widget());

            require_once TFL_ADDONS_DIR . 'service-v2.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_Service_V2_Widget());

            require_once TFL_ADDONS_DIR . 'list.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_List_Widget());

            require_once TFL_ADDONS_DIR . 'logo-footer.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_LFooter_Widget());

            require_once TFL_ADDONS_DIR . 'posts.php';
            $widgets_manager->register_widget_type(new \ThemesflatAddons\Widgets\TFL_Posts_Widget());

        }

    }
    /**
     * The main function responsible for returning the one true Themesflat_Elementor_Addons
     * Instance to functions everywhere.
     *
     * Use this function like you would a global variable, except without needing
     * to declare the global.
     *
     * Example: <?php $tfl = TFL(); ?>
     */
    function TFL()
    {
        return Themesflat_Elementor_Addons::instance();
    }

    // Get TFL Running
    TFL();
}

// End if class_exists check
