<?php
/**
 * ShopBar Theme Customizer
 *
 * @package shopbar
 */

defined( 'ABSPATH' ) || exit;

// Load customizer helper functions (active_callback checks, etc.).
require get_parent_theme_file_path( 'inc/customizer/customizer-helpers.php' );

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */

if ( ! function_exists( 'shopbar_customize_register' ) ) :
function shopbar_customize_register( $wp_customize ) {

    // Add custom controls.
    require get_parent_theme_file_path( 'inc/customizer/custom-controls/info/class-info-control.php' );
    require get_parent_theme_file_path( 'inc/customizer/custom-controls/info/class-title-info-control.php' );
    require get_parent_theme_file_path( 'inc/customizer/custom-controls/toggle-button/class-login-designer-toggle-control.php' );
    require get_parent_theme_file_path( 'inc/customizer/custom-controls/radio-images/class-radio-image-control.php' );

    // Register the custom control type.
    $wp_customize->register_control_type( 'Shopbar_Toggle_Control' );


    $wp_customize->get_setting( 'blogname' )->transport         = 'refresh';
    $wp_customize->get_setting( 'blogdescription' )->transport  = 'refresh';
    $wp_customize->get_setting( 'header_textcolor' )->transport = 'refresh';

    // Remove the core "Display Site Title and Tagline" checkbox to avoid
    // duplicating our custom toggle below.
    if ( $wp_customize->get_control( 'display_header_text' ) ) {
        $wp_customize->remove_control( 'display_header_text' );
    }

    if ( isset( $wp_customize->selective_refresh ) ) :
        $wp_customize->selective_refresh->add_partial( 'blogname', array(
            'selector'        => '.site-title a',
            'render_callback' => 'shopbar_site_title_callback',
            'fallback_refresh'    => false,
        ) );
        $wp_customize->selective_refresh->add_partial( 'blogdescription', array(
            'selector'        => '.site-description',
            'render_callback' => 'shopbar_site_description_callback',
            'fallback_refresh'    => false, 
        ) );
    endif;

    // Display Site Title and Tagline
    $wp_customize->add_setting( 
        'shopbar_display_site_title_tagline', 
        array(
            'default'           => true,
            'type'              => 'theme_mod',
            'sanitize_callback' => 'shopbar_sanitize_checkbox',
            'transport'         => 'refresh',
        ) 
    );

    $wp_customize->add_control( 
        new Shopbar_Toggle_Control( $wp_customize, 'shopbar_display_site_title_tagline', 
        array(
            'label'       => esc_html__( 'Display Site Title and Tagline', 'shopbar' ),
            'section'     => 'title_tagline',
            'type'        => 'shopbar-toggle',
            'settings'    => 'shopbar_display_site_title_tagline',
        ) 
    ));   

    // Logo Width control.
    $wp_customize->add_setting(
        'shopbar_logo_width',
        array(
            'default'           => 180,
            'type'              => 'theme_mod',
            'sanitize_callback' => 'absint',
            'transport'         => 'refresh',
        )
    );

    $wp_customize->add_control(
        'shopbar_logo_width',
        array(
            'label'       => esc_html__( 'Logo Width (px)', 'shopbar' ),
            'description' => esc_html__( 'Set the maximum width of the site logo. The logo keeps its aspect ratio.', 'shopbar' ),
            'section'     => 'title_tagline',
            'type'        => 'number',
            'priority'    => 8,
            'input_attrs' => array(
                'min'  => 60,
                'max'  => 500,
                'step' => 1,
            ),
        )
    );
}
endif;
add_action( 'customize_register', 'shopbar_customize_register' );

//header settings
get_template_part( 'inc/customizer/options/section-header' );

//page settings
get_template_part( 'inc/customizer/options/section-page' );

//blog settings
get_template_part( 'inc/customizer/options/section-blog' );

//footer settings
get_template_part( 'inc/customizer/options/section-footer' );

//woocommerce settings
get_template_part( 'inc/customizer/options/section-woocommerce' );

//today's deal settings
get_template_part( 'inc/customizer/options/section-deal' );

//data sanitization
get_template_part( 'inc/customizer/data-sanitization' );


/**
 * Enqueue the customizer stylesheet.
 */
if ( ! function_exists( 'shopbar_enqueue_customizer_stylesheets' ) ) :
function shopbar_enqueue_customizer_stylesheets() {
    wp_register_style( 'shopbar-customizer-css', get_template_directory_uri() . '/inc/customizer/assets/css/customizer.css', array(), wp_get_theme()->get( 'Version' ), 'all' );
    wp_enqueue_style( 'shopbar-customizer-css' );
}
endif;
add_action( 'customize_controls_print_styles', 'shopbar_enqueue_customizer_stylesheets' );

/**
 * Enqueue customizer preview JS.
 */
if ( ! function_exists( 'shopbar_customize_preview_js' ) ) :
function shopbar_customize_preview_js() {
    wp_enqueue_script( 'shopbar-customizer-preview-js', get_template_directory_uri() . '/inc/customizer/assets/js/customizer.js', array( 'customize-preview' ), false, true );
}
endif;
add_action( 'customize_preview_init', 'shopbar_customize_preview_js' );