<?php
function ci_theme_customize_register( $wp_customize ) {

	// Partial for various settings that affect the customizer styles, but can't have a dedicated icon, e.g. typography controls.
	$wp_customize->selective_refresh->add_partial(
		'theme_style',
		array(
			'selector'            => '#ci-color-scheme-inline-css',
			'render_callback'     => 'ci_theme_get_all_customizer_css',
			'settings'            => array_keys( ci_theme_get_registered_typography_controls() ),
			'container_inclusive' => false,
		)
	);

	$wp_customize->selective_refresh->add_partial(
		'theme_gfonts',
		array(
			'selector'            => '#ci-theme-user-google-fonts-css',
			'render_callback'     => 'ci_theme_customize_preview_google_fonts',
			'settings'            => array_keys( ci_theme_get_registered_typography_controls() ),
			'container_inclusive' => true,
		)
	);

	//
	// Typography
	//
	$wp_customize->add_section(
		'typography',
		array(
			'title'    => _x( 'Typography Options', 'customizer section title', 'ci_theme' ),
			'priority' => 40,
		)
	);

	require_once get_theme_file_path( '/functions/customizer/sections/global/typography.php' );

	$wp_customize->add_panel( 'theme_colors', array(
		'title'    => esc_html_x( 'Theme Colors', 'customizer section title', 'ci_theme' ),
		'priority' => 30,
	) );

	$wp_customize->add_section( 'theme_colors_header', array(
		'title'    => esc_html_x( 'Header', 'customizer section title', 'ci_theme' ),
		'panel'    => 'theme_colors',
		'priority' => 20,
	) );

	$wp_customize->add_setting( 'header_bg', array(
		'transport'         => 'postMessage',
		'default'           => '',
		'sanitize_callback' => 'sanitize_hex_color',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'header_bg', array(
		'section' => 'theme_colors_header',
		'label'   => esc_html__( 'Header Background', 'ci_theme' ),
	) ) );

	$wp_customize->add_setting( 'header_menu_color', array(
		'transport'         => 'postMessage',
		'default'           => '',
		'sanitize_callback' => 'sanitize_hex_color',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'header_menu_color', array(
		'section' => 'theme_colors_header',
		'label'   => esc_html__( 'Menu Text Color', 'ci_theme' ),
	) ) );

	$wp_customize->add_setting( 'header_submenu_color', array(
		'transport'         => 'postMessage',
		'default'           => '',
		'sanitize_callback' => 'sanitize_hex_color',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'header_submenu_color', array(
		'section' => 'theme_colors_header',
		'label'   => esc_html__( 'Submenu Text Color', 'ci_theme' ),
	) ) );

	$wp_customize->add_setting( 'header_menu_background', array(
		'transport'         => 'postMessage',
		'default'           => '',
		'sanitize_callback' => 'sanitize_hex_color',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'header_menu_background', array(
		'section' => 'theme_colors_header',
		'label'   => esc_html__( 'Submenu Background Color', 'ci_theme' ),
	) ) );

	$wp_customize->add_setting( 'header_menu_background_hover', array(
		'transport'         => 'postMessage',
		'default'           => '',
		'sanitize_callback' => 'sanitize_hex_color',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'header_menu_background_hover', array(
		'section' => 'theme_colors_header',
		'label'   => esc_html__( 'Submenu Background Hover Color', 'ci_theme' ),
	) ) );

	$wp_customize->add_section( 'theme_colors_global', array(
		'title'    => esc_html_x( 'Global', 'customizer section title', 'ci_theme' ),
		'panel'    => 'theme_colors',
		'priority' => 30,
	) );

	// Rename & Reposition the header image section.
	$wp_customize->get_control( 'background_color' )->section      = 'theme_colors_global';
	$wp_customize->get_control( 'background_image' )->section      = 'theme_colors_global';
	$wp_customize->get_control( 'background_preset' )->section     = 'theme_colors_global';
	$wp_customize->get_control( 'background_position' )->section   = 'theme_colors_global';
	$wp_customize->get_control( 'background_size' )->section       = 'theme_colors_global';
	$wp_customize->get_control( 'background_repeat' )->section     = 'theme_colors_global';
	$wp_customize->get_control( 'background_attachment' )->section = 'theme_colors_global';

	$wp_customize->add_setting( 'site_accent_color', array(
		'transport'         => 'postMessage',
		'default'           => '',
		'sanitize_callback' => 'sanitize_hex_color',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'site_accent_color', array(
		'section' => 'theme_colors_global',
		'label'   => esc_html__( 'Accent color', 'ci_theme' ),
	) ) );

	$wp_customize->add_setting( 'site_primary_color', array(
		'transport'         => 'postMessage',
		'default'           => '',
		'sanitize_callback' => 'sanitize_hex_color',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'site_primary_color', array(
		'section' => 'theme_colors_global',
		'label'   => esc_html__( 'Primary color', 'ci_theme' ),
	) ) );

	$wp_customize->add_setting( 'site_secondary_color', array(
		'transport'         => 'postMessage',
		'default'           => '',
		'sanitize_callback' => 'sanitize_hex_color',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'site_secondary_color', array(
		'section' => 'theme_colors_global',
		'label'   => esc_html__( 'Secondary color', 'ci_theme' ),
	) ) );

	$wp_customize->add_setting( 'site_text_color', array(
		'transport'         => 'postMessage',
		'default'           => '',
		'sanitize_callback' => 'sanitize_hex_color',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'site_text_color', array(
		'section' => 'theme_colors_global',
		'label'   => esc_html__( 'Text color', 'ci_theme' ),
	) ) );

	$wp_customize->add_setting( 'site_border_color', array(
		'transport'         => 'postMessage',
		'default'           => '',
		'sanitize_callback' => 'sanitize_hex_color',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'site_border_color', array(
		'section' => 'theme_colors_global',
		'label'   => esc_html__( 'Border color', 'ci_theme' ),
	) ) );

	$wp_customize->add_setting( 'site_header_color', array(
		'transport'         => 'postMessage',
		'default'           => '',
		'sanitize_callback' => 'sanitize_hex_color',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'site_header_color', array(
		'section' => 'theme_colors_global',
		'label'   => esc_html__( 'Heading color', 'ci_theme' ),
	) ) );

	$wp_customize->add_setting( 'site_form_bg', array(
		'transport'         => 'postMessage',
		'default'           => '',
		'sanitize_callback' => 'sanitize_hex_color',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'site_form_bg', array(
		'section' => 'theme_colors_global',
		'label'   => esc_html__( 'Form fields background color', 'ci_theme' ),
	) ) );

	$wp_customize->add_setting( 'site_form_color', array(
		'transport'         => 'postMessage',
		'default'           => '',
		'sanitize_callback' => 'sanitize_hex_color',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'site_form_color', array(
		'section' => 'theme_colors_global',
		'label'   => esc_html__( 'Form fields text color', 'ci_theme' ),
	) ) );

	$wp_customize->add_setting( 'site_form_border', array(
		'transport'         => 'postMessage',
		'default'           => '',
		'sanitize_callback' => 'sanitize_hex_color',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'site_form_border', array(
		'section' => 'theme_colors_global',
		'label'   => esc_html__( 'Form fields border color', 'ci_theme' ),
	) ) );

	$wp_customize->add_setting( 'site_button_bg', array(
		'transport'         => 'postMessage',
		'default'           => '',
		'sanitize_callback' => 'sanitize_hex_color',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'site_button_bg', array(
		'section' => 'theme_colors_global',
		'label'   => esc_html__( 'Button background color', 'ci_theme' ),
	) ) );

	$wp_customize->add_setting( 'site_button_text', array(
		'transport'         => 'postMessage',
		'default'           => '',
		'sanitize_callback' => 'sanitize_hex_color',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'site_button_text', array(
		'section' => 'theme_colors_global',
		'label'   => esc_html__( 'Button text color', 'ci_theme' ),
	) ) );

	$wp_customize->add_setting( 'site_button_bg_hover', array(
		'transport'         => 'postMessage',
		'default'           => '',
		'sanitize_callback' => 'sanitize_hex_color',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'site_button_bg_hover', array(
		'section' => 'theme_colors_global',
		'label'   => esc_html__( 'Button background hover color', 'ci_theme' ),
	) ) );

	$wp_customize->add_setting( 'site_button_border_color', array(
		'transport'         => 'postMessage',
		'default'           => '',
		'sanitize_callback' => 'sanitize_hex_color',
	) );
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'site_button_border_color', array(
		'section' => 'theme_colors_global',
		'label'   => esc_html__( 'Button border color', 'ci_theme' ),
	) ) );

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

