<?php
/**
 * Prato Store Theme Customizer
 *
 * @package prato_store
 */

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function prato_store_customize_register( $wp_customize ) {
	$wp_customize->get_setting( 'blogname' )->transport         = 'postMessage';
	$wp_customize->get_setting( 'blogdescription' )->transport  = 'postMessage';
	$wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';

	$prato_store_options = prato_store_theme_options();

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


	$prato_product_categories = get_terms( 'product_cat' );
	$product_count = count(array($prato_product_categories));
    if ( $product_count > 0 && !is_wp_error( $prato_product_categories )){
        $prato_select_categories= array();
        	$prato_select_categories[''] = __('Select Category','prato-store');
        foreach($prato_product_categories as $prato_product_category){
             $prato_select_categories[$prato_product_category->term_id] = $prato_product_category->name;
        }
    }
    else{
        $prato_select_categories = array(''=>__('Select Category','prato-store'));
    }

    $wp_customize->add_setting('prato_store_theme_options[site_title_show]',
        array(
            'type' => 'option',
            'default'        => true,
            'default' => $prato_store_options['site_title_show'],
            'sanitize_callback' => 'prato_store_sanitize_checkbox',
        )
    );

    $wp_customize->add_control('prato_store_theme_options[site_title_show]',
        array(
            'label' => esc_html__('Show Title & Tagline', 'prato-store'),
            'type' => 'Checkbox',
            'section' => 'title_tagline',

        )
    );


    $wp_customize->add_panel(
        'theme_options',
        array(
            'title' => esc_html__('Theme Options', 'prato-store'),
            'priority' => 2,
        )
    );


    /* Header Section */

    $wp_customize->add_section(
        'header_section',
        array(
            'title' => esc_html__( 'Header Section','prato-store' ),
            'panel'=>'theme_options',
            'capability'=>'edit_theme_options',
        )
    );
    $wp_customize->add_setting('prato_store_theme_options[header_hover]',
        array(
            'type' => 'option',
            'default'        => true,
            'default' => $prato_store_options['header_hover'],
            'sanitize_callback' => 'prato_store_sanitize_checkbox',
        )
    );

    $wp_customize->add_control('prato_store_theme_options[header_hover]',
        array(
            'label' => esc_html__('Check to Remove Header Hover effect', 'prato-store'),
            'type' => 'Checkbox',
            'section' => 'header_section',

        )
    );
    /* Banner Section */

    $wp_customize->add_section(
        'banner_section',
        array(
            'title' => esc_html__( 'Banner Section','prato-store' ),
            'panel'=>'theme_options',
            'capability'=>'edit_theme_options',
        )
    );




	$wp_customize->add_setting('prato_store_theme_options[banner_title]',
	    array(
	        'type' => 'option',
	        'sanitize_callback' => 'sanitize_text_field',
	    )
	);
	$wp_customize->add_control('banner_title',
	    array(
	        'label' => esc_html__('Title', 'prato-store'),
	        'type' => 'text',
	        'section' => 'banner_section',
	        'settings' => 'prato_store_theme_options[banner_title]',
	    )
	);
	$wp_customize->add_setting('prato_store_theme_options[title_size]',
	    array(
	        'type' => 'option',
	        'sanitize_callback' => 'absint',
			'default' => 70,
	    )
	);
	$wp_customize->add_control('title_size',
	    array(
	        'label' => esc_html__('Title Font Size (Min 20 - Max 80) - Default 70', 'prato-store'),
	        'type' => 'number',
	        'section' => 'banner_section',
	        'settings' => 'prato_store_theme_options[title_size]',
			'input_attrs' => array(
				'min' => 20,
				'max' => 80
			)
	    )
	);

	$wp_customize->add_setting('prato_store_theme_options[banner_button_txt]',
	array(
		'type' => 'option',
		'default' => $prato_store_options['banner_button_txt'],
		'sanitize_callback' => 'sanitize_text_field',
	)
);
$wp_customize->add_control('prato_store_theme_options[banner_button_txt]',
	array(
		'label' => esc_html__('Banner Button Text', 'prato-store'),
		'type' => 'text',
		'section' => 'banner_section',
		'settings' => 'prato_store_theme_options[banner_button_txt]',
	)
);
$wp_customize->add_setting('prato_store_theme_options[banner_button_url]',
	array(
		'type' => 'option',
		'default' => $prato_store_options['banner_button_url'],
		'sanitize_callback' => 'prato_store_sanitize_url',
	)
);
$wp_customize->add_control('prato_store_theme_options[banner_button_url]',
	array(
		'label' => esc_html__('Banner Button Link', 'prato-store'),
		'type' => 'text',
		'section' => 'banner_section',
		'settings' => 'prato_store_theme_options[banner_button_url]',
	)
);

	$wp_customize->add_setting('prato_store_theme_options[banner_bg_image]',
	    array(
	        'type' => 'option',
	        'sanitize_callback' => 'esc_url_raw',
	    )
	);
	$wp_customize->add_control(
	    new WP_Customize_Image_Control(
	        $wp_customize,
	        'prato_store_theme_options[banner_bg_image]',
	        array(
	            'label' => esc_html__('Add Background Image', 'prato-store'),
				'description' => esc_attr( __('Video Background is available in Premium version only', 'prato-store') ),
	            'section' => 'banner_section',
	            'settings' => 'prato_store_theme_options[banner_bg_image]',
	        ))
	);


	$wp_customize->add_setting('prato_store_theme_options[banner_height]',
	    array(
	        'type' => 'option',
	        'sanitize_callback' => 'absint',
	    )
	);
	$wp_customize->add_control('banner_height',
	    array(
	        'label' => esc_html__('Banner Height (Enter Numeric Value like 700) - Default Fullscreen', 'prato-store'),
	        'type' => 'number',
	        'section' => 'banner_section',
	        'settings' => 'prato_store_theme_options[banner_height]',
			'input_attrs' => array(
				'min' => 300,
				'max' => 800
			)
	    )
	);





    $wp_customize->add_section(
        'product_section',
        array(
            'title' => esc_html__( 'Product Section','prato-store' ),
            'panel'=>'theme_options',
            'capability'=>'edit_theme_options',
        )
    );
    $wp_customize->add_setting('prato_store_theme_options[product_section_show]',
        array(
            'type' => 'option',
            'default'        => true,
            'default' => $prato_store_options['product_section_show'],
            'sanitize_callback' => 'prato_store_sanitize_checkbox',
        )
    );

    $wp_customize->add_control('prato_store_theme_options[product_section_show]',
        array(
            'label' => esc_html__('Show Product Section', 'prato-store'),
            'type' => 'Checkbox',
            'priority' => 1,
            'section' => 'product_section',

        )
    );




	$wp_customize->add_setting('prato_store_theme_options[product_section_title]',
	    array(
	        'type' => 'option',
	        'sanitize_callback' => 'sanitize_text_field',
	    )
	);
	$wp_customize->add_control('product_section_title',
	    array(
	        'label' => esc_html__('Section Title', 'prato-store'),
	        'type' => 'text',
	        'section' => 'product_section',
	        'settings' => 'prato_store_theme_options[product_section_title]',
	    )
	);
	$wp_customize->add_setting( 'prato_store_theme_options[product_column]', array(
		'capability' => 'edit_theme_options',
		'default' => '4',
		'sanitize_callback' => 'prato_store_sanitize_radio',
		'type' => 'option',
  ) );

  $wp_customize->add_control( 'prato_store_theme_options[product_column]', array(
		'type' => 'radio',
		'section' => 'product_section',
		'label' => esc_attr( __('Choose Column', 'prato-store') ),
		'choices' => array(
		  '3' => esc_attr( __('3 Column', 'prato-store') ),
		  '4' => esc_attr( __('4 Column', 'prato-store') ),
		  '5' => esc_attr( __('5 Column', 'prato-store') ),
		),
	  ) );


	  $wp_customize->add_setting( 'noofproduct', array(
        'default' => '',
        'type' => 'theme_mod',
        'capability' => 'edit_theme_options',
        'transport' => '',
        'sanitize_callback' => 'prato_store_intval',
    ) );

    $wp_customize->add_control( 'noofproduct', array(
        'type' => 'range',
        'section' => 'product_section',
		'label' => __( 'Number of Products you want to show', 'prato-store' ),
		'description' => esc_attr( __('Max 10 Products can be shown in Free Version', 'prato-store') ),
    'input_attrs' => array(
            'min' => 1,
            'max' => 10,
            'step' => 1,
            ),
    ));

	function prato_store_intval( $value ) {
		return (int) $value;
	}

	  $wp_customize->add_setting( 'prato_store_theme_options[display_product]', array(
		'capability' => 'edit_theme_options',
		'default' => 'recent',
		'sanitize_callback' => 'prato_store_sanitize_radio',
		'type' => 'option',
  ) );

  $wp_customize->add_control( 'prato_store_theme_options[display_product]', array(
		'type' => 'radio',
		'section' => 'product_section',
		'label' => esc_attr( __('Product Types', 'prato-store') ),
		'choices' => array(
			'recent' => esc_attr( __('Recent Products', 'prato-store') ),
		  'featured' => esc_attr( __('Featured Products', 'prato-store') ),
		  'toprated' => esc_attr( __('Top Rated Products', 'prato-store') ),
		  'sale' => esc_attr( __('Sale Products', 'prato-store') ),
		),
	  ) );

	  $wp_customize->add_setting( 'prato_store_theme_options[add_to_cart]', array(
		'capability' => 'edit_theme_options',
		'default' => 'belowprice',
		'sanitize_callback' => 'prato_store_sanitize_radio',
		'type' => 'option',
  ) );

  $wp_customize->add_control( 'prato_store_theme_options[add_to_cart]', array(
		'type' => 'radio',
		'section' => 'product_section',
		'label' => esc_attr( __('Product Types', 'prato-store') ),
		'choices' => array(
			'overimage' => esc_attr( __('Show Add to Cart Button over Image on Hover', 'prato-store') ),
		  'belowprice' => esc_attr( __('Show Add to Cart Button below Price', 'prato-store') ),
		),
	  ) );





    $wp_customize->add_section(
        'product_cat_section',
        array(
            'title' => esc_html__( 'Product Category Section','prato-store' ),
            'panel'=>'theme_options',
            'capability'=>'edit_theme_options',
        )
    );


    $wp_customize->add_setting('prato_store_theme_options[category_item_show]',
        array(
            'type' => 'option',
            'default'        => true,
            'default' => $prato_store_options['category_item_show'],
            'sanitize_callback' => 'prato_store_sanitize_checkbox',
        )
    );

    $wp_customize->add_control('prato_store_theme_options[category_item_show]',
        array(
            'label' => esc_html__('Product Category Section Options', 'prato-store'),
            'type' => 'Checkbox',
            'priority' => 1,
            'section' => 'product_cat_section',

        )
    );


	$wp_customize->add_setting( 'prato_store_category_dropdown1', array(
		'capability' => 'edit_theme_options',
		'default' => 'Uncategorized',
		'sanitize_callback' => 'prato_store_sanitize_select',
	) );
	
		//Register the control
	$wp_customize->add_control( 'prato_store_category_dropdown1', array(
		'type' => 'select',
		'section' => 'product_cat_section', 
		'label' => __( 'Select category 1' , 'prato-store'),
		'choices' => $prato_select_categories, //Add the list with options
	) ); 

	$wp_customize->add_setting( 'prato_store_category_dropdown2', array(
		'capability' => 'edit_theme_options',
		'default' => 'Uncategorized',
		'sanitize_callback' => 'prato_store_sanitize_select',
	) );
	
		//Register the control
	$wp_customize->add_control( 'prato_store_category_dropdown2', array(
		'type' => 'select',
		'section' => 'product_cat_section', 
		'label' => __( 'Select category 2' , 'prato-store'),
		'choices' => $prato_select_categories, //Add the list with options
	) ); 


	$wp_customize->add_setting( 'prato_store_theme_options[category_layout]', array(
		'capability' => 'edit_theme_options',
		'default' => 'layout1',
		'sanitize_callback' => 'prato_store_sanitize_radio',
		'type' => 'option',
  ) );

  $wp_customize->add_control( 'prato_store_theme_options[category_layout]', array(
		'type' => 'radio',
		'section' => 'product_cat_section',
		'label' => esc_attr( __('Product Category Layout', 'prato-store') ),
		'description' => esc_attr( __('Only 2 Product Categories can be selected in Free Version and Shop Now Text can not be changed in Free Version', 'prato-store') ),
		'choices' => array(
			'layout1' => esc_attr( __('Layout 1', 'prato-store') ),
		  'layout2' => esc_attr( __('Layout 2', 'prato-store') ),
		),
	  ) );




    /* CTA Section */

    $wp_customize->add_section(
        'cta_section',
        array(
            'title' => esc_html__( 'Call to Action Section','prato-store' ),
            'panel'=>'theme_options',
            'capability'=>'edit_theme_options',
        )
    );


    $wp_customize->add_setting('prato_store_theme_options[cta_show]',
        array(
            'type' => 'option',
            'default'        => true,
            'default' => $prato_store_options['cta_show'],
            'sanitize_callback' => 'prato_store_sanitize_checkbox',
        )
    );



    $wp_customize->add_control('prato_store_theme_options[cta_show]',
        array(
            'label' => esc_html__('Show CTA Section', 'prato-store'),
            'type' => 'Checkbox',
            'priority' => 1,
            'section' => 'cta_section',

        )
    );
	$wp_customize->add_setting('prato_store_theme_options[cta_title]',
	    array(
	        'type' => 'option',
	        'sanitize_callback' => 'sanitize_text_field',
	    )
	);
	$wp_customize->add_control('cta_title',
	    array(
	        'label' => esc_html__('Title', 'prato-store'),
	        'type' => 'text',
	        'section' => 'cta_section',
	        'settings' => 'prato_store_theme_options[cta_title]',
	    )
	);

	
	$wp_customize->add_setting('prato_store_theme_options[cta_subtitle]',
	    array(
	        'type' => 'option',
	        'sanitize_callback' => 'sanitize_text_field',
	    )
	);
	$wp_customize->add_control('cta_subtitle',
	    array(
	        'label' => esc_html__('Description', 'prato-store'),
	        'type' => 'text',
	        'section' => 'cta_section',
	        'settings' => 'prato_store_theme_options[cta_subtitle]',
	    )
	);

	$wp_customize->add_setting('prato_store_theme_options[cta_button_txt]',
	    array(
	        'type' => 'option',
	        'default' => $prato_store_options['cta_button_txt'],
	        'sanitize_callback' => 'sanitize_text_field',
	    )
	);
	$wp_customize->add_control('prato_store_theme_options[cta_button_txt]',
	    array(
	        'label' => esc_html__('CTA Button Text', 'prato-store'),
	        'type' => 'text',
	        'section' => 'cta_section',
	        'settings' => 'prato_store_theme_options[cta_button_txt]',
	    )
	);
	$wp_customize->add_setting('prato_store_theme_options[cta_button_url]',
	    array(
	        'type' => 'option',
	        'default' => $prato_store_options['cta_button_url'],
	        'sanitize_callback' => 'prato_store_sanitize_url',
	    )
	);
	$wp_customize->add_control('prato_store_theme_options[cta_button_url]',
	    array(
	        'label' => esc_html__('CTA Button Link', 'prato-store'),
	        'type' => 'text',
	        'section' => 'cta_section',
	        'settings' => 'prato_store_theme_options[cta_button_url]',
	    )
	);


	$wp_customize->add_setting('prato_store_theme_options[cta_bg_image]',
	    array(
	        'type' => 'option',
	        'sanitize_callback' => 'esc_url_raw',
	    )
	);
	$wp_customize->add_control(
	    new WP_Customize_Image_Control(
	        $wp_customize,
	        'prato_store_theme_options[cta_bg_image]',
	        array(
	            'label' => esc_html__('Add Background Image', 'prato-store'),
	            'section' => 'cta_section',
	            'settings' => 'prato_store_theme_options[cta_bg_image]',
	        ))
	);






    /* Blog Section */

    $wp_customize->add_section(
        'blog_section',
        array(
            'title' => esc_html__( 'Blog Section','prato-store' ),
            'panel'=>'theme_options',
            'capability'=>'edit_theme_options',
        )
    );

    $wp_customize->add_setting('prato_store_theme_options[blog_show]',
        array(
            'type' => 'option',
            'default'        => true,
            'default' => $prato_store_options['blog_show'],
            'sanitize_callback' => 'prato_store_sanitize_checkbox',
        )
    );

    $wp_customize->add_control('prato_store_theme_options[blog_show]',
        array(
            'label' => esc_html__('Show Blog Section', 'prato-store'),
            'type' => 'Checkbox',
            'priority' => 1,
            'section' => 'blog_section',

        )
    );



	$wp_customize->add_setting('prato_store_theme_options[blog_title]',
	    array(
	        'capability' => 'edit_theme_options',
	        'default' => $prato_store_options['blog_title'],
	        'sanitize_callback' => 'sanitize_text_field',
	        'type' => 'option',
	    )
	);
	$wp_customize->add_control('prato_store_theme_options[blog_title]',
	    array(
	        'label' => esc_html__('Section Title', 'prato-store'),
	        'priority' => 1,
	        'section' => 'blog_section',
	        'type' => 'text',
	    )
	);

	$wp_customize->add_setting('prato_store_theme_options[blog_desc]',
	    array(
	        'default' => $prato_store_options['blog_desc'],
	        'type' => 'option',
	        'sanitize_callback' => 'sanitize_text_field'
	    )
	);

	$wp_customize->add_control('prato_store_theme_options[blog_desc]',
	    array(
	        'label' => esc_html__('Blog Section Description', 'prato-store'),
	        'type' => 'text',
	        'section' => 'blog_section',
	        'settings' => 'prato_store_theme_options[blog_desc]',
	    )
	);

	$wp_customize->add_setting('prato_store_theme_options[blog_category]', array(
	    'default' => $prato_store_options['blog_category'],
	    'type' => 'option',
	    'sanitize_callback' => 'prato_store_sanitize_select',
	    'capability' => 'edit_theme_options',

	));

	$wp_customize->add_control(new prato_store_Dropdown_Customize_Control(
	    $wp_customize, 'prato_store_theme_options[blog_category]',
	    array(
	        'label' => esc_html__('Select Blog Category', 'prato-store'),
	        'section' => 'blog_section',
	        'choices' => prato_store_get_categories_select(),
	        'settings' => 'prato_store_theme_options[blog_category]',
	    )
	));
	$wp_customize->add_setting('prato_store_theme_options[small_blog_img]',
	array(
		'type' => 'option',
		'default'        => true,
		'default' => $prato_store_options['small_blog_img'],
		'sanitize_callback' => 'prato_store_sanitize_checkbox',
	)
);

$wp_customize->add_control('prato_store_theme_options[small_blog_img]',
	array(
		'label' => esc_html__('Check to Show small blog Image', 'prato-store'),
		'type' => 'Checkbox',
		'section' => 'blog_section',

	)
);
     function prato_store_sanitize_checkbox( $input ) {
        if ( true === $input ) {
            return 1;
         } else {
            return 0;
         }
    }

    /* Blog Section */

    $wp_customize->add_section(
        'prefooter_section',
        array(
            'title' => esc_html__( 'Footer Section','prato-store' ),
            'panel'=>'theme_options',
            'capability'=>'edit_theme_options',
        )
    );

    $wp_customize->add_setting('prato_store_theme_options[show_prefooter]',
        array(
            'type' => 'option',
            'default'        => true,
            'default' => $prato_store_options['show_prefooter'],
            'sanitize_callback' => 'prato_store_sanitize_checkbox',
        )
    );

    $wp_customize->add_control('prato_store_theme_options[show_prefooter]',
        array(
            'label' => esc_html__('Show Prefooter Section', 'prato-store'),
			'description' => esc_attr( __('Copyright Text can be changed in only Premium Version', 'prato-store') ),
            'type' => 'Checkbox',
            'priority' => 1,
            'section' => 'prefooter_section',

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

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

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

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