<?php
function booklium_customizer_color_settings( WP_Customize_Manager $wp_customize ) {

	$wp_customize->add_setting( 'booklium_color_scheme', array(
		'default'           => 'default',
		'sanitize_callback' => 'booklium_sanitize_select',
		//'transport'         => 'refresh',

	) );

	$wp_customize->add_control( 'booklium_color_scheme', array(
		'type'     => 'select',
		'section'  => 'colors',
		'priority' => 0,
		'label'    => esc_html__( 'Color scheme', 'booklium' ),
		'choices'  => array(
			'default' => esc_html__( 'Default', 'booklium' ),
			'style-1' => esc_html__( 'Style 1', 'booklium' ),
			'style-2' => esc_html__( 'Style 2', 'booklium' ),
		),
	) );

	// Color settings
	$wp_customize->add_setting( 'booklium_accent_color', array(
		'default'           => booklium_get_default_value_for_selected_scheme_color( 'booklium_accent_color' ),
		'type'              => 'theme_mod',
		'sanitize_callback' => 'sanitize_hex_color',
		'transport'         => 'postMessage'
	) );

	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'booklium_accent_color', array(
		'label'   => esc_html__( 'Accent Color', 'booklium' ),
		'section' => 'colors',
		'setting' => 'booklium_accent_color'
	) ) );

	$wp_customize->add_setting( 'booklium_header_color', array(
		'default'           => booklium_get_default_value_for_selected_scheme_color( 'booklium_header_color' ),
		'type'              => 'theme_mod',
		'sanitize_callback' => 'sanitize_hex_color',
		'transport'         => 'postMessage'
	) );

	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'booklium_header_color', array(
		'label'   => esc_html__( 'Header Menu Color', 'booklium' ),
		'section' => 'colors',
		'setting' => 'booklium_header_color'
	) ) );

	$wp_customize->add_setting( 'booklium_header_hover_color', array(
		'default'           => booklium_get_default_value_for_selected_scheme_color( 'booklium_header_hover_color' ),
		'type'              => 'theme_mod',
		'sanitize_callback' => 'sanitize_hex_color',
		'transport'         => 'postMessage'
	) );

	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'booklium_header_hover_color', array(
		'label'   => esc_html__( 'Header Menu Hover Color', 'booklium' ),
		'section' => 'colors',
		'setting' => 'booklium_header_hover_color',
	) ) );

	$wp_customize->add_setting( 'booklium_header_bg_color', array(
		'default'           => booklium_get_default_value_for_selected_scheme_color( 'booklium_header_bg_color' ),
		'type'              => 'theme_mod',
		'sanitize_callback' => 'sanitize_hex_color',
		'transport'         => 'postMessage'
	) );

	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'booklium_header_bg_color', array(
		'label'   => esc_html__( 'Header Menu Background Color', 'booklium' ),
		'section' => 'colors',
		'setting' => 'booklium_header_bg_color',
	) ) );

	$wp_customize->add_setting( 'booklium_header_left_menu_dropdown_bg_color', [
		'default'           => booklium_get_default_value_for_selected_scheme_color( 'booklium_header_left_menu_dropdown_bg_color' ),
		'type'              => 'theme_mod',
		'sanitize_callback' => 'sanitize_hex_color',
		'transport' => 'refresh',
	]);

	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'booklium_header_left_menu_dropdown_bg_color', array(
		'label' => __( 'Header Left Menu Dropdown Background Color', 'booklium' ),
		'section' => 'colors',
		'setting' => 'booklium_header_left_menu_dropdown_bg_color',
		'active_callback' => function () {
			return get_theme_mod( 'booklium_site_layout' ) === 'site-menu-left';
		},
	) ) );

	$wp_customize->add_setting( 'booklium_footer_text_color', array(
		'default'           => booklium_get_default_value_for_selected_scheme_color( 'booklium_footer_text_color' ),
		'type'              => 'theme_mod',
		'sanitize_callback' => 'sanitize_hex_color',
		'transport'         => 'postMessage'
	) );

	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'booklium_footer_text_color', array(
		'label'   => esc_html__( 'Footer Text Color', 'booklium' ),
		'section' => 'colors',
		'setting' => 'booklium_footer_text_color'
	) ) );

	$wp_customize->add_setting( 'booklium_footer_widgets_title_color', array(
		'default'           => booklium_get_default_value_for_selected_scheme_color( 'booklium_footer_widgets_title_color' ),
		'type'              => 'theme_mod',
		'sanitize_callback' => 'sanitize_hex_color',
		'transport'         => 'postMessage'
	) );

	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'booklium_footer_widgets_title_color', array(
		'label'   => esc_html__( 'Footer Widgets Title Color', 'booklium' ),
		'section' => 'colors',
		'setting' => 'booklium_footer_widgets_title_color',
	) ) );

	$wp_customize->add_setting( 'booklium_footer_bg_color', array(
		'default'           => booklium_get_default_value_for_selected_scheme_color( 'booklium_footer_bg_color' ),
		'type'              => 'theme_mod',
		'sanitize_callback' => 'sanitize_hex_color',
		'transport'         => 'postMessage'
	) );

	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'booklium_footer_bg_color', array(
		'label'   => esc_html__( 'Footer Background Color', 'booklium' ),
		'section' => 'colors',
		'setting' => 'booklium_footer_bg_color',
	) ) );

	$wp_customize->add_setting( 'booklium_button_bg_color', array(
		'default'           => booklium_get_default_value_for_selected_scheme_color('booklium_button_bg_color'),
		'type'              => 'theme_mod',
		'sanitize_callback' => 'sanitize_hex_color',
		'transport'         => 'postMessage'
	) );

	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'booklium_button_bg_color', array(
		'label'   => esc_html__( 'Button Background Color', 'booklium' ),
		'section' => 'colors',
		'setting' => 'booklium_button_bg_color'
	) ) );

	$wp_customize->add_setting( 'booklium_button_color', array(
		'default'           => booklium_get_default_value_for_selected_scheme_color('booklium_button_color'),
		'type'              => 'theme_mod',
		'sanitize_callback' => 'sanitize_hex_color',
		'transport'         => 'postMessage'
	) );

	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'booklium_button_color', array(
		'label'   => esc_html__( 'Button Color', 'booklium' ),
		'section' => 'colors',
		'setting' => 'booklium_button_color'
	) ) );

	if ( class_exists( 'HotelBookingPlugin' ) ) {
		$wp_customize->add_setting( 'booklium_hb_accent_color', array(
			'default'           => booklium_get_default_value_for_selected_scheme_color('booklium_hb_accent_color'),
			'type'              => 'theme_mod',
			'sanitize_callback' => 'sanitize_hex_color',
			'transport'         => 'postMessage'
		) );

		$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'booklium_hb_accent_color', array(
			'label'   => esc_html__( 'HB Accent Color', 'booklium' ),
			'section' => 'colors',
			'setting' => 'booklium_hb_accent_color'
		) ) );

		$wp_customize->add_setting( 'booklium_calendar_bg_color', array(
			'default'           => booklium_get_default_value_for_selected_scheme_color('booklium_calendar_bg_color'),
			'type'              => 'theme_mod',
			'sanitize_callback' => 'sanitize_hex_color',
			'transport'         => 'postMessage'
		) );

		$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'booklium_calendar_bg_color', array(
			'label'   => esc_html__( 'HB Calendar Available Date Background', 'booklium' ),
			'section' => 'colors',
			'setting' => 'booklium_calendar_bg_color'
		) ) );

		$wp_customize->add_setting( 'booklium_calendar_color', array(
			'default'           => booklium_get_default_value_for_selected_scheme_color('booklium_calendar_color'),
			'type'              => 'theme_mod',
			'sanitize_callback' => 'sanitize_hex_color',
			'transport'         => 'postMessage'
		) );

		$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'booklium_calendar_color', array(
			'label'   => esc_html__( 'HB Calendar Available Date Color', 'booklium' ),
			'section' => 'colors',
			'setting' => 'booklium_calendar_color'
		) ) );
	}
}

