<?php
/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function vw_car_rental_logo_customize_register( $wp_customize ) {

	$wp_customize->add_setting('vw_car_rental_logo_width',array(
		'default'=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
		));
	$wp_customize->add_control('vw_car_rental_logo_width',array(
		'label'	=> __('Logo Width','vw-car-rental'),
		'description'	=> __('Enter a value in pixels. Example:20px','vw-car-rental'),
		'input_attrs' => array(
    	'placeholder' => __( '10px', 'vw-car-rental' ),),
		'section'=> 'title_tagline',
		'type'=> 'text'
		));

	$wp_customize->add_setting('vw_car_rental_logo_height',array(
		'default'=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('vw_car_rental_logo_height',array(
		'label'	=> __('Logo Height','vw-car-rental'),
		'description'	=> __('Enter a value in pixels. Example:20px','vw-car-rental'),
		'input_attrs' => array(
    	'placeholder' => __( '10px', 'vw-car-rental' ),),
		'section'=> 'title_tagline',
		'type'=> 'text'
	));

	$wp_customize->add_setting('vw_car_rental_logo_padding',array(
		'default'=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('vw_car_rental_logo_padding',array(
		'label'	=> __('Logo Padding','vw-car-rental'),
		'description'	=> __('Enter a value in pixels. Example:20px','vw-car-rental'),
		'input_attrs' => array(
      'placeholder' => __( '10px', 'vw-car-rental' ),
    ),
		'section'=> 'title_tagline',
		'type'=> 'text'
	));

	$wp_customize->add_setting('vw_car_rental_logo_margin',array(
		'default'=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('vw_car_rental_logo_margin',array(
		'label'	=> __('Logo Margin','vw-car-rental'),
		'description'	=> __('Enter a value in pixels. Example:20px','vw-car-rental'),
		'input_attrs' => array(
      'placeholder' => __( '10px', 'vw-car-rental' ),
    ),
		'section'=> 'title_tagline',
		'type'=> 'text'
	));

	$wp_customize->add_setting( 'vw_car_rental_logo_title_hide_show',array(
    'default' => 1,
    'transport' => 'refresh',
    'sanitize_callback' => 'vw_car_rental_switch_sanitization'
  ));  
  $wp_customize->add_control( new VW_Car_Rental_Toggle_Switch_Custom_Control( $wp_customize, 'vw_car_rental_logo_title_hide_show',array(
    'label' => esc_html__( 'Show / Hide Site Title','vw-car-rental' ),
    'priority'    => 10,
    'section' => 'title_tagline'
  )));

  $wp_customize->add_setting('vw_car_rental_site_title_font_size',array(
		'default'=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('vw_car_rental_site_title_font_size',array(
		'label'	=> __('Site Title Font Size','vw-car-rental'),
		'description'	=> __('Enter a value in pixels. Example:20px','vw-car-rental'),
		'input_attrs' => array(
      'placeholder' => __( '10px', 'vw-car-rental' ),
    ),
		'section'=> 'title_tagline',
		'type'=> 'text',
		'active_callback' => 'vw_car_rental_logo_title_hide_show'
	));

	$wp_customize->add_setting('vw_car_rental_site_title_color', array(
		'default'           => '#fff',
		'sanitize_callback' => 'sanitize_hex_color',
	));
	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'vw_car_rental_site_title_color', array(
		'label'    => __('Site Title Color', 'vw-car-rental'),
		'section'  => 'title_tagline',
		'active_callback' => 'vw_car_rental_logo_title_hide_show'
	)));

	// Tagline

  $wp_customize->add_setting( 'vw_car_rental_tagline_hide_show',array(
    'default' => 0,
    'transport' => 'refresh',
    'sanitize_callback' => 'vw_car_rental_switch_sanitization'
  ));  
  $wp_customize->add_control( new VW_Car_Rental_Toggle_Switch_Custom_Control( $wp_customize, 'vw_car_rental_tagline_hide_show',array(
    'label' => esc_html__( 'Show / Hide Site Tagline','vw-car-rental' ),
    'section' => 'title_tagline'
  )));

  $wp_customize->add_setting('vw_car_rental_site_tagline_font_size',array(
		'default'=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('vw_car_rental_site_tagline_font_size',array(
		'label'	=> __('Site Tagline Font Size','vw-car-rental'),
		'description'	=> __('Enter a value in pixels. Example:20px','vw-car-rental'),
		'input_attrs' => array(
      'placeholder' => __( '10px', 'vw-car-rental' ),
    ),
		'section'=> 'title_tagline',
		'type'=> 'text',
		'active_callback' => 'vw_car_rental_tagline_hide_show'
	));

	$wp_customize->add_setting('vw_car_rental_site_tagline_color', array(
		'default'           => '#fff',
		'sanitize_callback' => 'sanitize_hex_color',
	));
	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'vw_car_rental_site_tagline_color', array(
		'label'    => __('Site Tagline Color', 'vw-car-rental'),
		'section'  => 'title_tagline',
		'active_callback' => 'vw_car_rental_tagline_hide_show'
	)));
}
add_action( 'customize_register', 'vw_car_rental_logo_customize_register' );

/**
 * Add support for logo resizing by filtering `get_custom_logo`
 */
function vw_car_rental_customize_logo_resize( $html ) {
	$size = get_theme_mod( 'logo_size' );
	$custom_logo_id = get_theme_mod( 'custom_logo' );
	// set the short side minimum
	$min = 48;

	// don't use empty() because we can still use a 0
	if ( is_numeric( $size ) && is_numeric( $custom_logo_id ) ) {

		// we're looking for $img['width'] and $img['height'] of original image
		$logo = wp_get_attachment_metadata( $custom_logo_id );
		if ( ! $logo ) return $html;

		// get the logo support size
		$sizes = get_theme_support( 'custom-logo' );

		// Check for max height and width, default to image sizes if none set in theme
		$max['height'] = isset( $sizes[0]['height'] ) ? $sizes[0]['height'] : $logo['height'];
		$max['width'] = isset( $sizes[0]['width'] ) ? $sizes[0]['width'] : $logo['width'];

		// landscape or square
		if ( $logo['width'] >= $logo['height'] ) {
			$output = vw_car_rental_min_max( $logo['height'], $logo['width'], $max['height'], $max['width'], $size, $min );
			$img = array(
				'height'	=> $output['short'],
				'width'		=> $output['long']
			);
		// portrait
		} else if ( $logo['width'] < $logo['height'] ) {
			$output = vw_car_rental_min_max( $logo['width'], $logo['height'], $max['width'], $max['height'], $size, $min );
			$img = array(
				'height'	=> $output['long'],
				'width'		=> $output['short']
			);
		}

		// add the CSS
		$css = '
<style>
.custom-logo {
	height: ' . $img['height'] . 'px;
	max-height: ' . $max['height'] . 'px;
	max-width: ' . $max['width'] . 'px;
	width: ' . $img['width'] . 'px;
}
</style>';

		$html = $css . $html;
	}

	return $html;
}
add_filter( 'get_custom_logo', 'vw_car_rental_customize_logo_resize' );

/* Helper function to determine the max size of the logo */
function vw_car_rental_min_max( $short, $long, $short_max, $long_max, $percent, $min ){
	$ratio = ( $long / $short );
	$max['long'] = ( $long_max >= $long ) ? $long : $long_max;
	$max['short'] = ( $short_max >= ( $max['long'] / $ratio ) ) ? floor( $max['long'] / $ratio ) : $short_max;

	$ppp = ( $max['short'] - $min ) / 100;

	$size['short'] = round( $min + ( $percent * $ppp ) );
	$size['long'] = round( $size['short'] / ( $short / $long ) );

	return $size;
}

/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
function vw_car_rental_customize_preview_js() {
	wp_enqueue_script( 'vw-car-rental-customizer', esc_url(get_template_directory_uri()) . '/inc/logo/js/customize-preview.js', array( 'jquery', 'customize-preview' ), '201709081119', true );
}
add_action( 'customize_preview_init', 'vw_car_rental_customize_preview_js' );

/**
 * JS handlers for Customizer Controls
 */
function vw_car_rental_customize_controls_js() {
	wp_enqueue_script( 'vw-car-rental-customizer-controls', esc_url(get_template_directory_uri()) . '/inc/logo/js/customize-controls.js', array( 'jquery', 'customize-preview' ), '201709071000', true );
}
add_action( 'customize_controls_enqueue_scripts', 'vw_car_rental_customize_controls_js' );

/**
 * Testing function to remove logo_size theme mod
 */
function vw_car_rental_remove_theme_mod() {
	if ( isset( $_GET['remove_logo_size'] ) && 'true' == $_GET['remove_logo_size'] ){
		set_theme_mod( 'logo_size', '' );
	}
}
add_action( 'wp_loaded', 'vw_car_rental_remove_theme_mod' );