<?php

function constructionarc_themes_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';
	$wp_customize->get_control('header_textcolor')->section = "title_tagline";
	
	$wp_customize->get_section( 'static_front_page' )->title 	= esc_html__('Enable (Home) Front Page', 'construction-arc');
	$wp_customize->get_section( 'static_front_page' )->priority = 12;

	
	$wp_customize->register_control_type('constructionarc_Custom_Control_Tab');
	$wp_customize->register_control_type('constructionarc_Background_Control');
    $wp_customize->register_control_type('constructionarc_Range_Slider_Control');
    $wp_customize->register_control_type('constructionarc_Sortable_Control');
    $wp_customize->register_control_type('constructionarc_Custom_Control_Buttonset');
	$wp_customize->register_section_type('constructionarc_Toggle_Section');
	$wp_customize->register_section_type('constructionarc_Themes_Upgrade_Section');

	// Register custom section types.
	$wp_customize->register_section_type( 'constructionarc_Themes_Customize_Section' );
	$wp_customize->add_section(
		new constructionarc_Themes_Customize_Section(
			$wp_customize,
			'constructionarc-info',
			array(
				// 'title' => esc_html__('35% Off Use Coupon Code : NEW2023 Validity : DEC 26 - JAN 10', 'construction-arc'),
				'pro_text' => esc_html__( 'Upgrade To Pro','construction-arc' ),
				'pro_url'  => apply_filters('constructionarc-link', esc_url('https://ikreatethemes.com/wordpress-theme/construction-wordpress-theme/') ),
				'priority'  => -1,
			)
		)
	);
	
    require get_template_directory() . '/inc/customizer/customizer-panel/social-settings.php';
    require get_template_directory() . '/inc/customizer/customizer-panel/quick-info.php';
	require get_template_directory() . '/inc/customizer/customizer-panel/footer.php';
	require get_template_directory() . '/inc/customizer/customizer-panel/colors.php';
	
	$wp_customize->add_setting('constructionarc_enable_frontpage', array(
        'default' => 'disable',
        'sanitize_callback' => 'constructionarc_themes_sanitize_switch',  
    ));
    $wp_customize->add_control(new constructionarc_Switch_Control($wp_customize, 'constructionarc_enable_frontpage', array(
        'label' => esc_html__('Enable FrontPage', 'construction-arc'),
        'settings' => 'constructionarc_enable_frontpage',
		'description' => sprintf(esc_html__('Overwrites the homepage displays setting and shows the frontpage for Customizer %s', 'construction-arc'), '<a href="javascript:wp.customize.panel(\'constructionarc_frontpage_settings\').focus()">' . esc_html__('Front Page Sections', 'construction-arc') . '</a>') . '<br/><br/>' . esc_html__('Do not enable this option if you want to use Elementor in home page.', 'construction-arc'),
        'section' => 'static_front_page',
        'switch_label' => array(
            'enable' => esc_html__('On', 'construction-arc'),
            'disable' => esc_html__('Off', 'construction-arc'),
        ),
    )));
    
    
	$pages = array();
	$pages_obj = get_pages();
	$pages[''] = esc_html__('Select Page', 'construction-arc');
	foreach ($pages_obj as $page) {
	    $pages[$page->ID] = $page->post_title;
	}
	$blog_cat = constructionarc_themes_post_category();
	
	$wp_customize->add_panel('constructionarc_header_settings', array(
		'title'		=>	esc_html__('Header Settings','construction-arc'),
		'priority'	=>	10,
	));

	$wp_customize->get_section( 'title_tagline' )->panel = 'constructionarc_header_settings';


	$wp_customize->add_setting('constructionarc_pro_title_tagline', array(
		'sanitize_callback' => 'constructionarc_sanitize_text'
	));
	$wp_customize->add_control(new constructionarc_Themes_Upgrade_Text($wp_customize, 'constructionarc_pro_title_tagline', array(
		'section' => 'title_tagline',
		'label' => esc_html__('For More Settings,', 'construction-arc'),
		'choices' => array(
			esc_html__('Title, Tagline & Logo Alignment', 'construction-arc'),
			esc_html__('Title, Tagline Color Options', 'construction-arc')
		),
		'priority' => 250,
	)));


	/** Page Sidebar */
	$wp_customize->add_section('constructionarc_sidebar', array(
		'title'		=>	esc_html__('Page Sidebar Settings','construction-arc'),
		'panel'		=> 'constructionarc_general_settings_panel',
	));

	$wp_customize->add_setting('constructionarc_page_sidebar', array(
		'default' => 'no',
		'sanitize_callback' => 'constructionarc_themes_sanitize_options'
	));
	$wp_customize->add_control(new constructionarc_Selector($wp_customize, 'constructionarc_page_sidebar', array(
		'section' => 'constructionarc_sidebar',
		'label' => esc_html__('Page Layout Setting', 'construction-arc'),
		'options' => array(
			'no' => get_template_directory_uri() . '/inc/customizer/images/no-sidebar.png',
			'left' => get_template_directory_uri() . '/inc/customizer/images/left-sidebar.png',
			'right' => get_template_directory_uri() . '/inc/customizer/images/right-sidebar.png',
		)
	)));

	/** Upgrade Pro Version */
	$wp_customize->add_setting('constructionarc_pro_upgrade_sidebar_sticky', array(
		'sanitize_callback' => 'constructionarc_sanitize_text'
	));
	$wp_customize->add_control(new constructionarc_Themes_Upgrade_Text($wp_customize, 'constructionarc_pro_upgrade_sidebar_sticky', array(
		'section' => 'constructionarc_sidebar',
		'label' => esc_html__('For More Settings,', 'construction-arc'),
		'choices' => array(
			esc_html__('Sticky widget sidebar options', 'construction-arc'),
		),
		'priority' => 250,
	)));
	
	require get_template_directory() . '/inc/customizer/customizer-panel/general-settings.php';
	require get_template_directory() . '/inc/customizer/customizer-panel/top-header.php';
	require get_template_directory() . '/inc/customizer/customizer-panel/header.php';
	require get_template_directory() . '/inc/customizer/customizer-panel/header-cta.php';
	require get_template_directory() . '/inc/customizer/customizer-panel/blog.php';
	
	$wp_customize->add_panel('constructionarc_frontpage_settings', array(
		'title'		=>	esc_html__('Home Section','construction-arc'),
		'priority'	=>	35,
		'description' => esc_html__('Drag and Drop to Reorder', 'construction-arc'). '<img class="constructionarc_light-drag-spinner" src="'.admin_url('/images/spinner.gif').'">',
	));

	require get_template_directory() . '/inc/customizer/customizer-panel/home/common-settings.php';
	require get_template_directory() . '/inc/customizer/customizer-panel/home/home-slider-settings.php';
	require get_template_directory() . '/inc/customizer/customizer-panel/home/home-about-settings.php';
	require get_template_directory() . '/inc/customizer/customizer-panel/home/home-promoservices-settings.php';
	require get_template_directory() . '/inc/customizer/customizer-panel/home/home-cta-settings.php';
	require get_template_directory() . '/inc/customizer/customizer-panel/home/home-services-settings.php';
	require get_template_directory() . '/inc/customizer/customizer-panel/home/home-counter-settings.php';
	require get_template_directory() . '/inc/customizer/customizer-panel/home/home-video-settings.php';
	require get_template_directory() . '/inc/customizer/customizer-panel/home/home-recentwork-settings.php';
	//require get_template_directory() . '/inc/customizer/customizer-panel/home/home-howitworks-settings.php';
	require get_template_directory() . '/inc/customizer/customizer-panel/home/home-testimonial-settings.php';
	require get_template_directory() . '/inc/customizer/customizer-panel/home/home-team-settings.php';
	require get_template_directory() . '/inc/customizer/customizer-panel/home/home-client-settings.php';
	require get_template_directory() . '/inc/customizer/customizer-panel/home/home-blog-settings.php';
	require get_template_directory() . '/inc/customizer/customizer-panel/home/home-customa-settings.php';
	require get_template_directory() . '/inc/customizer/customizer-panel/home/home-contact-settings.php';
	require get_template_directory() . '/inc/customizer/customizer-panel/home/breadcrumb.php';	

	/****** Upgrade Pro in Front Page Section */
	$wp_customize->add_section(new constructionarc_Themes_Upgrade_Section($wp_customize, 'constructionarc-frontpage-notice', array(
		'title' => sprintf(esc_html__('Important! Home Page Sections are not enabled. Enable it %1shere%2s.', 'construction-arc'), '<a href="javascript:wp.customize.section( \'static_front_page\' ).focus()">', '</a>'),
		'priority' => -1,
		'class' => 'ikthemes-enable-front',
		'panel' => 'constructionarc_frontpage_settings',
	)));

	$wp_customize->add_section(new constructionarc_Themes_Upgrade_Section($wp_customize, 'constructionarc_frontpage_upgrade_pro_section', array(
		'title' => esc_html__('More Sections on Premium', 'construction-arc'),
		'panel' => 'constructionarc_frontpage_settings',
		'priority' => 500,
		'class' => 'ikthemes-upgrade-boxed',
		'options' => array(
			esc_html__('- All above section with more styles and customization options', 'construction-arc'),
			esc_html__('- Multiple Services Layouts', 'construction-arc'),
			esc_html__('- News and Events Section', 'construction-arc'),
			esc_html__('- Free Hand Text (HTML)', 'construction-arc'),
			esc_html__('- Highlight Section', 'construction-arc'),
			esc_html__('- How It Works Section', 'construction-arc'),
			esc_html__('- Portfolio Section', 'construction-arc'),
			esc_html__('- WooCommerce Section', 'construction-arc'),
			esc_html__('- Pricing Table Section', 'construction-arc'),
			esc_html__('- Tab Section', 'construction-arc'),
			esc_html__('- Custom Section B', 'construction-arc'),
			esc_html__('- 40+ Elementor Widgets', 'construction-arc'),
			esc_html__('- Advanced Typography Settings', 'construction-arc'),
			esc_html__('----------------------------------------------- Many More Sections ---------', 'construction-arc'),
			esc_html__('All the above sections can be created with Elementor block page builder or customizer whichever you prefer.', 'construction-arc'),
		),
		'upgrade_text' => esc_html__('Upgrade to Pro', 'construction-arc'),
		'upgrade_url' => apply_filters('constructionarc-link', esc_url('https://ikreatethemes.com/wordpress-theme/construction-wordpress-theme/') ),
	)));
}
add_action( 'customize_register', 'constructionarc_themes_customize_register' );