add_action( 'customize_register', 'booklium_customizer_color_settings' );

function booklium_get_color_schemes()
{

	return array(
		'default' => array(
			'booklium_accent_color' => array(
				'css_variable' => '--booklium-accent-color',
				'value' => '#13b0bb',
			),
			'booklium_header_color' => array(
				'css_variable' => '--booklium-header-color',
				'value' => '#17181a',
			),
			'booklium_header_hover_color' => array(
				'css_variable' => '--booklium-header-hover-color',
				'value' => '#13b0bb',
			),
			'booklium_header_bg_color' => array(
				'css_variable' => '--booklium-header-bg-color',
				'value' => '#ffffff',
			),
			'booklium_header_left_menu_dropdown_bg_color' => array(
				'css_variable' => '--booklium-header-left-menu-dropdown-bg-color',
				'value' => '#fafafa',
			),
			'booklium_footer_bg_color' => array(
				'css_variable' => '--booklium-footer-bg-color',
				'value' => '#fafafa',
			),
			'booklium_footer_text_color' => array(
				'css_variable' => '--booklium-footer-text-color',
				'value' => '#767b80',
			),
			'booklium_footer_widgets_title_color' => array(
				'css_variable' => '--booklium-footer-widgets-title-color',
				'value' => '#17181a',
			),
			'booklium_button_bg_color' => array(
				'css_variable' => '--booklium-button-bg-color',
				'value' => '#13b0bb',
			),
			'booklium_button_color' => array(
				'css_variable' => '--booklium-button-color',
				'value' => '#ffffff',
			),
			'booklium_hb_accent_color' => array(
				'css_variable' => '--booklium-hb-accent-color',
				'value' => '#ffce00',
			),
			'booklium_calendar_bg_color' => array(
				'css_variable' => '--booklium-calendar-bg-color',
				'value' => '#e7f7f8',
			),
			'booklium_calendar_color' => array(
				'css_variable' => '--booklium-calendar-color',
				'value' => '#13b0bb',
			),
		),
		'style-1' => array(
			'booklium_accent_color' => array(
				'css_variable' => '--booklium-accent-color',
				'value' => '#B09162',
			),
			'booklium_header_color' => array(
				'css_variable' => '--booklium-header-color',
				'value' => '#17181A',
			),
			'booklium_header_hover_color' => array(
				'css_variable' => '--booklium-header-hover-color',
				'value' => '#B09162',
			),
			'booklium_header_bg_color' => array(
				'css_variable' => '--booklium-header-bg-color',
				'value' => '#ffffff',
			),
			'booklium_header_left_menu_dropdown_bg_color' => array(
				'css_variable' => '--booklium-header-left-menu-dropdown-bg-color',
				'value' => '#F2F1EE',
			),
			'booklium_footer_bg_color' => array(
				'css_variable' => '--booklium-footer-bg-color',
				'value' => '#17181A',
			),
			'booklium_footer_text_color' => array(
				'css_variable' => '--booklium-footer-text-color',
				'value' => '#C3C3C3',
			),
			'booklium_footer_widgets_title_color' => array(
				'css_variable' => '--booklium-footer-widgets-title-color',
				'value' => '#ffffff',
			),
			'booklium_button_bg_color' => array(
				'css_variable' => '--booklium-button-bg-color',
				'value' => '#17181A',
			),
			'booklium_button_color' => array(
				'css_variable' => '--booklium-button-color',
				'value' => '#ffffff',
			),
			'booklium_hb_accent_color' => array(
				'css_variable' => '--booklium-hb-accent-color',
				'value' => '#B09162',
			),
			'booklium_calendar_bg_color' => array(
				'css_variable' => '--booklium-calendar-bg-color',
				'value' => '#f6f6f6',
			),
			'booklium_calendar_color' => array(
				'css_variable' => '--booklium-calendar-color',
				'value' => '#141414',
			),
		),

		'style-2' => array(
			'booklium_accent_color' => array(
				'css_variable' => '--booklium-accent-color',
				'value' => '#b09162',
			),
			'booklium_header_color' => array(
				'css_variable' => '--booklium-header-color',
				'value' => '#ffffff',
			),
			'booklium_header_hover_color' => array(
				'css_variable' => '--booklium-header-hover-color',
				'value' => '#B09162',
			),
			'booklium_header_bg_color' => array(
				'css_variable' => '--booklium-header-bg-color',
				'value' => '#203536',
			),
			'booklium_header_left_menu_dropdown_bg_color' => array(
				'css_variable' => '--booklium-header-left-menu-dropdown-bg-color',
				'value' => '#203536',
			),
			'booklium_footer_bg_color' => array(
				'css_variable' => '--booklium-footer-bg-color',
				'value' => '#203536',
			),
			'booklium_footer_text_color' => array(
				'css_variable' => '--booklium-footer-text-color',
				'value' => '#C3C3C3',
			),
			'booklium_footer_widgets_title_color' => array(
				'css_variable' => '--booklium-footer-widgets-title-color',
				'value' => '#ffffff',
			),
			'booklium_button_bg_color' => array(
				'css_variable' => '--booklium-button-bg-color',
				'value' => '#203536',
			),
			'booklium_button_color' => array(
				'css_variable' => '--booklium-button-color',
				'value' => '#ffffff',
			),
			'booklium_hb_accent_color' => array(
				'css_variable' => '--booklium-hb-accent-color',
				'value' => '#203536',
			),
			'booklium_calendar_bg_color' => array(
				'css_variable' => '--booklium-calendar-bg-color',
				'value' => '#f6f6f6',
			),
			'booklium_calendar_color' => array(
				'css_variable' => '--booklium-calendar-color',
				'value' => '#141414',
			),
		),
	);
}

