<?php

class BaseWidgets {
    private $class_name;

    public function __construct($class_name) {
        $this->class_name = $class_name;
    }

    public function get_style_depends() {
        return array(strtolower($this->class_name));
    }

    public function __call($name, $arguments) {
        if (method_exists($this, $name)) {
            return call_user_func_array([$this, $name], $arguments);
        }
        return null;
    }
}

class KavehWidgetReg {

    public $widgets_manager;
    public $widget_classes;

    public function __construct($widgets_manager) {
        add_action('wp_enqueue_scripts', [$this, 'register_widget_styles'],999999);
        $this->widgets_manager = $widgets_manager;
        $this->widget_classes = [
            'Amazingoffercarouselkaveh',
            'Productscarouselkaveh',
            'Sliderfirstkaveh',
            'Productstabkaveh',
            'Categorycarouselkaveh',
            'Blogkaveh',
            'Aboutuskaveh',
            'Amazingoffersecondstkaveh',
            'Categorycarouselsecondstkaveh',
            'Blogcarouselkaveh',
            'Newsletterkaveh',
            'Customerreviewlkaveh',
            'Slidersecondkaveh',
            'Amazingofferthirdstkaveh',
            'Brandscarouselkaveh',
            'Productstabgridkaveh',
            'Productscarouselsecondstkaveh',
            'Productscarouselthirdstkaveh',
            'Aboutussecondstkaveh',
            'Categorycarouselthirdstkaveh',
            'Productscarouselfourthstkaveh',
            'Productscarouselfifthstkaveh',
            'Productstabgrid2ndstkaveh',
            'Aboutusthirdstkaveh',
            'Sliderthirdkaveh',
            'Categoryverticalcolkaveh',
            'Brandscarouselsecondstkaveh',
            'Amazingofferfourthstkaveh',
            'Priceboxkaveh',
            'Blogcarouselsecondstkaveh',
            'Sliderfourthkaveh',
            'Categorygridsecondstkaveh',
            'Productstablekaveh',
            'Sliderfifthkaveh',
            'Featureshopgridkaveh',
            'Slidersixthkaveh',
            'Amazingofferfifthstkaveh',
            'Amazingoffersixthstkaveh',
            'Sliderseventhkaveh',
            'Amazingoffersevenstkaveh',
            'Slidereighthkaveh',
            'Amazingoffereightstkaveh',
            'Categorycarouselfourthstkaveh',
            'Searchkaveh',
            'Loginandsignupkaveh',
            'Cartkaveh',
            'Wishlistkaveh',
            'Phonenumberkaveh',
            'Listkaveh',
            'Socialkaveh',
            'Newslettersecondstkaveh',
            'Iconboxkaveh',
            'Brandsgridkaveh',
            'Headingkaveh',
            'Uptotopkaveh',
            'Bloglistkaveh',
            'Megamenuonekaveh',
            'Megamenutwokaveh',
            'Counterkaveh',
            'Aparatframekaveh',
            'Logoupkaveh',
            'Sliderninethkaveh',
            'Categorycarouselfifthkaveh',
            'Brandsthirdstkaveh',
            'Productscarouselsixthstkaveh',
            'Categorycarouselsixthkaveh',
            'Aboutusfourthststkaveh',
            'Productstabsecondstkaveh',
            'Categorycarouselseventhkaveh',
            'Categorycarouseleighthkaveh',
            'Categorycarouselninethkaveh',
            'Brandsfourthstkaveh',
            'Categorycarouseltenthkaveh',
            'Categorycarouseleleventhkaveh',
            'Slidertenthkaveh',
            'Slidereleventhkaveh',
            'Brandsfifthstkaveh',
            'Categorycarouseltwelvethkaveh',
            'Productscarouselseventhstkaveh',
            'Productscarouseleighthstkaveh',
            'Productscarouselninthstkaveh',
            'Productsvertistkaveh',
            'Headingwtbuttonkaveh',
            'Productscarouseltenthstkaveh',
            'Slidertwelvethkaveh',
            'Categorycarouselthirteenthstkaveh',
            'Categorycarouselfourteenthstkaveh',
            'Megamenuthreekaveh',
            'Categoryverticalsecondstcolkaveh',
            'Amazingoffernineststkaveh',
            'Categorycarouselseventeenthkaveh',
            'Cat__carousel__eighteenth',
            'class__landing__slider__kaveh',
            'Cat__carousel__nineteenth',
            'product__special__carousel',
            'Digipluskaveh',
            'Digikalabannercarouselkaveh',
            'Digikalaquicklinkskaveh',
            'Digikalaamazingoffermarketkaveh',
            'Digikalabrandscarouselkaveh',
            'Digikalatopsellingcarouselkaveh',
            'Digikalaproductscategoryonekaveh',
            'Digikalaproductscategorytwokaveh',
            'Digikalaamazingofferonekaveh',
            'Digikalaamazingoffertwokaveh',
            'Slidertthirteenkaveh',
            'Categorycarouseltwentythstkaveh',
            'Categorycarouseltwentyonethstkaveh',
            'Iconboxtwothstkaveh',
            'Categorycarouseltwentytwothstkaveh',
            'Categorycarouseltwentythreethstkaveh',
            'Sliderfourteenteenkaveh',
            'Iconboxthreethstkaveh',
            'Productscarouseleleventhstkaveh',
            'Productscarouseltwelvethstkaveh',
            'Productscarouselthirteenthstkaveh',
            'Productscarouselfourteenthstkaveh',
            'Productstabthirdstkaveh',
            'Productsrandomofferstkaveh',
            'Categorycarouseltwentyfourthstkaveh',
            'Categorycarouseltwentyfivethstkaveh',
            'Brandssixthstkaveh',
            'Productscarouselfifteenthstkaveh',
            'Productsdigistylecarouselkaveh',
            'Productsdigistylecarouselsecondstkaveh',
            'Blogdigikalagridkaveh',
            'Productsbanimodecarouselkaveh',
            'Productsbanimodesecondstcarouselkaveh',
            'Brandsseventhstkaveh',
            'Imageboxstkaveh',
            'Blogcarouselstkaveh',
            'WooArchivekaveh',
            'WooSidebarkaveh',
            'ButtonSidebarkaveh',
            'BlogArchivekaveh',
            'BlogArchiveSidebar',
            'WooRelatedkaveh',
            'kavehwoocommerceimages',
            'kavehwoocommercerating',
            'kavehwoocommerceattrexe',
            'kavehwoocommerceaddtocart',
            'kavehwoocommercemeta',
            'kavehwoocommerceprice',
            'kavehwoocommerceproducttabs',
            'kavehwoocommerceproductattr',
            'kavehwoocommerceproductcomment',
            'KavehBlogSingleRelated',
            'KavehBlogSingleComments',
            'categorycarouseltwentysixthstkaveh',
            'categorycarouseltwentyseventhstkaveh',
            'categorycarouseltwentyeighthstkaveh',
            'categorycarouseltwentyninethstkaveh',
            'servicecarouselfirststkaveh',
            'customerreviewsecondstkaveh',
            'imageboxcarouselfirststkaveh',
            'featureboxfirststkaveh',
            'buttonfirststkaveh',
            'iconboxfourthstkaveh',
            'sliderfifteenteenkaveh',
            'slidersixteenteenkaveh',
            'amazingoffertenthstkaveh',
            'productsrandomoffersecondstkaveh',
            'categoryfourkaveh',
            'Sliderseventeenkaveh',
            'Categorymapkaveh',
            'kavehbreadcrumb',
            'kavehwoostatus',
            'megamenukaveh',
        ];
    }