add_action( 'customize_controls_print_scripts', 'constructionarc_themes_customizer_dynamic_script', 30 );
function constructionarc_themes_customizer_dynamic_script(){ ?>
	<script type="text/javascript">
		jQuery( function( $ ) {
			wp.customize.panel( 'constructionarc_frontpage_settings', function( section ) {
				section.expanded.bind( function( isExpanded ) {
					if ( isExpanded ) {
						wp.customize.previewer.previewUrl.set( '<?php echo esc_js( home_url('/') ); ?>' );
					}
				} );
			} );
		} );
	</script>

	<?php
}


function constructionarc_themes_customize_partial_blogname() {
	bloginfo( 'name' );
}

function constructionarc_themes_customize_partial_blogdescription() {
	bloginfo( 'description' );
}


/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 *
 * Enqueue required scripts/styles for customizer panel
 *
 * @since 1.0.0
 *
 */
function constructionarc_themes_customize_preview_js() {

	wp_enqueue_script( 'constructionarc-customizer', get_template_directory_uri() . '/assets/js/customizer.js', array( 'customize-preview' ), '20151215', true );
}
add_action( 'customize_preview_init', 'constructionarc_themes_customize_preview_js' );



 if ( ! function_exists( 'constructionarc_themes_customize_scripts' ) ){

	function constructionarc_themes_customize_scripts() {
		wp_enqueue_script('wp-color-picker-alpha', get_template_directory_uri() . '/inc/customizer/js/wp-color-picker-alpha.js', array('jquery', 'wp-color-picker'), true);
		$color_picker_strings = array(
			'clear' => __('Clear', 'construction-arc'),
			'clearAriaLabel' => __('Clear color', 'construction-arc'),
			'defaultString' => __('Default', 'construction-arc'),
			'defaultAriaLabel' => __('Select default color', 'construction-arc'),
			'pick' => __('Select Color', 'construction-arc'),
			'defaultLabel' => __('Color value', 'construction-arc'),
		);
		wp_localize_script('wp-color-picker-alpha', 'wpColorPickerL10n', $color_picker_strings);
		wp_enqueue_script('constructionarc-customizer', get_template_directory_uri() . '/inc/customizer/js/customizer-admin.js', array('jquery', 'customize-controls'), true);
		wp_enqueue_script('constructionarc-customizer-script', get_template_directory_uri() . '/inc/customizer/js/customizer-controls.js', array('jquery', 'wp-color-picker', 'jquery-ui-datepicker'), true);
		wp_enqueue_style('constructionarc-customizer-style', get_template_directory_uri() . '/inc/customizer/css/customizer-controls.css', array('wp-color-picker'));
	}
}
add_action('customize_controls_enqueue_scripts', 'constructionarc_themes_customize_scripts');