function booklium_get_selected_color_scheme()
{
	$color_schemes = booklium_get_color_schemes();
	$selected_color_scheme_name = get_theme_mod('booklium_color_scheme', 'default');

	return isset($color_schemes[$selected_color_scheme_name]) ? $color_schemes[$selected_color_scheme_name] : $color_schemes['default'];
}

function booklium_get_default_value_for_selected_scheme_color($color)
{
	$selected_color_scheme = booklium_get_selected_color_scheme();

	if (isset($selected_color_scheme[$color]['value'])) {
		return $selected_color_scheme[$color]['value'];
	}

	return '';
}

function booklium_get_actual_value_for_selected_scheme_color($color)
{
	$selected_color_scheme = booklium_get_selected_color_scheme();
	$customizer_color = get_theme_mod($color, false);

	if ( $customizer_color ) {
		return $customizer_color;
	}

	if ( isset($selected_color_scheme[$color]['value']) ) {
		return $selected_color_scheme[$color]['value'];
	}

	return '';
}

function booklium_add_generated_inline_style()
{
	wp_add_inline_style('booklium-style', booklium_generate_inline_style());
}

add_action('wp_enqueue_scripts', 'booklium_add_generated_inline_style');

function booklium_generate_inline_style()
{
	$selected_color_scheme = booklium_get_selected_color_scheme();

	$style = '';

	foreach ((array)$selected_color_scheme as $color_name => $color) {
		$customizer_color = get_theme_mod($color_name, false);

		if ($customizer_color && $customizer_color != booklium_get_default_value_for_selected_scheme_color($color_name)) {
			$style .= $color['css_variable'] . ': ' . $customizer_color . ' !important; ';
		}
	}

	if ( $style ) {
		return 'body { ' . $style . ' } ';
	} else {
		return '';
	}
}

