<?php
/**
 * megapress Theme Customizer
 *
 * @package megapress
 */

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

	global $megapress_categories;

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

	if ( isset( $wp_customize->selective_refresh ) ) {
		$wp_customize->selective_refresh->add_partial( 'blogname', array(
			'selector'        => '.site-title a',
			'render_callback' => 'megapress_customize_partial_blogname',
		) );
		$wp_customize->selective_refresh->add_partial( 'blogdescription', array(
			'selector'        => '.site-description',
			'render_callback' => 'megapress_customize_partial_blogdescription',
		) );
	}

	// Add a theme options section.
	$wp_customize->add_panel( 'megapress_theme_options' , array(
	  'title' => __( 'Theme Options', 'megapress' ),
	  'capability'     => 'edit_theme_options',
	  'priority' => 105, // Before Widgets.
	  'active_callback' => '',
	) );

	$wp_customize->add_section( 'megapress_banner', array(
		'priority'       => 110,
		'capability'     => 'edit_theme_options',
		'title'      => __('Banner Settings', 'megapress'),
		'active_callback' => '',
		'panel'  => 'megapress_theme_options',
	) );

	$wp_customize->add_setting( 'megapress_banner_type',
		array(
			'default'    => 'static',
			'transport'  => 'postMessage',
			'capability' => 'edit_theme_options',
			'sanitize_callback' => 'megapress_sanitize_select'
		) 
	);	


	$wp_customize->add_control(
		'megapress_banner_type', 
		array(
			'label'    => __( 'Banner Type?', 'megapress' ),
			'section'  => 'megapress_banner',
			'type'     => 'select',
			'choices'  => array(
				'static'  => __('Static Image', 'megapress'),
				'slideshow' => __('Slideshow', 'megapress'),
			),
		)
	);

	$wp_customize->add_setting( 'megapress_static_image' , array(
	    'default'   => '',
	    'transport' => 'postMessage',
	    'capability' => 'edit_theme_options',
	    'sanitize_callback' => 'megapress_sanitize_image'
	) );

	$wp_customize->add_control(
       new WP_Customize_Image_Control(
           $wp_customize,
           'megapress_static_image',
           array(
               'label'      => __( 'Upload an image', 'megapress' ),
               'section'    => 'megapress_banner',
               'settings'   => 'megapress_static_image',
               'description' => __( 'Suggested image dimension: 1920*1080','megapress' ),
               'active_callback' => 'megapress_banner_is_static'
           )
       )
   );


	$wp_customize->add_setting( 'megapress_slideshow' , array(
	    'default'   => 'select',
	    'transport' => 'postMessage',
	    'capability' => 'edit_theme_options',
	    'sanitize_callback' => 'megapress_sanitize_select'
	) );

	$wp_customize->add_control(	'megapress_slideshow', array(
			'label'    => __( 'Choose Post Categories to Select From:', 'megapress' ),
			'section'  => 'megapress_banner',
			'type'     => 'select',
			'choices'  => $megapress_categories,
			'active_callback' => 'megapress_banner_is_slideshow'
		)
	);	

	//homepage
	$wp_customize->add_setting( 'megapress_homepage_sidebar' , array(
	    'default'   => 'yes',
	    'transport' => 'refresh',
	    'capability' => 'edit_theme_options',
	    'sanitize_callback' => 'megapress_sanitize_select'
	) );

	$wp_customize->add_control(
		'megapress_homepage_sidebar', 
		array(
			'label'    => __( 'Display Sidebar ?', 'megapress' ),
			'section'  => 'static_front_page',
			'type'     => 'select',
			'choices'  => array(
				'yes'  => __('Yes', 'megapress'),
				'no' => __('No', 'megapress'),
			),
		)
	);

	$wp_customize->add_section( 'megapress_layout', array(
		'priority'       => 111,
		'capability'     => 'edit_theme_options',
		'title'      => __('Layout Settings', 'megapress'),
		'active_callback' => '',
		'panel'  => 'megapress_theme_options',
	) );

	$wp_customize->add_setting( 'megapress_layout_posts_home' , array(
	    'default'   => 'list',
	    'transport' => 'refresh',
	    'capability' => 'edit_theme_options',
	    'sanitize_callback' => 'megapress_sanitize_select'
	) );

	$wp_customize->add_control(
		'megapress_layout_posts_home', 
		array(
			'label'    => __( 'Home Page Posts Display Format', 'megapress' ),
			'section'  => 'megapress_layout',
			'type'     => 'select',
			'choices'  => array(
				'list'  => __('List', 'megapress'),
				'grid' => __('Grid', 'megapress'),
			),
		)
	);

	$wp_customize->add_setting( 'megapress_layout_posts_archive' , array(
	    'default'   => 'list',
	    'transport' => 'refresh',
	    'capability' => 'edit_theme_options',
	    'sanitize_callback' => 'megapress_sanitize_select'
	) );

	$wp_customize->add_control(
		'megapress_layout_posts_archive', 
		array(
			'label'    => __( 'Archive Posts Display Format', 'megapress' ),
			'section'  => 'megapress_layout',
			'type'     => 'select',
			'choices'  => array(
				'list'  => __('List', 'megapress'),
				'grid' => __('Grid', 'megapress'),
			),
		)
	);

	$wp_customize->add_setting( 'megapress_sidebar_pages' , array(
	    'default'   => 'no',
	    'transport' => 'refresh',
	    'capability' => 'edit_theme_options',
	    'sanitize_callback' => 'megapress_sanitize_select'
	) );

	$wp_customize->add_control(
		'megapress_sidebar_pages', 
		array(
			'label'    => __( 'Do pages have sidebar?', 'megapress' ),
			'section'  => 'megapress_layout',
			'type'     => 'select',
			'choices'  => array(
				'yes'  => __('Yes', 'megapress'),
				'no' => __('No', 'megapress'),
			),
		)
	);


	$wp_customize->add_setting( 'megapress_sidebar_archive' , array(
	    'default'   => 'no',
	    'transport' => 'refresh',
	    'capability' => 'edit_theme_options',
	    'sanitize_callback' => 'megapress_sanitize_select'
	) );

	$wp_customize->add_control(
		'megapress_sidebar_archive', 
		array(
			'label'    => __( 'Do archives have sidebar?', 'megapress' ),
			'section'  => 'megapress_layout',
			'type'     => 'select',
			'choices'  => array(
				'yes'  => __('Yes', 'megapress'),
				'no' => __('No', 'megapress'),
			),
		)
	);


	$wp_customize->add_setting( 'megapress_sidebar_post' , array(
	    'default'   => 'no',
	    'transport' => 'refresh',
	    'capability' => 'edit_theme_options',
	    'sanitize_callback' => 'megapress_sanitize_select'
	) );

	$wp_customize->add_control(
		'megapress_sidebar_post', 
		array(
			'label'    => __( 'Do single post have sidebar?', 'megapress' ),
			'section'  => 'megapress_layout',
			'type'     => 'select',
			'choices'  => array(
				'yes'  => __('Yes', 'megapress'),
				'no' => __('No', 'megapress'),
			),
		)
	);

	$wp_customize->add_section( 'megapress_footer', array(
		'priority'       => 112,
		'capability'     => 'edit_theme_options',
		'title'      => __('Footer Settings', 'megapress'),
		'active_callback' => '',
		'panel'  => 'megapress_theme_options',
	) );

	$wp_customize->add_setting( 'megapress_footer_copyright_text' , array(
	    'default'   => '',
	    'transport' => 'postMessage',
	    'capability' => 'edit_theme_options',
	    'sanitize_callback' => 'sanitize_textarea_field'
	) );

	$wp_customize->add_control( 'megapress_footer_copyright_text', 
		array(
			'label'    => __( 'Copyright Text', 'megapress' ),
			'section'  => 'megapress_footer',
			'type'     => 'textarea'
		)
	);


	
}
add_action( 'customize_register', 'megapress_customize_register' );

/**
 * Render the site title for the selective refresh partial.
 *
 * @return void
 */
function megapress_customize_partial_blogname() {
	bloginfo( 'name' );
}

/**
 * Render the site tagline for the selective refresh partial.
 *
 * @return void
 */
function megapress_customize_partial_blogdescription() {
	bloginfo( 'description' );
}

function megapress_banner_is_slideshow( $value ){

	if( 'slideshow' == get_theme_mod('megapress_banner_type') ) {
		return true;
	}
	return false;
	
}

function megapress_banner_is_static( $value ){

	if( 'static' == get_theme_mod('megapress_banner_type') ) {
		return true;
	}
	return false;
	
}

/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
function megapress_customize_preview_js() {
	wp_enqueue_script( 'megapress-customizer', get_template_directory_uri() . '/js/customizer.js', array( 'customize-preview' ), '20151215', true );
}
add_action( 'customize_preview_init', 'megapress_customize_preview_js' );

/**
 * Load dynamic logic for the customizer controls area.
 */
function megapress_panels_js() {
	wp_enqueue_script( 'megapress-customize-controls', get_theme_file_uri( '/js/customize-controls.js' ), array(), '20181231', true );
}
add_action( 'customize_controls_enqueue_scripts', 'megapress_panels_js' );