/* global signifyColorMain, Color */
/**
 * Add a listener to the Color Scheme control to update other color controls to new values/defaults.
 * Also trigger an update of the Color Scheme CSS when a color is changed.
 */

( function( api ) {
	var colorScheme  = signifyColorMain.colorScheme;
	var colorOptions = signifyColorMain.colorOptions;
	var cssTemplate = wp.template( 'signify-color-scheme' ),
		colorSchemeKeys = colorOptions,
		colorSettings   = colorOptions;

	api.controlConstructor.select = api.Control.extend( {
		ready: function() {
			if ( 'color_scheme' === this.id ) {
				this.setting.bind( 'change', function( value ) {
					var colors = colorScheme[value].colors;

					var i = 0;

					jQuery.each( colorOptions, function( index, value ) {
						api( value ).set( colors[i] );
						api.control( value ).container.find( '.color-picker-hex' )
							.data( 'data-default-color', colors[i] )
							.wpColorPicker( 'defaultColor', colors[i] );
						i++;
					});
				});
			}

			// Theme Scheme.
			if ( 'theme_scheme'=== this.id ) {
				this.setting.bind( 'change', function( value ) {
					if ( 'default' === value ) {
						api( 'color_scheme' ).set( 'dark' );
						api( 'color_scheme' ).set( 'default' );
						api( 'signify_hero_content_style' ).set( 'style-one' );
						api( 'signify_events_style' ).set( 'style-one' );
						api( 'signify_header_layout' ).set( 'header-style-one' );
						api( 'signify_hero_content_txt_align' ).set( 'text-align-left' );
						api( 'signify_promo_head_image_position' ).set( 'content-align-left' );
						api( 'signify_body_font' ).set( 'open-sans' );
						api( 'signify_title_font' ).set( 'playfair-display' );
						api( 'signify_tagline_font' ).set( 'open-sans' );
						api( 'signify_content_title_font' ).set( 'playfair-display' );
						api( 'signify_headings_font' ).set( 'playfair-display' );
						api( 'signify_section_title_font' ).set( 'playfair-display' );
					} else if ( 'wedding' === value ) {
						api( 'color_scheme' ).set( 'default' );
						api( 'color_scheme' ).set( 'wedding' );
						api( 'signify_hero_content_style' ).set( 'style-two' );
						api( 'signify_events_style' ).set( 'style-two' );
						api( 'signify_body_font' ).set( 'open-sans' );
						api( 'signify_title_font' ).set( 'playfair-display' );
						api( 'signify_tagline_font' ).set( 'open-sans' );
						api( 'signify_content_title_font' ).set( 'playfair-display' );
						api( 'signify_headings_font' ).set( 'playfair-display' );
						api( 'signify_section_title_font' ).set( 'playfair-display' );
					} else if ( 'education' === value ) {
						api( 'color_scheme' ).set( 'default' );
						api( 'color_scheme' ).set( 'education' );
						api( 'signify_header_layout' ).set( 'header-style-two' );
						api( 'signify_events_style' ).set( 'style-two' );
						api( 'signify_body_font' ).set( 'open-sans' );
						api( 'signify_title_font' ).set( 'playfair-display' );
						api( 'signify_tagline_font' ).set( 'open-sans' );
						api( 'signify_content_title_font' ).set( 'playfair-display' );
						api( 'signify_headings_font' ).set( 'playfair-display' );
						api( 'signify_section_title_font' ).set( 'playfair-display' );
					} else if ( 'photography' === value ) {
						api( 'color_scheme' ).set( 'default' );
						api( 'color_scheme' ).set( 'photography' );
						api( 'signify_header_layout' ).set( 'header-style-two' );
						api( 'signify_events_style' ).set( 'style-two' );
						api( 'signify_hero_content_txt_align' ).set( 'text-align-center' );
						api( 'signify_promo_head_image_position' ).set( 'content-align-center' );
						api( 'signify_body_font' ).set( 'oxygen' );
						api( 'signify_title_font' ).set( 'oswald' );
						api( 'signify_tagline_font' ).set( 'oxygen' );
						api( 'signify_content_title_font' ).set( 'oswald' );
						api( 'signify_headings_font' ).set( 'oswald' );
						api( 'signify_section_title_font' ).set( 'oswald' );
					} else if ( 'corporate' === value ) {
						api( 'color_scheme' ).set( 'default' );
						api( 'color_scheme' ).set( 'corporate' );
						api( 'signify_header_layout' ).set( 'header-style-two' );
						api( 'signify_events_style' ).set( 'style-two' );
						api( 'signify_promo_head_image_position' ).set( 'content-align-center' );
						api( 'signify_body_font' ).set( 'open-sans' );
						api( 'signify_title_font' ).set( 'open-sans' );
						api( 'signify_tagline_font' ).set( 'open-sans' );
						api( 'signify_content_title_font' ).set( 'poppins' );
						api( 'signify_headings_font' ).set( 'poppins' );
						api( 'signify_section_title_font' ).set( 'poppins' );
					} else if ( 'ecommerce' === value ) {
						api( 'color_scheme' ).set( 'default' );
						api( 'color_scheme' ).set( 'ecommerce' );
						api( 'signify_events_style' ).set( 'style-two' );
						api( 'signify_body_font' ).set( 'open-sans' );
						api( 'signify_title_font' ).set( 'playfair-display' );
						api( 'signify_tagline_font' ).set( 'open-sans' );
						api( 'signify_content_title_font' ).set( 'playfair-display' );
						api( 'signify_headings_font' ).set( 'playfair-display' );
						api( 'signify_section_title_font' ).set( 'playfair-display' );
						
						if ( undefined !== api( 'signify_woocommerce_layout' ) ) {
							api( 'signify_woocommerce_layout' ).set( 'right-sidebar' );
						}
					} else if ( 'tune' === value ) {
						api( 'color_scheme' ).set( 'default' );
						api( 'color_scheme' ).set( 'tune' );
						api( 'signify_header_layout' ).set( 'header-style-two' );
						api( 'signify_body_font' ).set( 'open-sans' );
						api( 'signify_title_font' ).set( 'poppins' );
						api( 'signify_tagline_font' ).set( 'open-sans' );
						api( 'signify_content_title_font' ).set( 'poppins' );
						api( 'signify_headings_font' ).set( 'poppins' );
						api( 'signify_section_title_font' ).set( 'poppins' );
					} else if ( 'music-dark' === value ) {
						api( 'color_scheme' ).set( 'default' );
						api( 'color_scheme' ).set( 'music-dark' );
						api( 'signify_header_layout' ).set( 'header-style-two' );
						api( 'signify_body_font' ).set( 'open-sans' );
						api( 'signify_title_font' ).set( 'poppins' );
						api( 'signify_tagline_font' ).set( 'open-sans' );
						api( 'signify_content_title_font' ).set( 'poppins' );
						api( 'signify_headings_font' ).set( 'poppins' );
						api( 'signify_section_title_font' ).set( 'poppins' );
					}
				});
			}
		}
	} );

	// Generate the CSS for the current Color Scheme.
	function updateCSS() {
		var scheme = api( 'color_scheme' )(),
			css,
			colors = _.object( colorSchemeKeys, colorScheme[ scheme ].colors );

		// Merge in color scheme overrides.
		_.each( colorSettings, function( setting ) {
			colors[ setting ] = api( setting )();
		});

		// Add additional color.
		// jscs:disable

		// jscs:enable

		css = cssTemplate( colors );

		api.previewer.send( 'update-color-scheme-css', css );
	}

	// Update the CSS whenever a color setting is changed.
	_.each( colorSettings, function( setting ) {
		api( setting, function( setting ) {
			setting.bind( updateCSS );
		});
	});
} )( wp.customize );