function booklium_generate_inline_style_for_editor()
{
	$selected_color_scheme = booklium_get_selected_color_scheme();

	$style = '';

	foreach ($selected_color_scheme as $color_name => $color) {
		$customizer_color = get_theme_mod($color_name, booklium_get_default_value_for_selected_scheme_color($color_name));

		if ($customizer_color) {
			$style .= $color['css_variable'] . ': ' . $customizer_color . ' !important; ';
		}
	}

	if ( $style ) {
		return '.editor-styles-wrapper { ' . $style . ' } ';
	} else {
		return '';
	}
}

function booklium_editor_customizer_styles() {

	$css = booklium_generate_inline_style_for_editor();

	if ( $css ) {
		//register fake stylesheet which allow add inline style
		wp_register_style( 'booklium-editor-customizer', false );
		wp_enqueue_style( 'booklium-editor-customizer' );
		wp_add_inline_style( 'booklium-editor-customizer', $css );
	}
}

add_action( 'enqueue_block_editor_assets', 'booklium_editor_customizer_styles' );

function booklium_add_theme_support_editor_color_pallette() {

	$color_scheme = get_theme_mod( 'booklium_color_scheme', 'default' );

	if ( 'style-1' === $color_scheme ) {
		add_theme_support( 'editor-color-palette', array(
			array(
				'name'  => esc_html__( 'Color 1', 'booklium' ),
				'slug'  => 'color-1',
				'color' => '#B09162',
			),
			array(
				'name'  => esc_html__( 'Color 2', 'booklium' ),
				'slug'  => 'color-2',
				'color' => '#17181A',
			),
			array(
				'name'  => esc_html__( 'Color 3', 'booklium' ),
				'slug'  => 'color-3',
				'color' => '#525459',
			),

			array(
				'name'  => esc_html__( 'Color 4', 'booklium' ),
				'slug'  => 'color-4',
				'color' => '#767B7F',
			),
			array(
				'name'  => esc_html__( 'Color 5', 'booklium' ),
				'slug'  => 'color-5',
				'color' => '#C3C3C3',
			),
			array(
				'name'  => esc_html__( 'Color 6', 'booklium' ),
				'slug'  => 'color-6',
				'color' => '#DFDFDF',
			),
			array(
				'name'  => esc_html__( 'Color 7', 'booklium' ),
				'slug'  => 'color-7',
				'color' => '#F6F6F6',
			),
			array(
				'name'  => esc_html__( 'Color 8', 'booklium' ),
				'slug'  => 'color-8',
				'color' => '#F2F1EE',
			),
			array(
				'name'  => esc_html__( 'Color 9', 'booklium' ),
				'slug'  => 'color-9',
				'color' => '#E7E5DF',
			),
			array(
				'name'  => esc_html__( 'Color 10', 'booklium' ),
				'slug'  => 'color-10',
				'color' => '#ffffff',
			),
		) );
	}

	if ( 'style-2' === $color_scheme ) {
		add_theme_support( 'editor-color-palette', array(
			array(
				'name'  => esc_html__( 'Color 11', 'booklium' ),
				'slug'  => 'color-11',
				'color' => '#203536',
			),
			array(
				'name'  => esc_html__( 'Color 1', 'booklium' ),
				'slug'  => 'color-1',
				'color' => '#B09162',
			),
			array(
				'name'  => esc_html__( 'Color 2', 'booklium' ),
				'slug'  => 'color-2',
				'color' => '#17181A',
			),
			array(
				'name'  => esc_html__( 'Color 3', 'booklium' ),
				'slug'  => 'color-3',
				'color' => '#525459',
			),

			array(
				'name'  => esc_html__( 'Color 4', 'booklium' ),
				'slug'  => 'color-4',
				'color' => '#767B7F',
			),
			array(
				'name'  => esc_html__( 'Color 5', 'booklium' ),
				'slug'  => 'color-5',
				'color' => '#C3C3C3',
			),
			array(
				'name'  => esc_html__( 'Color 6', 'booklium' ),
				'slug'  => 'color-6',
				'color' => '#DFDFDF',
			),
			array(
				'name'  => esc_html__( 'Color 7', 'booklium' ),
				'slug'  => 'color-7',
				'color' => '#F6F6F6',
			),
			array(
				'name'  => esc_html__( 'Color 8', 'booklium' ),
				'slug'  => 'color-8',
				'color' => '#F2F1EE',
			),
			array(
				'name'  => esc_html__( 'Color 9', 'booklium' ),
				'slug'  => 'color-9',
				'color' => '#E7E5DF',
			),
			array(
				'name'  => esc_html__( 'Color 10', 'booklium' ),
				'slug'  => 'color-10',
				'color' => '#ffffff',
			),
		) );
	}

	if ( 'default' === $color_scheme ) {

		add_theme_support( 'editor-color-palette', array(
			array(
				'name'  => esc_html__( 'Light Sea Green', 'booklium' ),
				'slug'  => 'light-sea-green',
				'color' => '#13b0bb'
			),
			array(
				'name'  => esc_html__( 'Tangerine Yellow', 'booklium' ),
				'slug'  => 'tangerine-yellow',
				'color' => '#ffce00'
			),
			array(
				'name'  => esc_html__( 'White Smoke', 'booklium' ),
				'slug'  => 'white-smoke',
				'color' => '#ededed'
			),
			array(
				'name'  => esc_html__( 'White Lilac', 'booklium' ),
				'slug'  => 'white-lilac',
				'color' => '#ebebec'
			),
			array(
				'name'  => esc_html__( 'Storm Grey', 'booklium' ),
				'slug'  => 'storm-grey',
				'color' => '#767b80'
			),
			array(
				'name'  => esc_html__( 'Arsenic', 'booklium' ),
				'slug'  => 'arsenic',
				'color' => '#3b4249'
			),
			array(
				'name'  => esc_html__( 'Black', 'booklium' ),
				'slug'  => 'black',
				'color' => '#000000'
			),
			array(
				'name'  => esc_html__( 'White', 'booklium' ),
				'slug'  => 'white',
				'color' => '#ffffff'
			),
		) );
	}
}
