<?php
/**
 * Widget FUnctions.
 *
 * @package NewsNation
 */
/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function newsnation_widgets_init(){

    $newsnation_default = newsnation_get_default_theme_options();

    register_sidebar(array(
        'name' => esc_html__('Sidebar', 'newsnation'),
        'id' => 'sidebar-1',
        'description' => esc_html__('Add widgets here.', 'newsnation'),
        'before_widget' => '<div id="%1$s" class="widget sidebar-widget theme-widget-default %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<header class="theme-widget-header"><h3 class="widget-title">',
        'after_title' => '</h3><span class="block-title-dash"></span></header>',
    ));

    register_sidebar( array(
        'name' => esc_html__('Offcanvas Widget', 'newsnation'),
        'id' => 'newsnation-offcanvas-widget',
        'description' => esc_html__('Add widgets here.', 'newsnation'),
        'before_widget' => '<div id="%1$s" class="widget theme-widget-default %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<header class="theme-widget-header"><h3 class="widget-title">',
        'after_title' => '</h3><span class="block-title-dash"></span></header>',
    ));

    $wpi_newsnation_home_sections_2 = get_theme_mod('wpi_newsnation_home_sections_2', json_encode($newsnation_default['wpi_newsnation_home_sections_2']));
    $wpi_newsnation_home_sections_2 = json_decode($wpi_newsnation_home_sections_2);

    foreach( $wpi_newsnation_home_sections_2 as $newsnation_home_section ){

        $home_section_type = isset( $newsnation_home_section->home_section_type ) ? $newsnation_home_section->home_section_type : '';

        switch( $home_section_type ){

            case 'home-widget-area':

                $ed_home_widget_area = isset( $newsnation_home_section->section_ed ) ? $newsnation_home_section->section_ed : '';

                if( $ed_home_widget_area == 'yes' ){

                    register_sidebar(array(
                        'name' => esc_html__('Front Page Widget Area', 'newsnation'),
                        'id' => 'front-page-widget-area-1',
                        'description' => esc_html__('Add widgets here.', 'newsnation'),
                        'before_widget' => '<div id="%1$s" class="widget %2$s">',
                        'after_widget' => '</div>',
                        'before_title' => '<header class="theme-widget-header"><h3 class="widget-title">',
                        'after_title' => '</h3><span class="block-title-dash"></span></header>',
                    ));

                }

                break;

            default:

                break;

        }

    }

    $newsnation_default = newsnation_get_default_theme_options();
    $footer_column_layout = absint(get_theme_mod('footer_column_layout', $newsnation_default['footer_column_layout']));

    for( $i = 0; $i < $footer_column_layout; $i++ ){

        if ($i == 0) {
            $count = esc_html__('One', 'newsnation');
        }
        if ($i == 1) {
            $count = esc_html__('Two', 'newsnation');
        }
        if ($i == 2) {
            $count = esc_html__('Three', 'newsnation');
        }
        if ($i == 3) {
            $count = esc_html__('Four', 'newsnation');
        }

        register_sidebar(array(
            'name' => esc_html__('Footer Widget ', 'newsnation') . $count,
            'id' => 'newsnation-footer-widget-' . $i,
            'description' => esc_html__('Add widgets here.', 'newsnation'),
            'before_widget' => '<div id="%1$s" class="widget theme-widget-default %2$s">',
            'after_widget' => '</div>',
            'before_title' => '<header class="theme-widget-header"><h3 class="widget-title">',
            'after_title' => '</h3><span class="block-title-dash"></span></header>',
        ));

    }

}

add_action('widgets_init', 'newsnation_widgets_init');
require get_template_directory() . '/inc/widgets/widget-base.php';
require get_template_directory() . '/inc/widgets/author.php';
require get_template_directory() . '/inc/widgets/home-page-widget.php';
require get_template_directory() . '/inc/widgets/home-page-carousel-widget.php';
require get_template_directory() . '/inc/widgets/home-page-banner-slider-widget.php';
require get_template_directory() . '/inc/widgets/social-link.php';
require get_template_directory() . '/inc/widgets/featured-category-widget.php';
