<?php

function car_mechanic_remove_customize_register() {
    global $wp_customize;
    $wp_customize->remove_setting( 'automobile_hub_menu_font_size' );
    $wp_customize->remove_control( 'automobile_hub_menu_font_size' );

    $wp_customize->remove_setting( 'automobile_hub_cart_option' );
    $wp_customize->remove_control( 'automobile_hub_cart_option' );

    $wp_customize->remove_setting( 'automobile_hub_tp_color_option_link' );
    $wp_customize->remove_control( 'automobile_hub_tp_color_option_link' );

    $wp_customize->remove_setting( 'automobile_hub_cart_icon' );
    $wp_customize->remove_control( 'automobile_hub_cart_icon' );

    $wp_customize->remove_setting( 'automobile_hub_slider_icon' );
    $wp_customize->remove_control( 'automobile_hub_slider_icon' );
    
    $wp_customize->remove_setting( 'automobile_hub_about_icon' );
    $wp_customize->remove_control( 'automobile_hub_about_icon' );

    $wp_customize->remove_setting( 'automobile_hub_slider_text' );
    $wp_customize->remove_control( 'automobile_hub_slider_text' );

    $wp_customize->remove_setting( 'automobile_hub_show_slider_content' );
    $wp_customize->remove_control( 'automobile_hub_show_slider_content' );

}
add_action( 'customize_register', 'car_mechanic_remove_customize_register', 11 );

if ( ! defined( 'CAR_MECHANIC_TEXT' ) ) {
    define( 'CAR_MECHANIC_TEXT', __( 'Car Mechanic Pro','car-mechanic' ));
}
if ( ! defined( 'CAR_MECHANIC_BUY_TEXT' ) ) {
    define( 'CAR_MECHANIC_BUY_TEXT', __( 'Upgrade Pro','car-mechanic' ));
}


add_action( 'customize_register', function( $manager ) {

// Load custom sections.
load_template( trailingslashit( get_template_directory() ) . '/inc/section-pro.php' );

    $manager->register_section_type( Automobile_Hub_Button::class );

    $manager->add_section(
        new Automobile_Hub_Button( $manager, 'automobile_hub_pro', [
            'title'       => esc_html( CAR_MECHANIC_TEXT,'car-mechanic' ),
            'priority'    => 0,
            'button_text' => __( 'GET PREMIUM', 'car-mechanic' ),
            'button_url'  => esc_url( AUTOMOBILE_HUB_PRO_THEME_URL )
        ] )
    );

} );