add_action( 'customize_register', 'ci_theme_customize_register_custom_controls', 9 );
/**
 * Registers custom Customizer controls.
 *
 * @param WP_Customize_Manager $wpc Reference to the customizer's manager object.
 */
function ci_theme_customize_register_custom_controls( $wp_customize ) {

	require_once get_theme_file_path( '/functions/customizer/controls/typography/typography.php' );
	$wp_customize->register_control_type( 'CI_Theme_Customize_Typography_Control' );
}

/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
function ci_theme_base_customize_preview_js() {
	$theme = wp_get_theme();

	wp_enqueue_script( 'ci-theme-customizer-preview', get_template_directory_uri() . '/js/customizer-preview.js', array( 'customize-preview' ), $theme->get( 'Version' ), true );

	// Generic preview code.
	wp_enqueue_script( 'ci-theme-customizer-typo-preview', get_theme_file_uri( '/functions/customizer/preview/preview.js' ), array( 'jquery' ), $theme->get( 'Version' ), true );

	// Options-specific preview code.
	wp_enqueue_script( 'ci-theme-customizer-preview-global', get_theme_file_uri( '/functions/customizer/preview/global.js' ), array( 'jquery' ), $theme->get( 'Version' ), true );
}
add_action( 'customize_preview_init', 'ci_theme_base_customize_preview_js' );