require get_template_directory() . '/inc/customizer/customizer-control-class.php';
require get_template_directory() . '/inc/customizer/customizer-sanitization.php';


function constructionarc_themes_sections_reorder() {
    if (isset($_POST['sections'])) {
        set_theme_mod('constructionarc_frontpage_sections', $_POST['sections']);
    }
    wp_die();
}
add_action('wp_ajax_constructionarc_sections_reorder', 'constructionarc_themes_sections_reorder');

function constructionarc_themes_get_section_position($key) {
    $sections = constructionarc_themes_homepage_section();
    $position = array_search($key, $sections);
    $return = ( $position + 1 ) * 15;
    return $return;
}

if( !function_exists('constructionarc_themes_homepage_section') ){
	function constructionarc_themes_homepage_section(){
		$defaults = apply_filters('constructionarc_homepage_sections',
			array(
				'constructionarc_aboutus_section',
				'constructionarc_promoservice_section',
				'constructionarc_calltoaction_section',
				'constructionarc_service_section',
				'constructionarc_counter_section',
				'constructionarc_video_calltoaction_section',
				'constructionarc_recentwork_section',
				'constructionarc_testimonial_section',
				'constructionarc_team_section',
				'constructionarc_client_section',
				'constructionarc_blog_section',
				'constructionarc_customa_section',
				'constructionarc_contact_section',
			)
		);
		$sections = get_theme_mod('constructionarc_frontpage_sections', $defaults);
        return $sections;
	}
}