function car_mechanic_customize_register( $wp_customize ) {

	// Pro Version
    class car_mechanic_Customize_Pro_Version extends WP_Customize_Control {
        public $type = 'pro_options';

        public function render_content() {
            echo '<span>Unlock Premium <strong>'. esc_html( $this->label ) .'</strong>? </span>';
            echo '<a href="'. esc_url($this->description) .'" target="_blank">';
                echo '<span class="dashicons dashicons-info"></span>';
                echo '<strong> '. esc_html( CAR_MECHANIC_BUY_TEXT,'car-mechanic' ) .'<strong></a>';
            echo '</a>';
        }
    }

    // Custom Controls
    function car_mechanic_sanitize_custom_control( $input ) {
        return $input;
    }

// Register the custom control type.
	$wp_customize->register_control_type( 'Car_Mechanic_Toggle_Control' );

	//Tp Color Option
	$wp_customize->add_setting( 'car_mechanic_tp_secondary_color_option', array(
	    'default' => '',
	    'priority' => 2,
	    'sanitize_callback' => 'sanitize_hex_color'
  	));
  	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'car_mechanic_tp_secondary_color_option', array(
			'label'     => __('Theme Secondary  Color', 'car-mechanic'),
	    'description' => __('It will change the complete theme secondary color in one click.', 'car-mechanic'),
	    'section' => 'automobile_hub_color_option',
	    'settings' => 'car_mechanic_tp_secondary_color_option',
  	)));
 
 	//Topbar Section
	$wp_customize->add_setting('car_mechanic_location_text',array(
		'default'	=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('car_mechanic_location_text',array(
		'label'	=> __('Add Location Text','car-mechanic'),
		'section'	=> 'automobile_hub_topbar',
		'type'		=> 'text'
	));
	$wp_customize->add_setting('car_mechanic_location',array(
		'default'	=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('car_mechanic_location',array(
		'label'	=> __('Add Location','car-mechanic'),
		'section'	=> 'automobile_hub_topbar',
		'type'		=> 'text'
	));

	//Featured Car Section
	$wp_customize->add_section( 'car_mechanic_featured_car_section' , array(
    	'title'      => __( 'Best Car Deals Settings', 'car-mechanic' ),
		'panel' => 'automobile_hub_panel_id',
		'priority' => 4,
	) );
    $wp_customize->add_setting( 'car_mechanic_featured_car_show_hide', array(
		'default'           => true,
		'transport'         => 'refresh',
		'sanitize_callback' => 'automobile_hub_sanitize_checkbox',
	) );
	$wp_customize->add_control( new Car_Mechanic_Toggle_Control ( $wp_customize, 'car_mechanic_featured_car_show_hide', array(
		'label'       => esc_html__( 'Show / Hide section', 'car-mechanic' ),
		'section'     => 'car_mechanic_featured_car_section',
		'type'        => 'toggle',
		'settings'    => 'car_mechanic_featured_car_show_hide',
	) ) );

	$wp_customize->add_setting( 'car_mechanic_deal_sec_animation', array(
		'default'           => true,
		'transport'         => 'refresh',
		'sanitize_callback' => 'automobile_hub_sanitize_checkbox',
	) );
	$wp_customize->add_control( new Automobile_Hub_Toggle_Control( $wp_customize, 'car_mechanic_deal_sec_animation', array(
		'label'       => esc_html__( 'Show / Hide Best Car Deals Section Animation', 'car-mechanic' ),
		'priority' => 1,
		'section'     => 'car_mechanic_featured_car_section',
		'type'        => 'toggle',
		'settings'    => 'car_mechanic_deal_sec_animation',
	) ) );

	$wp_customize->add_setting('car_mechanic_featured_car_section_tittle',array(
		'default'	=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('car_mechanic_featured_car_section_tittle',array(
		'label'	=> __('Section Title','car-mechanic'),
		'section'	=> 'car_mechanic_featured_car_section',
		'type'		=> 'text'
	));
	$wp_customize->add_setting('car_mechanic_featured_car_section_text',array(
		'default'	=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));
	$wp_customize->add_control('car_mechanic_featured_car_section_text',array(
		'label'	=> __('Section Text','car-mechanic'),
		'section'	=> 'car_mechanic_featured_car_section',
		'type'		=> 'text'
	));

	$categories = get_categories();
	$cats = array();
	$i = 0;
	$offer_cat[]= 'select';
	foreach($categories as $category){
		if($i==0){
			$default = $category->slug;
			$i++;
		}
		$offer_cat[$category->slug] = $category->name;
	}
	$wp_customize->add_setting('car_mechanic_featured_car_section_category',array(
		'default'	=> 'select',
		'sanitize_callback' => 'automobile_hub_sanitize_choices',
	));
	$wp_customize->add_control('car_mechanic_featured_car_section_category',array(
		'type'    => 'select',
		'choices' => $offer_cat,
		'label' => __('Select Category','car-mechanic'),
		'section' => 'car_mechanic_featured_car_section',
	));

	$wp_customize->add_setting('car_mechanic_num_posts', array(
	    'default' => 3,
	    'sanitize_callback' => 'absint',
	));
	$wp_customize->add_control('car_mechanic_num_posts', array(
	    'label' => __('Number of Featured Cars', 'car-mechanic'),
	    'section' => 'car_mechanic_featured_car_section',
	    'type' => 'number',
	    'input_attrs' => array(
	        'min' => 1,
	        'step' => 1,
	    ),
	));

	// Get the number of posts to display from the customizer setting
	$car_mechanic_num_posts = get_theme_mod('car_mechanic_num_posts', 3); // Default to 3 if no setting is found

	// Loop through the number of posts and create settings for each
	for ($i = 1; $i <= $car_mechanic_num_posts; $i++) {

	    $wp_customize->add_setting('car_mechanic_body_type' . $i, array(
	        'default' => '',
	        'sanitize_callback' => 'sanitize_text_field',
	    ));
	    $wp_customize->add_control('car_mechanic_body_type' . $i, array(
	        'label' => sprintf(__('Body Type %d', 'car-mechanic'), $i),
	        'section' => 'car_mechanic_featured_car_section',
	        'type' => 'text',
	    ));

	    $wp_customize->add_setting('car_mechanic_model_year' . $i, array(
	        'default' => '',
	        'sanitize_callback' => 'sanitize_text_field',
	    ));
	    $wp_customize->add_control('car_mechanic_model_year' . $i, array(
	        'label' => sprintf(__('Model Year %d', 'car-mechanic'), $i),
	        'section' => 'car_mechanic_featured_car_section',
	        'type' => 'text',
	    ));

	    $wp_customize->add_setting('car_mechanic_mileage' . $i, array(
	        'default' => '',
	        'sanitize_callback' => 'sanitize_text_field',
	    ));
	    $wp_customize->add_control('car_mechanic_mileage' . $i, array(
	        'label' => sprintf(__('Mileage %d', 'car-mechanic'), $i),
	        'section' => 'car_mechanic_featured_car_section',
	        'type' => 'text',
	    ));

	    $wp_customize->add_setting('car_mechanic_compare_price' . $i, array(
	        'default' => '',
	        'sanitize_callback' => 'sanitize_text_field',
	    ));
	    $wp_customize->add_control('car_mechanic_compare_price' . $i, array(
	        'label' => sprintf(__('Compare Price %d', 'car-mechanic'), $i),
	        'section' => 'car_mechanic_featured_car_section',
	        'type' => 'text',
	    ));
	}

	// Pro Version
    $wp_customize->add_setting( 'car_mechanic_services_pro_version_logo', array(
        'sanitize_callback' => 'car_mechanic_sanitize_custom_control'
    ));
    $wp_customize->add_control( new car_mechanic_Customize_Pro_Version ( $wp_customize,'car_mechanic_services_pro_version_logo', array(
        'section'     => 'car_mechanic_featured_car_section',
        'type'        => 'pro_options',
        'label'       => esc_html__( 'Features ', 'car-mechanic' ),
        'description' => esc_url( AUTOMOBILE_HUB_PRO_THEME_URL ),
        'priority'    => 10,
    )));


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