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

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

	$wp_customize->add_panel('themefarmer_fontpage_panel', array(
		'title'    => __('Frontpage Sections', 'storez'),
		'priority' => 30,
	));

	

	if(class_exists('WooCommerce')){
		$wp_customize->add_section('storez_frontpage_categories_section', array(
			'title'    => __('Featured Categories', 'storez'),
			'priority' => 170,
			'panel'    => 'themefarmer_fontpage_panel',
		));
		$cat_options = array('' => esc_html__( 'Select Category', 'storez' ));
		$prod_categories = get_terms( 'product_cat', array(
				        'orderby'    => 'name',
				        'order'      => 'ASC',
				        'parent' 	 => 0,
				        'number'	 => 999,
				        'hide_empty' => true
				    ));
		foreach ($prod_categories as $key => $acat) {
			$cat_options[$acat->term_id]=$acat->name;
		}

		for ($i=1; $i <= 4; $i++){

			$wp_customize->add_setting('storez_frontpage_cat_id_'.$i, array(
				'sanitize_callback' => 'absint',
			));

			$wp_customize->add_control('storez_frontpage_cat_id_'.$i, array(
				'type'     => 'select',
				'section'  => 'storez_frontpage_categories_section',
				'label'      => sprintf(esc_html__( 'Featured Category %s', 'storez' ), $i),
				'choices'  => $cat_options,
			));
		}
	}



	$wp_customize->add_panel('storez_header_options_panel', array(
		'priority' => 30,
		'title'    => __('Header Options', 'storez'),
	));
	
	/*types*/
	$wp_customize->add_section('storez_header_options_section', array(
		'title'    => __('Header Options', 'storez'),
		'priority' => 10,
		'panel'    => 'storez_header_options_panel',
	));

	$wp_customize->add_setting('storez_sticky_header_enable', array(
		'sanitize_callback' => 'storez_sanitize_checkbox',
		'default'           => true,
	));

	$wp_customize->add_control('storez_sticky_header_enable', array(
		'type'     => 'checkbox',
		'priority' => 10,
		'section'  => 'storez_header_options_section',
		'label'    => __('Sticky Header Enable/Disable', 'storez'),
	));

	$wp_customize->add_setting('storez_catalogue_menu_text', array(
		'sanitize_callback' => 'sanitize_text_field',
		'default'           => esc_html__('Products Catalogue','storez'),
		'transport'         => 'postMessage',
	));

	$wp_customize->add_control('storez_catalogue_menu_text', array(
		'type'     => 'text',
		'priority' => 10,
		'section'  => 'storez_header_options_section',
		'label'    => __('Sticky Header Enable/Disable', 'storez'),
	));

	/*types*/
	

	/*topbar*/
	$wp_customize->add_section('storez_topbar_section', array(
		'title'    => __('Topbar Options', 'storez'),
		'priority' => 10,
		'panel'    => 'storez_header_options_panel',
	));

	$wp_customize->add_setting('storez_topbar_enable', array(
		'sanitize_callback' => 'storez_sanitize_checkbox',
		'default'           => true,
	));

	$wp_customize->add_control('storez_topbar_enable', array(
		'type'     => 'checkbox',
		'priority' => 10,
		'section'  => 'storez_topbar_section',
		'label'    => __('Topbar Enable/Disable', 'storez'),
	));

	$wp_customize->add_setting('storez_top_email', array(
		'sanitize_callback' => 'sanitize_email',
	));

	$wp_customize->add_control('storez_top_email', array(
		'type'    => 'email',
		'section' => 'storez_topbar_section',
		'label'   => __('Email', 'storez'),
	));

	$wp_customize->add_setting('storez_top_phone', array(
		'sanitize_callback' => 'sanitize_text_field',
	));

	$wp_customize->add_control('storez_top_phone', array(
		'type'     => 'text',
		'priority' => 200,
		'section'  => 'storez_topbar_section',
		'label'    => __('Phone', 'storez'),
	));

	$wp_customize->add_setting('storez_top_address', array(
		'sanitize_callback' => 'sanitize_text_field',
	));

	$wp_customize->add_control('storez_top_address', array(
		'type'     => 'textarea',
		'priority' => 200,
		'section'  => 'storez_topbar_section',
		'label'    => __('Address', 'storez'),
	));
	/*topbar*/

	/*Layout Options*/
	

	$wp_customize->add_section('storez_site_layouts_section', array(
		'title'    => __('Site Layout Options', 'storez'),
		'panel'    => 'storez_layout_panel',
		'priority' => 10,
	));

	$wp_customize->add_setting('storez_site_layout_type', array(
		'sanitize_callback' => 'sanitize_text_field',
		'transport'         => 'postMessage',
		'default' => 'full',
	));

	$wp_customize->add_control('storez_site_layout_type', array(
		'type'     => 'select',
		'section'  => 'storez_site_layouts_section',
		'label'    => __('Site Layout', 'storez'),
		'choices' => array(
			'full' => esc_html__( 'Full Width', 'storez' ),
			'boxed' => esc_html__( 'Boxed', 'storez' ),
		)
	));
	
	

	if (class_exists('ThemeFarmer_Field_Image_Select')) {
		$wp_customize->add_panel('storez_layout_panel', array(
			'title'    => __('Layouts Optinos', 'storez'),
			'priority' => 30,
		));

		$wp_customize->add_section('storez_blog_page_layouts_section', array(
			'title'    => __('Blog Layout', 'storez'),
			'panel'    => 'storez_layout_panel',
			'priority' => 10,
		));

		$wp_customize->add_setting('storez_blog_post_index_layout', array(
			'sanitize_callback' => 'sanitize_text_field',
			'default'           => 'right',
		));

		$wp_customize->add_control(new ThemeFarmer_Field_Image_Select($wp_customize, 'storez_blog_post_index_layout', array(
			'label'   => __('All Posts Page Layout', 'storez'),
			'section' => 'storez_blog_page_layouts_section',
			'choices' => array(
				'left'  => esc_url(get_template_directory_uri() . '/images/layout/2cleft.png'),
				'full'  => esc_url(get_template_directory_uri() . '/images/layout/full.png'),
				'right' => esc_url(get_template_directory_uri() . '/images/layout/2cright.png'),
			),
		)));

		$wp_customize->add_setting('storez_blog_single_post_layout', array(
			'sanitize_callback' => 'sanitize_text_field',
			'default'           => 'right',
		));

		$wp_customize->add_control(new ThemeFarmer_Field_Image_Select($wp_customize, 'storez_blog_single_post_layout', array(
			'label'   => __('Single Post Layout', 'storez'),
			'section' => 'storez_blog_page_layouts_section',
			'choices' => array(
				'left'  => esc_url(get_template_directory_uri() . '/images/layout/2cleft.png'),
				'full'  => esc_url(get_template_directory_uri() . '/images/layout/full.png'),
				'right' => esc_url(get_template_directory_uri() . '/images/layout/2cright.png'),
			),
		)));

		$wp_customize->add_setting('storez_blog_single_page_layout', array(
			'sanitize_callback' => 'sanitize_text_field',
			'default'           => 'right',
		));

		$wp_customize->add_control(new ThemeFarmer_Field_Image_Select($wp_customize, 'storez_blog_single_page_layout', array(
			'label'   => __('Single Page Layout', 'storez'),
			'section' => 'storez_blog_page_layouts_section',
			'choices' => array(
				'left'  => esc_url(get_template_directory_uri() . '/images/layout/2cleft.png'),
				'full'  => esc_url(get_template_directory_uri() . '/images/layout/full.png'),
				'right' => esc_url(get_template_directory_uri() . '/images/layout/2cright.png'),
			),
		)));

		if (class_exists('WooCommerce')) {
			$wp_customize->add_section('storez_shop_page_layouts_section', array(
				'title'    => __('Shop Layout', 'storez'),
				'panel'    => 'storez_layout_panel',
				'priority' => 10,
			));

			$wp_customize->add_setting('storez_shop_index_layout', array(
				'sanitize_callback' => 'sanitize_text_field',
				'default'           => 'left',
			));

			$wp_customize->add_control(new ThemeFarmer_Field_Image_Select($wp_customize, 'storez_shop_index_layout', array(
				'label'   => __('Shop Layout', 'storez'),
				'section' => 'storez_shop_page_layouts_section',
				'choices' => array(
					'left'  => esc_url(get_template_directory_uri() . '/images/layout/2cleft.png'),
					'full'  => esc_url(get_template_directory_uri() . '/images/layout/full.png'),
					'right' => esc_url(get_template_directory_uri() . '/images/layout/2cright.png'),
				),
			)));

			$wp_customize->add_setting('storez_shop_single_product_layout', array(
				'sanitize_callback' => 'sanitize_text_field',
				'default'           => 'full',
			));

			$wp_customize->add_control(new ThemeFarmer_Field_Image_Select($wp_customize, 'storez_shop_single_product_layout', array(
				'label'   => __('Single Product Layout', 'storez'),
				'section' => 'storez_shop_page_layouts_section',
				'choices' => array(
					'left'  => esc_url(get_template_directory_uri() . '/images/layout/2cleft.png'),
					'full'  => esc_url(get_template_directory_uri() . '/images/layout/full.png'),
					'right' => esc_url(get_template_directory_uri() . '/images/layout/2cright.png'),
				),
			)));
		}
	}


			

	/*Layout Options*/

	$frontpage_sections = array('sidebar-widgets-front-page-top-widget-area', 'sidebar-widgets-front-page-products-widget-area', 'sidebar-widgets-front-page-widget-area-column');
	foreach ($frontpage_sections as $key => $asection_id) {
		$section = $wp_customize->get_section($asection_id);
		if($section){
			$section->panel = 'themefarmer_fontpage_panel';
			$section->priority = 200;
		}
	}

	if(class_exists('StoreZ_Upsale_Customize_Section1')){
		$wp_customize->add_section(new StoreZ_Upsale_Customize_Section1($wp_customize, 'storez-upsell-section', array(
			'priority' => 999,
			'title'    => __('UPGRADE TO PRO', 'storez'),
			'pro_text' => __('Go Pro', 'storez'),
			'pro_url'  => 'https://themefarmer.com/product/storez-pro/',
		)));
	}
	
	$wp_customize->get_section('header_image')->panel         = 'storez_header_options_panel';
	$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' => 'storez_customize_partial_blogname',
		));
		
		$wp_customize->selective_refresh->add_partial('blogdescription', array(
			'selector'        => '.site-description',
			'render_callback' => 'storez_customize_partial_blogdescription',
		));

		$wp_customize->selective_refresh->add_partial('storez_catalogue_menu_text', array(
			'selector'        => 'button.product-catalogue-heading span',
			'render_callback' => 'storez_customize_partial_catalogue_menu_text',
		));
	}


	$wp_customize->add_setting('storez_theme_link_color', array(
		'sanitize_callback' => 'sanitize_text_field',
		'transport'         => 'refresh',
		'default'           => '#606060',
	));

	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'storez_theme_link_color', array(
		'label'   => __('Link Color', 'storez'),
		'section' => 'colors',
	)));

	$wp_customize->add_setting('storez_theme_link_hover_color', array(
		'sanitize_callback' => 'sanitize_text_field',
		'transport'         => 'refresh',
		'default'           => '#606060',
	));

	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'storez_theme_link_hover_color', array(
		'label'   => __('Link Hover Color', 'storez'),
		'section' => 'colors',
	)));

	$wp_customize->add_setting('storez_theme_post_title_color', array(
		'sanitize_callback' => 'sanitize_text_field',
		'transport'         => 'refresh',
		'default'           => '#606060',
	));

	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'storez_theme_post_title_color', array(
		'label'   => __('Article Title Color', 'storez'),
		'section' => 'colors',
	)));

	$wp_customize->add_setting('storez_theme_post_title_hover_color', array(
		'sanitize_callback' => 'sanitize_text_field',
		'transport'         => 'refresh',
		'default'           => '#606060',
	));

	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'storez_theme_post_title_hover_color', array(
		'label'   => __('Article Title Hover Color', 'storez'),
		'section' => 'colors',
	)));

	$wp_customize->add_setting('storez_theme_post_meta_color', array(
		'sanitize_callback' => 'sanitize_text_field',
		'transport'         => 'refresh',
		'default'           => '#606060',
	));

	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'storez_theme_post_meta_color', array(
		'label'   => __('Article Meta Color', 'storez'),
		'section' => 'colors',
	)));

	$wp_customize->add_setting('storez_theme_post_meta_hover_color', array(
		'sanitize_callback' => 'sanitize_text_field',
		'transport'         => 'refresh',
		'default'           => '#606060',
	));

	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'storez_theme_post_meta_hover_color', array(
		'label'   => __('Article Meta Hover Color', 'storez'),
		'section' => 'colors',
	)));


	$wp_customize->add_setting('storez_product_disable_zoom_feature', array(
		'sanitize_callback' => 'storez_sanitize_checkbox',
		'transport'         => 'refresh',
		'default'           => true,

	));

	$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'storez_product_disable_zoom_feature', array(
		'label'   => __('New "product image zoom magnifier"', 'storez'),
		'section' => 'woocommerce_product_images',
		'type'     => 'checkbox',
		'priority' => 2,
	)));
}
add_action('customize_register', 'storez_customize_register');

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

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


