<?php
namespace Elementor;


/*! ADD THEME WIDGETS
------------------------------------------------->*/
add_action( 'elementor/widgets/widgets_registered', 'Elementor\banta_widgets' );
function banta_widgets() {

    $widgets = glob( get_template_directory() . '/elementor/widgets/*.php' );
    $headerWidgets = glob( get_template_directory() . '/elementor/widgets/header/*.php' );
    $widgets = array_merge($widgets,$headerWidgets);

    if (class_exists( 'WooCommerce' )) {
        $cardWidgets = glob( get_template_directory() . '/elementor/widgets/woocommerce/*.php' );
        $widgets = array_merge($widgets,$cardWidgets);
    }

    foreach( $widgets as $key ){
        if (str_contains($key,'banta_cart_button.php') || str_contains($key,'banta_account_button.php')) {
            if (class_exists( 'WooCommerce' )){
                if ( file_exists( $key ) ) {
                    require_once $key;
                }
            }
        }else{
            if ( file_exists( $key ) ) {
                require_once $key;
            }
        }
    }
}




/*! ADD WIDGET CATEGORIES
------------------------------------------------->*/

add_action( 'elementor/elements/categories_registered', 'Elementor\jiawaz_widget_categories' );
function jiawaz_widget_categories( $elements_manager ) {
    $categories = [];

    $categories['jiawaz_theme'] = [
        'title' => esc_html__( 'Jiawaz General Widgets', TEXT_DOMAIN ),
        'icon'  => 'fa fa-search'
    ];

    $categories['jiawaz_header'] = [
        'title' => esc_html__( 'Jiawaz Header Widget', TEXT_DOMAIN ),
        'icon'  => 'fa fa-search'
    ];

    if ( class_exists( 'WooCommerce' ) ) {
        $categories['jiawaz_woocommerce_category'] = [
            'title' => esc_html__( 'Jiawaz Woocommerce Widgets', TEXT_DOMAIN ),
            'icon'  => 'fa fa-search'
        ];

        // Try to detect if we're editing a single product template
        if ( isset( $_GET['post'] ) ) {
            $template_id = intval( $_GET['post'] );
            $template_type = get_post_meta( $template_id, '_elementor_template_type', true );
            if ($template_type === 'product'){
                $categories['jiawaz_single_product'] = [
                    'title' => esc_html__( 'Jiawaz Single Product Widgets', TEXT_DOMAIN ),
                    'icon'  => 'fa fa-shopping-cart'
                ];
            }
        }
    }

    // add categories to begin of list
    $old_categories = $elements_manager->get_categories();
    $categories     = array_merge( $categories, $old_categories );
    $set_categories = function ( $categories ) {
        $this->categories = $categories;
    };
    $set_categories->call( $elements_manager, $categories );
}


//add_action( 'elementor/elements/categories_registered', 'Elementor\jiawaz_widget_categories' );
//function jiawaz_widget_categories($elements_manager ) {
//
//    $categories = [];
//    $categories['jiawaz_theme'] =
//        [
//            'title' => esc_html__( 'Jiawaz General Widgets', TEXT_DOMAIN ),
//            'icon' => 'fa fa-search'
//        ];
//
//    $categories['jiawaz_header'] =
//        [
//            'title' => esc_html__( 'Jiawaz Header Widget', TEXT_DOMAIN ),
//            'icon' => 'fa fa-search'
//        ];
//
//    if (class_exists( 'WooCommerce' )) {
//        $categories['jiawaz_woocommerce_category'] =
//            [
//                'title' => esc_html__( 'Jiawaz Woocommerce Widgets', TEXT_DOMAIN ),
//                'icon' => 'fa fa-search'
//            ];
//    }
//
//    // add categories to begin of list
//    $old_categories = $elements_manager->get_categories();
//    $categories = array_merge($categories, $old_categories);
//    $set_categories = function ( $categories ) {
//        $this->categories = $categories;
//    };
//    $set_categories->call( $elements_manager, $categories );
//}


/*! inject required html to footer
------------------------------------------------->*/
if (class_exists( 'WooCommerce' )) {
    add_action('wp_footer', function (){

        // Check if current page is using Elementor canvas template
        if (get_page_template_slug() === 'elementor_canvas') {
            return; // Skip injection on Elementor canvas template
        }

        // backdrop
        echo "<div class='bcp-backdrop'></div>";

        $showStock = get_option(TEXT_DOMAIN . '_hide_quick_view_stock');
        $showReviews = get_option(TEXT_DOMAIN . '_hide_quick_view_reviews');
        $showDescription = get_option(TEXT_DOMAIN . '_hide_quick_view_description');
        $showPrice = get_option(TEXT_DOMAIN . '_hide_quick_view_price');

        // modal
        echo "<div class='bcp-modal'>";
        echo "<span class='bcp-modal-close'><i class='icon-x1'></i></span>";
        echo "<div class='bcp-modal-image swiper'><div class='swiper-wrapper'></div><div class='custom-swiper-pagination pagination-position-bottom-center'></div><div class='custom-swiper-button-prev swiper-button-primary'><i class='iconly-Arrow-Right'></i></div><div class='custom-swiper-button-next swiper-button-primary'><i class='iconly-Arrow-Left'></i></div></div>";
        echo "<div class='bcp-modal-body woocommerce'>";
        echo "<span class='bcp-modal-title'></span>";
        if ($showReviews != 'true'){
            echo "<div class='bcp-modal-rating'></div>";
        }
        if ($showStock != 'true'){
            echo "<div class='bcp-modal-stock'>".__('Available, you can order now!',TEXT_DOMAIN)."</div>";
            echo "<div class='bcp-modal-stock outofstock unavailable'>".__('Unavailable!',TEXT_DOMAIN)."</div>";
        }
        if ($showDescription != 'true'){
            echo "<div class='bcp-modal-description'></div>";
        }
        echo "</div>"; // bcp-modal-body
        echo "<div class='bcp-modal-footer'>";

        if ($showPrice != 'true'){
            echo "<div class='bcp-modal-price'></div>";
        }

        echo "<a class='bcp-modal-more'><span>".__('More product details',TEXT_DOMAIN)."</span><i class='iconly-Arrow-Left'></i></a>";
        echo "</div>"; // bcp-modal-footer
        echo "</div>"; // bcp-modal
    });
}




