<?php
/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function united_blog_widgets_init()
{
    // Initialize sidebar_args as an empty array
    $sidebar_args = array();

    $sidebar_args['sidebar'] = array(
        'name' => __('Sidebar', 'united-blog'),
        'id' => 'sidebar-1',
        'description' => '',
        'before_widget' => '<div id="%1$s" class="widget united-blog-widget %2$s"><div class="widget-content">',
        'after_widget' => '</div></div>',
        'before_title' => '<div class="widget-title-wrapper"><h2 class="widget-title"><span>',
        'after_title' => '</span></h2></div>',
    );

    // WooCommerce sidebars
    if (united_blog_is_wc_active()) {
        $sidebar_args['wc_sidebar'] = array(
            'name' => __('WooCommerce Shop/Category page Sidebar', 'united-blog'),
            'id' => 'wc-sidebar',
            'description' => __('Widgets added to this region will appear on the shop or category page of WooCommerce.', 'united-blog'),
        );
        $sidebar_args['wc_product_single_sidebar'] = array(
            'name' => __('WooCommerce Product Detail Page Sidebar', 'united-blog'),
            'id' => 'wc-product-single-sidebar',
            'description' => __('Widgets added to this region will appear on the detail page of a WooCommerce product.', 'united-blog'),
        );
    }

    // Other widget areas
    $sidebar_args['offcanvas_before_menu'] = array(
        'name' => __('Offcanvas Before Menu', 'united-blog'),
        'id' => 'offcanvas-top-menu',
        'description' => __('Widgets added to this region will appear before the menu in the offcanvas sidebar.', 'united-blog'),
    );
    $sidebar_args['offcanvas_after_menu'] = array(
        'name' => __('Offcanvas After Menu', 'united-blog'),
        'id' => 'offcanvas-bottom-menu',
        'description' => __('Widgets added to this region will appear after the menu in the offcanvas sidebar.', 'united-blog'),
    );
    $sidebar_args['homepage_fullwidth_top'] = array(
        'name' => __('Fullwidth Homepage Top', 'united-blog'),
        'id' => 'homepage-fullwidth-top',
        'description' => __('Widgets added to this region will appear above the homepage columns content.', 'united-blog'),
    );
    $sidebar_args['homepage_multiple_one'] = array(
        'name' => __('Homepage Column One', 'united-blog'),
        'id' => 'homepage-column-one',
        'description' => __('Widgets added to this region will appear on the homepage column.', 'united-blog'),
    );
    $sidebar_args['homepage_multiple_two'] = array(
        'name' => __('Homepage Column Two', 'united-blog'),
        'id' => 'homepage-column-two',
        'description' => __('Widgets added to this region will appear on the homepage column.', 'united-blog'),
    );

    if (united_blog_get_option('front_page_enable_sidebar')) {
        $sidebar_args['homepage_sidebar'] = array(
            'name' => __('Homepage Sidebar', 'united-blog'),
            'id' => 'home-page-sidebar',
            'description' => __('Widgets added to this region will appear on the homepage sidebar.', 'united-blog'),
        );
    }

    $sidebar_args['homepage_fullwidth_bottom'] = array(
        'name' => __('Fullwidth Homepage Bottom', 'united-blog'),
        'id' => 'homepage-fullwidth-bottom',
        'description' => __('Widgets added to this region will appear below the homepage content. Useful if you want to have a sidebar on the homepage but want some content below without the sidebar.', 'united-blog'),
    );
    $sidebar_args['footer_fullwidth_top'] = array(
        'name' => __('Above Footer', 'united-blog'),
        'id' => 'footer-fullwidth-top',
        'description' => __('Widgets added to this region will appear above the footer.', 'united-blog'),
    );

    // Footer column widget areas
    $footer_column = 4;
    $footer_column_layout = united_blog_get_option('footer_column_layout');
    if ($footer_column_layout) {
        switch ($footer_column_layout) {
            case 'footer_layout_1':
                $footer_column = 4;
                break;
            case 'footer_layout_2':
            case 'footer_layout_5':
                $footer_column = 3;
                break;
            case 'footer_layout_3':
            case 'footer_layout_4':
            case 'footer_layout_6':
                $footer_column = 2;
                break;
            default:
                $footer_column = 4;
        }
    }
    $cols = intval(apply_filters('united_blog_footer_widget_columns', $footer_column));
    for ($j = 1; $j <= $cols; $j++) {
        $footer = sprintf('footer_%d', $j);
        $footer_region_name = sprintf(__('Footer Column %1$d', 'united-blog'), $j);
        $footer_region_description = sprintf(__('Widgets added here will appear in column %1$d of the footer.', 'united-blog'), $j);
        $sidebar_args[$footer] = array(
            'name' => $footer_region_name,
            'id' => sprintf('footer-%d', $j),
            'description' => $footer_region_description,
        );
    }

    $sidebar_args['footer_fullwidth_bottom'] = array(
        'name' => __('Below Footer', 'united-blog'),
        'id' => 'footer-fullwidth-bottom',
        'description' => __('Widgets added to this region will appear after the footer and before sub-footer.', 'united-blog'),
    );

    // Apply filter to sidebar arguments
    $sidebar_args = apply_filters('united_blog_sidebar_args', $sidebar_args);

    // Register sidebars
    foreach ($sidebar_args as $sidebar => $args) {
        $widget_tags = array(
            'before_widget' => '<div id="%1$s" class="widget united-blog-widget %2$s"><div class="widget-content">',
            'after_widget' => '</div></div>',
            'before_title' => '<div class="widget-title-wrapper"><h2 class="widget-title">',
            'after_title' => '</h2></div>',
        );

        // Filter widget tags
        $filter_hook = sprintf('united_blog_%s_widget_tags', $sidebar);
        $widget_tags = apply_filters($filter_hook, $widget_tags);

        // Register sidebar with combined args and widget tags
        if (is_array($widget_tags)) {
            register_sidebar($args + $widget_tags);
        }
    }
}
add_action('widgets_init', 'united_blog_widgets_init');