<?php
/**
 * Brooklyn Lite Theme Customizer
 *
 * @package Brooklyn Lite
 */

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function brooklyn_lite_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';

	//=============================================================
	// Remove header image and widgets option from theme customizer
	//=============================================================
	$wp_customize->remove_control( 'header_image' );

	//=============================================================
	// Remove Colors, Background image, and Static front page
	// option from theme customizer
	//=============================================================
	$wp_customize->remove_section( 'colors' );
	$wp_customize->remove_section( 'background_image' );
	$wp_customize->remove_section( 'static_front_page' );

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

	// About Theme section
	$wp_customize->add_section( 'theme_detail', array(
		'title'    => __( 'About Theme', 'brooklyn-lite' ),
		'priority' => 9,
	) );

	$wp_customize->add_setting( 'upgrade_text', array(
		'default'           => '',
		'sanitize_callback' => '__return_false',
	) );

	// =========================================================================
	// PANEL: Brooklyn Theme Options
	// =========================================================================
	$wp_customize->add_panel( 'brooklyn_lite_panel', array(
		'priority'   => 2,
		'capability' => 'edit_theme_options',
		'title'      => __( 'Brooklyn Theme Options', 'brooklyn-lite' ),
	) );

	// Google Fonts list (shared by typography selects)
	$google_fonts = array(
		'Muli'              => 'Muli',
		'Montserrat'        => 'Montserrat',
		'Open Sans'         => 'Open Sans',
		'Lato'              => 'Lato',
		'Roboto'            => 'Roboto',
		'Poppins'           => 'Poppins',
		'Raleway'           => 'Raleway',
		'Playfair Display'  => 'Playfair Display',
		'Merriweather'      => 'Merriweather',
		'Oswald'            => 'Oswald',
		'Source Sans Pro'   => 'Source Sans Pro',
		'Nunito'            => 'Nunito',
		'Rubik'             => 'Rubik',
		'Work Sans'         => 'Work Sans',
		'DM Sans'           => 'DM Sans',
		'Inter'             => 'Inter',
		'Outfit'            => 'Outfit',
		'Manrope'           => 'Manrope',
		'Plus Jakarta Sans' => 'Plus Jakarta Sans',
		'Space Grotesk'     => 'Space Grotesk',
	);

	// =========================================================================
	// SECTION 1: Header Layout
	// =========================================================================
	$wp_customize->add_section( 'brooklyn_lite_section_menu', array(
		'title'    => __( 'Header Layout', 'brooklyn-lite' ),
		'panel'    => 'brooklyn_lite_panel',
		'priority' => 1,
	) );

	// Menu Type
	$wp_customize->add_setting( 'menu_type', array(
		'default'           => 'header-default',
		'type'              => 'theme_mod',
		'transport'         => 'refresh',
		'sanitize_callback' => 'brooklyn_lite_sanitize_select',
	) );
	$wp_customize->add_control( 'menu_type', array(
		'label'   => __( 'Header Type', 'brooklyn-lite' ),
		'section' => 'brooklyn_lite_section_menu',
		'type'    => 'radio',
		'choices' => array(
			'header-default'     => __( 'Default Header', 'brooklyn-lite' ),
			'header-transparent' => __( 'Transparent Header', 'brooklyn-lite' ),
		),
	) );

	// Menu Container
	$wp_customize->add_setting( 'menu_container', array(
		'default'           => 'menu-container',
		'type'              => 'theme_mod',
		'transport'         => 'refresh',
		'sanitize_callback' => 'brooklyn_lite_sanitize_select',
	) );
	$wp_customize->add_control( 'menu_container', array(
		'label'   => __( 'Menu Container', 'brooklyn-lite' ),
		'section' => 'brooklyn_lite_section_menu',
		'type'    => 'radio',
		'choices' => array(
			'menu-container'  => __( 'Container Menu', 'brooklyn-lite' ),
			'menu-full-width' => __( 'Full/Stretched Menu', 'brooklyn-lite' ),
		),
	) );

	// =========================================================================
	// SECTION 2: Blog & Archive Settings
	// =========================================================================
	$wp_customize->add_section( 'brooklyn_lite_section_blog_index', array(
		'title'    => __( 'Blog & Archive Settings', 'brooklyn-lite' ),
		'panel'    => 'brooklyn_lite_panel',
		'priority' => 3,
	) );

	// Blog Layout
	$wp_customize->add_setting( 'blog_layout', array(
		'default'           => 'layout-default',
		'type'              => 'theme_mod',
		'transport'         => 'refresh',
		'sanitize_callback' => 'brooklyn_lite_sanitize_select',
	) );
	$wp_customize->add_control( 'blog_layout', array(
		'label'   => __( 'Blog Layout', 'brooklyn-lite' ),
		'section' => 'brooklyn_lite_section_blog_index',
		'type'    => 'radio',
		'choices' => array(
			'layout-default'     => __( 'Default', 'brooklyn-lite' ),
			'layout-grid'        => __( 'Grid', 'brooklyn-lite' ),
			'layout-classic'     => __( 'Classic', 'brooklyn-lite' ),
			'layout-two-columns' => __( 'Two Columns', 'brooklyn-lite' ),
		),
	) );

	// Blog Excerpt Length
	$wp_customize->add_setting( 'brooklyn_lite_blog_excerpt', array(
		'default'           => 20,
		'type'              => 'theme_mod',
		'transport'         => 'refresh',
		'sanitize_callback' => 'absint',
	) );
	$wp_customize->add_control( 'brooklyn_lite_blog_excerpt', array(
		'label'       => __( 'Excerpt Length', 'brooklyn-lite' ),
		'section'     => 'brooklyn_lite_section_blog_index',
		'type'        => 'number',
		'input_attrs' => array(
			'min'  => 5,
			'max'  => 80,
			'step' => 1,
		),
	) );

	// Read More Text
	$wp_customize->add_setting( 'brooklyn_lite_read_more_text', array(
		'default'           => 'Read more',
		'type'              => 'theme_mod',
		'transport'         => 'refresh',
		'sanitize_callback' => 'sanitize_text_field',
	) );
	$wp_customize->add_control( 'brooklyn_lite_read_more_text', array(
		'label'   => __( 'Read More Text', 'brooklyn-lite' ),
		'section' => 'brooklyn_lite_section_blog_index',
		'type'    => 'text',
	) );

	// Hide All Meta
	$wp_customize->add_setting( 'brooklyn_lite_blog_meta', array(
		'default'           => 0,
		'type'              => 'theme_mod',
		'transport'         => 'refresh',
		'sanitize_callback' => 'brooklyn_lite_sanitize_checkbox',
	) );
	$wp_customize->add_control( 'brooklyn_lite_blog_meta', array(
		'label'   => __( 'Hide All Meta?', 'brooklyn-lite' ),
		'section' => 'brooklyn_lite_section_blog_index',
		'type'    => 'checkbox',
	) );

	// Blog index visibility toggles
	$blog_checkboxes = array(
		'index_hide_thumb'     => __( 'Hide Thumbnail', 'brooklyn-lite' ),
		'index_hide_date'      => __( 'Hide Date', 'brooklyn-lite' ),
		'index_hide_cats'      => __( 'Hide Categories', 'brooklyn-lite' ),
		'index_hide_tags'      => __( 'Hide Tags', 'brooklyn-lite' ),
		'index_hide_author'    => __( 'Hide Author', 'brooklyn-lite' ),
		'index_hide_comments'  => __( 'Hide Comments', 'brooklyn-lite' ),
		'index_hide_read_more' => __( 'Hide Read More', 'brooklyn-lite' ),
	);

	foreach ( $blog_checkboxes as $id => $label ) {
		$wp_customize->add_setting( $id, array(
			'default'           => 0,
			'type'              => 'theme_mod',
			'transport'         => 'refresh',
			'sanitize_callback' => 'brooklyn_lite_sanitize_checkbox',
		) );
		$wp_customize->add_control( $id, array(
			'label'   => $label,
			'section' => 'brooklyn_lite_section_blog_index',
			'type'    => 'checkbox',
		) );
	}

	// Breadcrumbs
	$wp_customize->add_setting( 'brooklyn_lite_breadcrumbs', array(
		'default'           => 0,
		'type'              => 'theme_mod',
		'transport'         => 'refresh',
		'sanitize_callback' => 'brooklyn_lite_sanitize_checkbox',
	) );
	$wp_customize->add_control( 'brooklyn_lite_breadcrumbs', array(
		'label'   => __( 'Hide Breadcrumbs', 'brooklyn-lite' ),
		'section' => 'brooklyn_lite_section_blog_index',
		'type'    => 'checkbox',
	) );

	// =========================================================================
	// SECTION 3: Single Post Settings
	// =========================================================================
	$wp_customize->add_section( 'brooklyn_lite_section_blog_single', array(
		'title'    => __( 'Single Post Settings', 'brooklyn-lite' ),
		'panel'    => 'brooklyn_lite_panel',
		'priority' => 5,
	) );

	// Single Post Layout
	$wp_customize->add_setting( 'single_post_layout', array(
		'default'           => 'layout-default',
		'type'              => 'theme_mod',
		'transport'         => 'refresh',
		'sanitize_callback' => 'brooklyn_lite_sanitize_select',
	) );
	$wp_customize->add_control( 'single_post_layout', array(
		'label'   => __( 'Post Layout', 'brooklyn-lite' ),
		'section' => 'brooklyn_lite_section_blog_single',
		'type'    => 'radio',
		'choices' => array(
			'layout-default' => __( 'Default', 'brooklyn-lite' ),
			'layout-full'    => __( 'No sidebar', 'brooklyn-lite' ),
		),
	) );

	// Single Post Content Layout
	$wp_customize->add_setting( 'single_post_content_layout', array(
		'default'           => 'layout-default',
		'type'              => 'theme_mod',
		'transport'         => 'refresh',
		'sanitize_callback' => 'brooklyn_lite_sanitize_select',
	) );
	$wp_customize->add_control( 'single_post_content_layout', array(
		'label'   => __( 'Content Layout', 'brooklyn-lite' ),
		'section' => 'brooklyn_lite_section_blog_single',
		'type'    => 'radio',
		'choices' => array(
			'layout-default' => __( 'Default', 'brooklyn-lite' ),
			'layout-2'       => __( 'Layout 2', 'brooklyn-lite' ),
			'layout-3'       => __( 'Layout 3', 'brooklyn-lite' ),
		),
	) );

	// =========================================================================
	// SECTION 4: Footer Settings
	// =========================================================================
	$wp_customize->add_section( 'brooklyn_lite_section_footer', array(
		'title'    => __( 'Footer Settings', 'brooklyn-lite' ),
		'panel'    => 'brooklyn_lite_panel',
		'priority' => 5,
	) );

	// Copyright Text
	$wp_customize->add_setting(
		'copyright_text',
		array(
			'sanitize_callback' => 'brooklyn_lite_sanitize_html',
			'default'           => '&copy; 2020 All Rights Reserved',
			'type'              => 'theme_mod',
			'transport'         => 'refresh',
		)
	);
	$wp_customize->add_control( 'copyright_text', array(
		'label'   => __( 'Copyright Text', 'brooklyn-lite' ),
		'section' => 'brooklyn_lite_section_footer',
		'type'    => 'textarea',
	) );

	// Social URLs
	$social_urls = array(
		'brooklyn_lite_facebook'  => __( 'Facebook URL', 'brooklyn-lite' ),
		'brooklyn_lite_twitter'   => __( 'Twitter URL', 'brooklyn-lite' ),
		'brooklyn_lite_skype'     => __( 'Skype URL', 'brooklyn-lite' ),
		'brooklyn_lite_instagram' => __( 'Instagram URL', 'brooklyn-lite' ),
		'brooklyn_lite_dribble'   => __( 'Dribbble URL', 'brooklyn-lite' ),
		'brooklyn_lite_vimeo'     => __( 'Vimeo URL', 'brooklyn-lite' ),
	);

	foreach ( $social_urls as $id => $label ) {
		$wp_customize->add_setting( $id, array(
			'default'           => '',
			'type'              => 'theme_mod',
			'transport'         => 'refresh',
			'sanitize_callback' => 'esc_url_raw',
		) );
		$wp_customize->add_control( $id, array(
			'label'   => $label,
			'section' => 'brooklyn_lite_section_footer',
			'type'    => 'url',
		) );
	}

	// =========================================================================
	// SECTION 5: Typography Settings
	// =========================================================================
	$wp_customize->add_section( 'brooklyn_lite_section_fonts', array(
		'title'    => __( 'Typography Settings', 'brooklyn-lite' ),
		'panel'    => 'brooklyn_lite_panel',
		'priority' => 5,
	) );

	// Headings Font Family
	$wp_customize->add_setting( 'headings_font_family', array(
		'default'           => 'Muli',
		'type'              => 'theme_mod',
		'transport'         => 'refresh',
		'sanitize_callback' => 'brooklyn_lite_sanitize_select',
	) );
	$wp_customize->add_control( 'headings_font_family', array(
		'label'   => __( 'Headings Font Family', 'brooklyn-lite' ),
		'section' => 'brooklyn_lite_section_fonts',
		'type'    => 'select',
		'choices' => $google_fonts,
	) );

	// Body Font Family
	$wp_customize->add_setting( 'body_font_family', array(
		'default'           => 'Montserrat',
		'type'              => 'theme_mod',
		'transport'         => 'refresh',
		'sanitize_callback' => 'brooklyn_lite_sanitize_select',
	) );
	$wp_customize->add_control( 'body_font_family', array(
		'label'   => __( 'Body Font Family', 'brooklyn-lite' ),
		'section' => 'brooklyn_lite_section_fonts',
		'type'    => 'select',
		'choices' => $google_fonts,
	) );

	// Font size range controls
	$font_sizes = array(
		'font_size_body' => array(
			'label'   => __( 'Body Font Size (px)', 'brooklyn-lite' ),
			'default' => 16,
			'min'     => 10,
			'max'     => 30,
		),
		'font_size_site_title' => array(
			'label'   => __( 'Site Title Font Size (px)', 'brooklyn-lite' ),
			'default' => 36,
			'min'     => 16,
			'max'     => 50,
		),
		'font_size_site_desc' => array(
			'label'   => __( 'Site Description Font Size (px)', 'brooklyn-lite' ),
			'default' => 16,
			'min'     => 12,
			'max'     => 30,
		),
		'font_size_menu_top_items' => array(
			'label'   => __( 'Top Menu Items Font Size (px)', 'brooklyn-lite' ),
			'default' => 16,
			'min'     => 10,
			'max'     => 30,
		),
		'font_size_menu_sub_items' => array(
			'label'   => __( 'Submenu Items Font Size (px)', 'brooklyn-lite' ),
			'default' => 13,
			'min'     => 10,
			'max'     => 30,
		),
		'font_size_index_title' => array(
			'label'   => __( 'Index Post Title Font Size (px)', 'brooklyn-lite' ),
			'default' => '',
			'min'     => 10,
			'max'     => 50,
		),
		'font_size_single_title' => array(
			'label'   => __( 'Single Post Title Font Size (px)', 'brooklyn-lite' ),
			'default' => 36,
			'min'     => 10,
			'max'     => 50,
		),
		'font_size_footer_social_icons' => array(
			'label'   => __( 'Footer Social Icons Font Size (px)', 'brooklyn-lite' ),
			'default' => 16,
			'min'     => 10,
			'max'     => 30,
		),
		'font_size_footer_credits' => array(
			'label'   => __( 'Footer Credits Font Size (px)', 'brooklyn-lite' ),
			'default' => 13,
			'min'     => 10,
			'max'     => 30,
		),
	);

	foreach ( $font_sizes as $id => $args ) {
		$wp_customize->add_setting( $id, array(
			'default'           => $args['default'],
			'type'              => 'theme_mod',
			'transport'         => 'refresh',
			'sanitize_callback' => 'absint',
		) );
		$wp_customize->add_control( $id, array(
			'label'       => $args['label'],
			'section'     => 'brooklyn_lite_section_fonts',
			'type'        => 'range',
			'input_attrs' => array(
				'min'  => $args['min'],
				'max'  => $args['max'],
				'step' => 1,
			),
		) );
	}

	// =========================================================================
	// SECTION 6: Customize Colors
	// =========================================================================
	$wp_customize->add_section( 'brooklyn_lite_section_colors', array(
		'title'    => __( 'Customize Colors', 'brooklyn-lite' ),
		'panel'    => 'brooklyn_lite_panel',
		'priority' => 17,
	) );

	$color_controls = array(
		// General
		'color_primary' => array(
			'label'   => __( 'Primary Color', 'brooklyn-lite' ),
			'default' => '#ffd200',
		),
		// Header / Site Identity
		'color_mt1_site_title' => array(
			'label'   => __( 'Site Title Color', 'brooklyn-lite' ),
			'default' => '#ffffff',
		),
		'color_mt1_site_desc' => array(
			'label'   => __( 'Site Description Color', 'brooklyn-lite' ),
			'default' => '#ffffff',
		),
		// Menu Colors
		'color_mt1_top_menu_items' => array(
			'label'   => __( 'Top Menu Items Color', 'brooklyn-lite' ),
			'default' => '#262626',
		),
		'color_mt1_bg_color' => array(
			'label'   => __( 'Menu Background Color', 'brooklyn-lite' ),
			'default' => '',
		),
		'color_submenu_items' => array(
			'label'   => __( 'Submenu Items Color', 'brooklyn-lite' ),
			'default' => '#262626',
		),
		'color_submenu_bg' => array(
			'label'   => __( 'Submenu Background Color', 'brooklyn-lite' ),
			'default' => '#fff',
		),
		'mobile_toggle_color' => array(
			'label'   => __( 'Mobile Toggle Color', 'brooklyn-lite' ),
			'default' => '',
		),
		// Post Colors
		'color_index_post_title' => array(
			'label'   => __( 'Index Post Title Color', 'brooklyn-lite' ),
			'default' => '#262626',
		),
		'color_single_post_title' => array(
			'label'   => __( 'Single Post Title Color', 'brooklyn-lite' ),
			'default' => '#262626',
		),
		// Meta Colors
		'color_meta_cat_bg' => array(
			'label'   => __( 'Meta Category Background', 'brooklyn-lite' ),
			'default' => '#fff',
		),
		'color_meta_text' => array(
			'label'   => __( 'Meta Text Color', 'brooklyn-lite' ),
			'default' => '#999',
		),
		'color_meta_links' => array(
			'label'   => __( 'Meta Links Color', 'brooklyn-lite' ),
			'default' => '#999',
		),
		// Content
		'color_post_text' => array(
			'label'   => __( 'Post Text Color', 'brooklyn-lite' ),
			'default' => '#262626',
		),
	);

	foreach ( $color_controls as $id => $args ) {
		$wp_customize->add_setting( $id, array(
			'default'           => $args['default'],
			'type'              => 'theme_mod',
			'transport'         => 'refresh',
			'sanitize_callback' => 'sanitize_hex_color',
		) );
		$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, $id, array(
			'label'   => $args['label'],
			'section' => 'brooklyn_lite_section_colors',
		) ) );
	}
}
add_action( 'customize_register', 'brooklyn_lite_customize_register' );

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

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

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