add_action( 'customize_controls_enqueue_scripts', 'ci_theme_customize_controls_js' );
/**
 * Registers/Enqueues styles and scripts for customizer controls.
 */
function ci_theme_customize_controls_js() {
	wp_enqueue_style( 'ci-theme-customizer-controls', get_theme_file_uri( '/functions/customizer/controls/style.css' ), array(), wp_get_theme()->get( 'Version' ) );
}

/**
 * Helpers
 */
function ci_theme_typography_control_defaults_empty_breakpoints( $override_breakpoints = array() ) {
	$defaults = array(
		'desktop' => array(
			'family'     => '',
			'variant'    => '',
			'size'       => '',
			'lineHeight' => '',
			'transform'  => '',
			'spacing'    => '',
			'is_gfont'   => false,
		),
		'tablet'  => array(
			'family'     => '',
			'variant'    => '',
			'size'       => '',
			'lineHeight' => '',
			'transform'  => '',
			'spacing'    => '',
			'is_gfont'   => false,
		),
		'mobile'  => array(
			'family'     => '',
			'variant'    => '',
			'size'       => '',
			'lineHeight' => '',
			'transform'  => '',
			'spacing'    => '',
			'is_gfont'   => false,
		),
	);

	$return = array();
	foreach ( $defaults as $breakpoint => $values ) {
		if ( isset( $override_breakpoints[ $breakpoint ] ) && is_array( $override_breakpoints[ $breakpoint ] ) ) {
			$return[ $breakpoint ] = array_merge( $values, $override_breakpoints[ $breakpoint ] );
		} else {
			$return[ $breakpoint ] = $values;
		}
	}

	return $return;
}

function ci_theme_sanitize_typography_control_breakpoints( $values ) {
	$defaults = ci_theme_typography_control_defaults_empty_breakpoints();

	if ( ! empty( $values ) && is_string( $values ) ) {
		$values = json_decode( $values, true );
	}

	if ( ! is_array( $values ) ) {
		return $defaults;
	}

	$values = wp_parse_args( $values, $defaults );

	foreach ( $values as $breakpoint => $value ) {
		if ( ! array_key_exists( $breakpoint, $defaults ) ) {
			unset( $values[ $breakpoint ] );
		}
	}

	$new_values = array();

	foreach ( $values as $breakpoint => $breakpoint_values ) {
		if ( array_key_exists( $breakpoint, $defaults ) ) {
			$new_breakpoint_values = wp_parse_args( $breakpoint_values, $defaults[ $breakpoint ] );

			$new_breakpoint_values['family']     = sanitize_text_field( $new_breakpoint_values['family'] );
			$new_breakpoint_values['variant']    = sanitize_text_field( $new_breakpoint_values['variant'] );
			$new_breakpoint_values['size']       = ci_theme_sanitize_intval_or_empty( $new_breakpoint_values['size'] );
			$new_breakpoint_values['lineHeight'] = ci_theme_sanitize_intval_or_empty( $new_breakpoint_values['lineHeight'] );
			$new_breakpoint_values['transform']  = sanitize_text_field( $new_breakpoint_values['transform'] );
			$new_breakpoint_values['spacing']    = ci_theme_sanitize_floatval_or_empty( $new_breakpoint_values['spacing'] );
			$new_breakpoint_values['is_gfont']   = (bool) intval( $new_breakpoint_values['is_gfont'] );

			$new_values[ $breakpoint ] = $new_breakpoint_values;
		}
	}

	return $new_values;
}

/**
 * Customizer partial callbacks.
 */
require_once get_theme_file_path( '/functions/customizer/partial-callbacks.php' );

/**
 * CSS Generator.
 */
require_once get_theme_file_path( '/functions/customizer/class-ci-theme-customizer-css-generator.php' );

/**
 * Customizer generated styles.
 */
require_once get_theme_file_path( '/functions/customizer/generated-styles.php' );
