<?php
/**
 * Add Banner section and it's fields inside Frontpage Settings panel.
 * 
 * @package Ogma
 */

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

add_action( 'customize_register', 'ogma_register_frontpage_banner_options' );

if ( ! function_exists( 'ogma_register_frontpage_banner_options' ) ) :

    /**
     * Register theme options for Banner section.
     * 
     * @param WP_Customize_Manager $wp_customize Object that holds the customizer data.
     * @since 1.0.0
     */
    function ogma_register_frontpage_banner_options( $wp_customize ) {

        /*
         * Failsafe is safe
         */
        if ( ! isset( $wp_customize ) ) {
            return;
        }

        /**
         * Add Banner Section
         * 
         * Frontpage Settings > Banner
         * 
         * @uses $wp_customize->add_section() https://developer.wordpress.org/reference/classes/wp_customize_manager/add_section/
         * @since 1.0.0
         */
        $wp_customize->add_section( new Ogma_Customize_Section (
            $wp_customize, 'ogma_section_frontpage_banner',
                array(
                    'priority'  => 5,
                    'panel'     => 'ogma_panel_frontpage',
                    'title'     => __( 'Main Banner', 'ogma' ),
                )
            )
        );

        /**
         * Tabs field for Banner section
         *
         * Frontpage Settings > Banner
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'frontpage_banner_tabs',
            array(
                'default'           => ogma_get_customizer_default( 'frontpage_banner_tabs' ),
                'sanitize_callback' => 'sanitize_text_field',
            )
        );
        $wp_customize->add_control( new Ogma_Control_Tabs(
            $wp_customize, 'frontpage_banner_tabs',
                array(
                    'priority'      => 5,
                    'section'       => 'ogma_section_frontpage_banner',
                    'settings'      => 'frontpage_banner_tabs',
                    'choices'       => ogma_frontpage_banner_tabs_choices()
                )
            )
        );

        /**
         * Radio image field for banner layout
         *
         * Frontpage Settings > Banner
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'frontpage_banner_layout',
            array(
                'default'           => ogma_get_customizer_default( 'frontpage_banner_layout' ),
                'sanitize_callback' => 'ogma_sanitize_select',
            )
        );
        $wp_customize->add_control( new Ogma_Control_Radio_Image(
            $wp_customize, 'frontpage_banner_layout',
                array(
                    'priority'          => 15,
                    'section'           => 'ogma_section_frontpage_banner',
                    'settings'          => 'frontpage_banner_layout',
                    'label'             => __( 'Banner Layout', 'ogma' ),
                    'choices'           => ogma_frontpage_banner_layout_choices(),
                )
            )
        );

        /**
         * Heading field for banner slider settings
         *
         * Frontpage Settings > Banner
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'banner_slider_heading',
            array(
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( new Ogma_Control_Heading(
            $wp_customize, 'banner_slider_heading',
                array(
                    'priority'          => 20,
                    'section'           => 'ogma_section_frontpage_banner',
                    'settings'          => 'banner_slider_heading',
                    'label'             => __( 'Slider Settings', 'ogma' ),
                )
            )
        );

        /**
         * Select option of category for slider posts
         *
         * Frongpage Settings > Banner
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'banner_slider_category',
            array(
                'default'           => ogma_get_customizer_default( 'banner_slider_category' ),
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( new Ogma_Control_Dropdown_Categories(
            $wp_customize, 'banner_slider_category',
                array(
                    'priority'          => 25,
                    'section'           => 'ogma_section_frontpage_banner',
                    'settings'          => 'banner_slider_category',
                    'label'             => __( 'Slider Category', 'ogma' ),
                )
            )
        );

        /**
         * Select option for slider posts order by
         *
         * Frongpage Settings > Banner
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'banner_slider_order_by',
            array(
                'default'           => ogma_get_customizer_default( 'banner_slider_order_by' ),
                'sanitize_callback' => 'ogma_sanitize_select'
            )
        );
        $wp_customize->add_control( 'banner_slider_order_by',
            array(
                'priority'          => 30,
                'section'           => 'ogma_section_frontpage_banner',
                'settings'          => 'banner_slider_order_by',
                'label'             => __( 'Posts Order By', 'ogma' ),
                'type'              => 'select',
                'choices'           => ogma_posts_order_by_choices(),
            )
        );

        /**
         * Select option for slider posts date filter
         *
         * Frongpage Settings > Banner
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'banner_slider_date_filter',
            array(
                'default'           => ogma_get_customizer_default( 'banner_slider_date_filter' ),
                'sanitize_callback' => 'ogma_sanitize_select'
            )
        );
        $wp_customize->add_control( 'banner_slider_date_filter',
            array(
                'priority'          => 35,
                'section'           => 'ogma_section_frontpage_banner',
                'settings'          => 'banner_slider_date_filter',
                'label'             => __( 'Posts Date Filter', 'ogma' ),
                'type'              => 'select',
                'choices'           => ogma_posts_date_filter_choices(),
            )
        );

        /**
         * Heading field for banner block settings
         *
         * Frontpage Settings > Banner
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'banner_block_heading',
            array(
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( new Ogma_Control_Heading(
            $wp_customize, 'banner_block_heading',
                array(
                    'priority'          => 40,
                    'section'           => 'ogma_section_frontpage_banner',
                    'settings'          => 'banner_block_heading',
                    'label'             => __( 'Block Settings', 'ogma' ),
                    'active_callback'   => 'ogma_cb_has_banner_layout_one'
                )
            )
        );

        /**
         * Select option of category for block posts
         *
         * Frongpage Settings > Banner
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'banner_block_category',
            array(
                'default'           => ogma_get_customizer_default( 'banner_block_category' ),
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( new Ogma_Control_Dropdown_Categories(
            $wp_customize, 'banner_block_category',
                array(
                    'priority'          => 50,
                    'section'           => 'ogma_section_frontpage_banner',
                    'settings'          => 'banner_block_category',
                    'label'             => __( 'Block Category', 'ogma' ),
                    'active_callback'   => 'ogma_cb_has_banner_layout_one'
                )
            )
        );

        /**
         * Select option for block posts order by
         *
         * Frongpage Settings > Banner
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'banner_block_order_by',
            array(
                'default'           => ogma_get_customizer_default( 'banner_block_order_by' ),
                'sanitize_callback' => 'ogma_sanitize_select'
            )
        );
        $wp_customize->add_control( 'banner_block_order_by',
            array(
                'priority'          => 55,
                'section'           => 'ogma_section_frontpage_banner',
                'settings'          => 'banner_block_order_by',
                'label'             => __( 'Posts Order By', 'ogma' ),
                'type'              => 'select',
                'choices'           => ogma_posts_order_by_choices(),
                'active_callback'   => 'ogma_cb_has_banner_layout_one'
            )
        );

        /**
         * Heading field for banner tab settings
         *
         * Frontpage Settings > Banner
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'banner_tab_heading',
            array(
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( new Ogma_Control_Heading(
            $wp_customize, 'banner_tab_heading',
                array(
                    'priority'          => 60,
                    'section'           => 'ogma_section_frontpage_banner',
                    'settings'          => 'banner_tab_heading',
                    'label'             => __( 'Tab Settings', 'ogma' ),
                    'active_callback'   => 'ogma_cb_has_banner_layout_two'
                )
            )
        );

        /**
         * Text field for latest tab label
         *
         * Frontpage Settings > Banner
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'banner_tab_label_latest',
            array(
                'default'           => ogma_get_customizer_default( 'banner_tab_label_latest' ),
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( 'banner_tab_label_latest',
            array(
                'priority'          => 70,
                'section'           => 'ogma_section_frontpage_banner',
                'settings'          => 'banner_tab_label_latest',
                'label'             => __( 'Latest Tab', 'ogma' ),
                'type'              => 'text',
                'active_callback'   => 'ogma_cb_has_banner_layout_two'
            )
        );

        /**
         * Text field for popular tab label
         *
         * Frontpage Settings > Banner
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'banner_tab_label_popular',
            array(
                'default'           => ogma_get_customizer_default( 'banner_tab_label_popular' ),
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( 'banner_tab_label_popular',
            array(
                'priority'          => 75,
                'section'           => 'ogma_section_frontpage_banner',
                'settings'          => 'banner_tab_label_popular',
                'label'             => __( 'Popular Tab', 'ogma' ),
                'description'       => __( 'Popular posts will be listed by comment count.', 'ogma' ),
                'type'              => 'text',
                'active_callback'   => 'ogma_cb_has_banner_layout_two'
            )
        );

        /**
         * Text field for trending tab label
         *
         * Frontpage Settings > Banner
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'banner_tab_label_trending',
            array(
                'default'           => ogma_get_customizer_default( 'banner_tab_label_trending' ),
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( 'banner_tab_label_trending',
            array(
                'priority'          => 80,
                'section'           => 'ogma_section_frontpage_banner',
                'settings'          => 'banner_tab_label_trending',
                'label'             => __( 'Trending Tab', 'ogma' ),
                'type'              => 'text',
                'active_callback'   => 'ogma_cb_has_banner_layout_two'
            )
        );

        /**
         * Select option of category for trending tab
         *
         * Frongpage Settings > Banner
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'banner_tab_trending_category',
            array(
                'default'           => ogma_get_customizer_default( 'banner_tab_trending_category' ),
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( new Ogma_Control_Dropdown_Categories(
            $wp_customize, 'banner_tab_trending_category',
                array(
                    'priority'          => 85,
                    'section'           => 'ogma_section_frontpage_banner',
                    'settings'          => 'banner_tab_trending_category',
                    'label'             => __( 'Trending Category', 'ogma' ),
                    'active_callback'   => 'ogma_cb_has_banner_layout_two'
                )
            )
        );

        /**
         * Heading field for column reorder settings
         *
         * Frontpage Settings > Banner
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'banner_reorder_heading',
            array(
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( new Ogma_Control_Heading(
            $wp_customize, 'banner_reorder_heading',
                array(
                    'priority'          => 90,
                    'section'           => 'ogma_section_frontpage_banner',
                    'settings'          => 'banner_reorder_heading',
                    'label'             => __( 'Column Reorder', 'ogma' ),
                )
            )
        );

        /**
         * Sortable field for banner column re-order
         *
         * Frongpage Settings > Banner
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'banner_one_column_reorder',
            array(
                'default'           => ogma_get_customizer_default( 'banner_one_column_reorder' ),
                'sanitize_callback' => 'ogma_sanitize_sortable'
            )
        );
        $wp_customize->add_control( new Ogma_Control_Sortable(
            $wp_customize, 'banner_one_column_reorder',
                array(
                    'priority'          => 95,
                    'section'           => 'ogma_section_frontpage_banner',
                    'settings'          => 'banner_one_column_reorder',
                    'label'             => __( 'Banner Column Re-order', 'ogma' ),
                    'choices'           => ogma_banner_one_column_reorder_choices(),
                    'active_callback'   => 'ogma_cb_has_banner_layout_one'
                )
            )
        );

        /**
         * Sortable field for banner column re-order
         *
         * Frongpage Settings > Banner
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'banner_two_column_reorder',
            array(
                'default'           => ogma_get_customizer_default( 'banner_two_column_reorder' ),
                'sanitize_callback' => 'ogma_sanitize_sortable'
            )
        );
        $wp_customize->add_control( new Ogma_Control_Sortable(
            $wp_customize, 'banner_two_column_reorder',
                array(
                    'priority'          => 100,
                    'section'           => 'ogma_section_frontpage_banner',
                    'settings'          => 'banner_two_column_reorder',
                    'label'             => __( 'Banner Column Re-order', 'ogma' ),
                    'choices'           => ogma_banner_two_column_reorder_choices(),
                    'active_callback'   => 'ogma_cb_has_banner_layout_two'
                )
            )
        );

        /**
         * Select option for banner background option
         *
         * Frongpage Settings > Banner
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'banner_bg_type',
            array(
                'default'           => ogma_get_customizer_default( 'banner_bg_type' ),
                'sanitize_callback' => 'ogma_sanitize_select'
            )
        );
        $wp_customize->add_control( 'banner_bg_type',
            array(
                'priority'          => 150,
                'section'           => 'ogma_section_frontpage_banner',
                'settings'          => 'banner_bg_type',
                'label'             => __( 'Banner Background Type', 'ogma' ),
                'type'              => 'select',
                'choices'           => ogma_bg_type_choices(),
            )
        );

        /**
         * Color Picker field for banner background color
         * 
         * Frongpage Settings > Banner
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'banner_bg_color',
            array(
                'default'           => ogma_get_customizer_default( 'banner_bg_color' ),
                'sanitize_callback' => 'sanitize_hex_color',
            )
        );
        $wp_customize->add_control( new WP_Customize_Color_Control(
            $wp_customize, 'banner_bg_color',
                array(
                    'priority'          => 155,
                    'section'           => 'ogma_section_frontpage_banner',
                    'settings'          => 'banner_bg_color',
                    'label'             => __( 'Background Color', 'ogma' ),
                    'active_callback'   => 'ogma_cb_has_banner_bg_type_color'
                )
            )
        );

        /**
         * Image field for background image in banner section
         *
         * Frongpage Settings > Banner
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'banner_bg_image',
            array(
                'default'           => ogma_get_customizer_default( 'banner_bg_image' ),
                'sanitize_callback' => 'absint'
            )
        );
        $wp_customize->add_control( new WP_Customize_Cropped_Image_Control(
            $wp_customize, 'banner_bg_image',
                array(
                    'priority'          => 160,
                    'section'           => 'ogma_section_frontpage_banner',
                    'settings'          => 'banner_bg_image',
                    'label'             => __( 'Background Image', 'ogma' ),
                    'height'            => 600,
                    'width'             => 1920,
                    'flex_width'        =>true,
                    'active_callback'   => 'ogma_cb_has_banner_bg_type_image'
                )
            )
        );

        /**
         * Upgrade field for main banner
         * 
         * Frongpage Settings > Banner
         *
         * @since 1.0.0
         */ 
        $wp_customize->add_setting( 'upgrade_main_banner',
            array(
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( new Ogma_Control_Upgrade(
            $wp_customize, 'upgrade_main_banner',
                array(
                    'priority'      => 200,
                    'section'       => 'ogma_section_frontpage_banner',
                    'settings'      => 'upgrade_main_banner',
                    'label'         => __( 'More Features with Ogma Pro', 'ogma' ),
                    'choices'       => ogma_upgrade_choices( 'ogma_main_banner' )
                )
            )
        );


    }

endif;