/**
 * Enqueue shadcn-inspired styles and JS for Customizer controls panel.
 */
function brooklyn_lite_customize_controls_enqueue() {
	wp_enqueue_style(
		'brooklyn-lite-customizer-controls',
		get_template_directory_uri() . '/assets/css/customizer-controls.css',
		array(),
		BROOKLYN_LITE_VER
	);
	wp_enqueue_script(
		'brooklyn-lite-customizer-controls',
		get_template_directory_uri() . '/assets/js/customizer-controls.js',
		array( 'customize-controls' ),
		BROOKLYN_LITE_VER,
		true
	);
}
add_action( 'customize_controls_enqueue_scripts', 'brooklyn_lite_customize_controls_enqueue' );

// =========================================================================
// Sanitize Callbacks
// =========================================================================

// brooklyn_lite_sanitize_checkbox() is defined in template-tags.php.

if ( ! function_exists( 'brooklyn_lite_sanitize_html' ) ) :
/**
 * Sanitize HTML content (allows safe tags).
 *
 * @param string $input The value to sanitize.
 * @return string Sanitized HTML.
 */
function brooklyn_lite_sanitize_html( $input ) {
	return wp_kses_post( $input );
}
endif;

if ( ! function_exists( 'brooklyn_lite_sanitize_select' ) ) :
/**
 * Sanitize select/radio values against their valid choices.
 *
 * @param string               $input   The value to sanitize.
 * @param WP_Customize_Setting $setting The setting instance.
 * @return string Sanitized value or default.
 */
function brooklyn_lite_sanitize_select( $input, $setting ) {
	$input   = sanitize_text_field( $input );
	$choices = $setting->manager->get_control( $setting->id )->choices;

	return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
}
endif;
