<?php
/**
 * Add Main Area section and it's fields inside Header Settings panel.
 * 
 * @package Ogma
 */

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

add_action( 'customize_register', 'ogma_register_main_area_options' );

if ( ! function_exists( 'ogma_register_main_area_options' ) ) :

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

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

        /**
         * Add Main Area Section
         * 
         * Header Settings > Main Area
         * 
         * @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_header_main_area',
                array(
                    'priority'  => 20,
                    'panel'     => 'ogma_panel_header',
                    'title'     => __( 'Main Area', 'ogma' ),
                )
            )
        );

        /**
         * Tabs field for Main Area section
         *
         * Header Settings > Main Area
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'header_main_area_tabs',
            array(
                'default'           => ogma_get_customizer_default( 'header_main_area_tabs' ),
                'sanitize_callback' => 'sanitize_text_field',
            )
        );
        $wp_customize->add_control( new Ogma_Control_Tabs(
            $wp_customize, 'header_main_area_tabs',
                array(
                    'priority'      => 5,
                    'section'       => 'ogma_section_header_main_area',
                    'settings'      => 'header_main_area_tabs',
                    'choices'       => ogma_header_main_area_tabs_choices()
                )
            )
        );

        /**
         * Toggle option for primary menu sticky.
         *
         * Header Settings > Main Area
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'header_sticky_enable',
            array(
                'default'           => ogma_get_customizer_default( 'header_sticky_enable' ),
                'sanitize_callback' => 'ogma_sanitize_checkbox'
            )
        );
        $wp_customize->add_control( new Ogma_Control_Toggle(
            $wp_customize, 'header_sticky_enable',
                array(
                    'priority'      => 10,
                    'section'       => 'ogma_section_header_main_area',
                    'settings'      => 'header_sticky_enable',
                    'label'         => __( 'Enable Header Sticky', 'ogma' )
                )
            )
        );

        /**
         * Toggle option for header site mode switcher.
         *
         * Header Settings > Main Area
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'header_site_mode_switch_enable',
            array(
                'default'           => ogma_get_customizer_default( 'header_site_mode_switch_enable' ),
                'sanitize_callback' => 'ogma_sanitize_checkbox'
            )
        );
        $wp_customize->add_control( new Ogma_Control_Toggle(
            $wp_customize, 'header_site_mode_switch_enable',
                array(
                    'priority'      => 15,
                    'section'       => 'ogma_section_header_main_area',
                    'settings'      => 'header_site_mode_switch_enable',
                    'label'         => __( 'Enable site mode switcher', 'ogma' )
                )
            )
        );

        /**
         * Toggle option for header search icon.
         *
         * Header Settings > Main Area
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'header_search_enable',
            array(
                'default'           => ogma_get_customizer_default( 'header_search_enable' ),
                'sanitize_callback' => 'ogma_sanitize_checkbox'
            )
        );
        $wp_customize->add_control( new Ogma_Control_Toggle(
            $wp_customize, 'header_search_enable',
                array(
                    'priority'      => 20,
                    'section'       => 'ogma_section_header_main_area',
                    'settings'      => 'header_search_enable',
                    'label'         => __( 'Enable Search Icon', 'ogma' )
                )
            )
        );

        /**
         * Toggle option for header sticky sidebar toggle.
         *
         * Header Settings > Main Area
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'header_sticky_sidebar_toggle_enable',
            array(
                'default'           => ogma_get_customizer_default( 'header_sticky_sidebar_toggle_enable' ),
                'sanitize_callback' => 'ogma_sanitize_checkbox'
            )
        );
        $wp_customize->add_control( new Ogma_Control_Toggle(
            $wp_customize, 'header_sticky_sidebar_toggle_enable',
                array(
                    'priority'      => 25,
                    'section'       => 'ogma_section_header_main_area',
                    'settings'      => 'header_sticky_sidebar_toggle_enable',
                    'label'         => __( 'Enable sticky sidebar toggle', 'ogma' )
                )
            )
        );

        /**
         * Redirect field for sticky sidebar widget area.
         *
         * Header Settings > Main Area
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'header_sticky_sidebar_redirect',
            array(
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( new Ogma_Control_Redirect(
            $wp_customize, 'header_sticky_sidebar_redirect',
                array(
                    'priority'      => 25,
                    'section'       => 'ogma_section_header_main_area',
                    'settings'      => 'header_sticky_sidebar_redirect',
                    'choices'       => array(
                        'header_sticky_sidebar_redirect' => array(
                            'type'          => 'section',
                            'type_id'       => 'sidebar-widgets-header-sticky-sidebar',
                            'type_label'    => __( 'Manage sidebar from here', 'ogma' )
                        )
                    ),
                    'active_callback'   => 'ogma_cb_has_sticky_sidebar_toggle_enable'
                )
            )
        );

        /**
         * Heading field for custom button
         *
         * Header Settings > Main Area
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'header_custom_button_heading',
            array(
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( new Ogma_Control_Heading(
            $wp_customize, 'header_custom_button_heading',
                array(
                    'priority'          => 30,
                    'section'           => 'ogma_section_header_main_area',
                    'settings'          => 'header_custom_button_heading',
                    'label'             => __( 'Custom Button', 'ogma' )
                )
            )
        );

        /**
         * Text field for custom button label
         *
         * Header Settings > Main Area
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'header_custom_button_label',
            array(
                'default'           => ogma_get_customizer_default( 'header_custom_button_label' ),
                'transport'         => 'postMessage',
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( 'header_custom_button_label',
            array(
                'priority'          => 30,
                'section'           => 'ogma_section_header_main_area',
                'settings'          => 'header_custom_button_label',
                'label'             => __( 'Custom Button Label', 'ogma' ),
                'type'              => 'text',
            )
        );

        /**
         * Url field for custom button link
         *
         * Header Settings > Main Area
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'header_custom_button_link',
            array(
                'default'           => ogma_get_customizer_default( 'header_custom_button_link' ),
                'sanitize_callback' => 'esc_url_raw'
            )
        );
        $wp_customize->add_control( 'header_custom_button_link',
            array(
                'priority'          => 35,
                'section'           => 'ogma_section_header_main_area',
                'settings'          => 'header_custom_button_link',
                'label'             => __( 'Custom Button Link', 'ogma' ),
                'type'              => 'url',
            )
        );


        /**
         * Radio image field for header main area layout.
         *
         * Header Settings > Main Area
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'header_main_area_layout',
            array(
                'default'           => ogma_get_customizer_default( 'header_main_area_layout' ),
                'sanitize_callback' => 'ogma_sanitize_select',
            )
        );
        $wp_customize->add_control( new Ogma_Control_Radio_Image(
            $wp_customize, 'header_main_area_layout',
                array(
                    'priority'      => 55,
                    'section'       => 'ogma_section_header_main_area',
                    'settings'      => 'header_main_area_layout',
                    'label'         => __( 'Main Area Layout', 'ogma' ),
                    'choices'       => ogma_header_main_area_layout_choices(),
                )
            )
        );

        /**
         * Select option for header main background type
         *
         * Header Settings > Main Area
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'header_main_bg_type',
            array(
                'default'           => ogma_get_customizer_default( 'header_main_bg_type' ),
                'sanitize_callback' => 'ogma_sanitize_select'
            )
        );
        $wp_customize->add_control( 'header_main_bg_type',
            array(
                'priority'          => 60,
                'section'           => 'ogma_section_header_main_area',
                'settings'          => 'header_main_bg_type',
                'label'             => __( 'Background Type', 'ogma' ),
                'type'              => 'select',
                'choices'           => ogma_bg_type_choices()
            )
        );

        /**
         * Color Picker field for Main Area background color
         * 
         * Header Settings > Main Area
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'header_main_bg_color',
            array(
                'default'           => ogma_get_customizer_default( 'header_main_bg_color' ),
                'sanitize_callback' => 'sanitize_hex_color',
            )
        );
        $wp_customize->add_control( new WP_Customize_Color_Control(
            $wp_customize, 'header_main_bg_color',
                array(
                    'priority'          => 65,
                    'section'           => 'ogma_section_header_main_area',
                    'settings'          => 'header_main_bg_color',
                    'label'             => __( 'Background Color', 'ogma' ),
                    'active_callback'   => 'ogma_cb_has_bg_type_color',
                )
            )
        );

        /**
         * Cropped Image field for header main area background image
         *
         * Header Settings > Main Area
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'header_main_bg_image',
            array(
                'default'           => ogma_get_customizer_default( 'header_main_bg_image' ),
                'sanitize_callback' => 'absint'
            )
        );
        $wp_customize->add_control( new WP_Customize_Cropped_Image_Control(
            $wp_customize, 'header_main_bg_image',
                array(
                    'priority'          => 70,
                    'section'           => 'ogma_section_header_main_area',
                    'settings'          => 'header_main_bg_image',
                    'label'             => __( 'Background Image', 'ogma' ),
                    'height'            => 270,
                    'width'             => 1320,
                    'flex_width'        =>true,
                    'active_callback'   => 'ogma_cb_has_bg_type_image'
                )
            )
        );

        /**
         * Upgrade field for Header Main
         * 
         * Header Settings > Main Area
         *
         * @since 1.0.0
         */ 
        $wp_customize->add_setting( 'upgrade_header_main',
            array(
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( new Ogma_Control_Upgrade(
            $wp_customize, 'upgrade_header_main',
                array(
                    'priority'      => 100,
                    'section'       => 'ogma_section_header_main_area',
                    'settings'      => 'upgrade_header_main',
                    'label'         => __( 'More Features with Ogma Pro', 'ogma' ),
                    'choices'       => ogma_upgrade_choices( 'ogma_header_main' )
                )
            )
        );

    }

endif;