<?php
/**
 * Add Preloader section and it's fields inside General Settings panel.
 * 
 * @package Yaatra
 */

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

add_action( 'customize_register', 'yaatra_register_preloader_options' );

if ( ! function_exists( 'yaatra_register_preloader_options' ) ) :

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

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

        /**
         * Add Site Style Section
         * 
         * General Settings > Preloader
         * 
         * @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 Yaatra_Customize_Section (
            $wp_customize, 'yaatra_section_preloader',
                array(
                    'priority'              => 20,
                    'panel'                 => 'yaatra_panel_general',
                    'title'                 => __( 'Preloader', 'yaatra' ),
                )
            )
        );

        /**
         * Toggle option for preloader.
         *
         * General Settings > Preloader
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'yaatra_preloader_enable',
            array(
                'default'           => yaatra_get_customizer_default ( 'yaatra_preloader_enable' ),
                'sanitize_callback' => 'yaatra_sanitize_checkbox'
            )
        );
        $wp_customize->add_control( new Yaatra_Control_Toggle(
            $wp_customize, 'yaatra_preloader_enable',
                array(
                    'priority'      => 10,
                    'section'       => 'yaatra_section_preloader',
                    'settings'      => 'yaatra_preloader_enable',
                    'label'         => __( 'Enable Preloader', 'yaatra' )
                )
            )
        );

        /**
         * Radio image field for preloader styles
         *
         * General Settings > Preloader
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'yaatra_preloader_style',
            array(
                'default'           => yaatra_get_customizer_default ( 'yaatra_preloader_style' ),
                'sanitize_callback' => 'yaatra_sanitize_select',
            )
        );
        $wp_customize->add_control( new Yaatra_Control_Radio_Image(
            $wp_customize, 'yaatra_preloader_style',
                array(
                    'priority'          => 20,
                    'section'           => 'yaatra_section_preloader',
                    'settings'          => 'yaatra_preloader_style',
                    'label'             => __( 'Preloader Layout', 'yaatra' ),
                    'choices'           => yaatra_preloader_style_choices(),
                    'input_attrs'       => array(
                        'column'    => 4,
                    ),
                    'active_callback'   => 'yaatra_cb_has_enable_preloader'
                )
            )
        );

        /**
         * Upgrade field for preloader section
         * 
         * General Settings > Preloader
         *
         * @since 1.0.0
         */ 
        $wp_customize->add_setting( 'upgrade_preloader',
            array(
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( new Yaatra_Control_Upgrade(
            $wp_customize, 'upgrade_preloader',
                array(
                    'priority'      => 70,
                    'section'       => 'yaatra_section_preloader',
                    'settings'      => 'upgrade_preloader',
                    'label'         => __( 'More features with Yaatra Pro', 'yaatra' ),
                    'choices'       => yaatra_upgrade_choices( 'yaatra_preloader' )
                )
            )
        );

    }

endif;