    public function registerWidgets() {
        $widget_classes = $this->widget_classes;
        foreach ($widget_classes as $class_name) {
            $file_path = KAVEH_TEMPLATE_PATH . '/inc/elements/' . strtolower($class_name) . '/' . strtolower($class_name) . '.php';

            
            if (file_exists($file_path)) {
                require_once($file_path);
  
                if (class_exists($class_name)) {
                    // Create a dynamic class extending the base class
                    $dynamic_class_name = $class_name . 'Dynamic';
                    if (!class_exists($dynamic_class_name)) {
                        eval("class $dynamic_class_name extends $class_name {
                            use DynamicWidgetsExtension;
                            public function get_style_depends() {
                                return array(strtolower('$class_name'));
                            }
                        }");
                    }
                    $this->widgets_manager->register_widget_type(new $dynamic_class_name());
                }
            }
        }
    }

    public function register_widget_styles() {
        foreach ($this->widget_classes as $class_name) {
            $file_name = strtolower($class_name) . (is_rtl() ? '' : '-ltr') . '.css';
            $file_path = '/inc/elements/' . strtolower($class_name) . '/' . $file_name;
            wp_register_style(strtolower($class_name), KAVEH_TEMPLATE_URI . $file_path);
        }
    }

    public static function init() {
        $self = new self(\Elementor\Plugin::$instance->widgets_manager);
        add_action('elementor/widgets/widgets_registered', [$self, 'registerWidgets']);
    }
}


trait DynamicWidgetsExtension {
    public function __call($name, $arguments) {
        if (method_exists($this, $name)) {
            return call_user_func_array([$this, $name], $arguments);
        }
        return null;
    }
}

add_action('elementor/init', ['KavehWidgetReg', 'init']);