function storez_customize_partial_catalogue_menu_text(){
	return get_theme_mod('storez_catalogue_menu_text');
}

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

if (class_exists('WP_Customize_Section')){
	class StoreZ_Upsale_Customize_Section extends WP_Customize_Section {
		public $type = 'themefarmer-upsell';
		public function render() {
			$classes = 'accordion-section control-section-' . $this->type;
			$id      = 'themefarmer-upsell-buttons-section';
			?>
		    <li id="accordion-section-<?php echo esc_attr($this->id); ?>"class="<?php echo esc_attr($classes); ?>">
		        <div class="themefarmer-upsale">
		          	<?php esc_html_e( 'UPGRADE TO PRO', 'storez' ); ?><a href="<?php echo esc_url('https://themefarmer.com/product/storez-pro/'); ?>" target="_blank" class="themefarmer-upsale-bt" id="themefarmer-pro-button"><?php esc_html_e('Go Pro', 'storez');?></a>
		        </div>
		    </li>
		    <?php
		}
	}


	class StoreZ_Upsale_Customize_Section1 extends WP_Customize_Section {
		public $type = 'themefarmer-upsell-button';
		public $pro_text = '';
		public $pro_url = '';
		public function render() {
			?>
		    <li id="accordion-section-<?php echo esc_attr($this->id); ?>" class="accordion-section control-section cannot-expand <?php echo esc_attr('control-section-' . $this->type); ?>" style="display: block;">
		        <h3 class="accordion-section-title storez-upsale">
		        	<?php echo esc_html($this->title); ?>
		        	<?php if($this->pro_text && $this->pro_url): ?>
		          	<a href="<?php echo esc_url($this->pro_url); ?>" target="_blank" class="theme-upsale-button button button-secondary alignright" id="storez-pro-button"><?php echo esc_html($this->pro_text); ?></a>
		          	<?php endif; ?>
		        </h3>
		    </li>
		    <?php
		}
	}
}