<?php

namespace WooSwatches;

use function WooSwatchesPro\is_license_valid;

class Assets {

	public function __construct() {
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_public_assets' ) );		
	}	

	public function enqueue_public_assets() {
		
		$is_defer = is_wp_version_compatible( '6.3' ) ? array( 'strategy' => 'defer' ) : true;
		
		wp_register_style( 'gs-tippyjs', GS_WOO_SWATCHES_PLUGIN_URI . '/assets/libs/tippyjs/tippy.css', array(), GS_WOO_SWATCHES_VERSION );
		wp_register_script( 'gs-tippyjs', GS_WOO_SWATCHES_PLUGIN_URI . '/assets/libs/tippyjs/tippy-bundle.umd.min.js', array(), GS_WOO_SWATCHES_VERSION, $is_defer );

		if ( ! is_pro_active() || ! is_license_valid() ) {
			wp_register_style( 'gs-woo-variation-swatches', GS_WOO_SWATCHES_PLUGIN_URI . '/assets/css/public.min.css', array(), GS_WOO_SWATCHES_VERSION );
			wp_register_script( 'gs-woo-variation-swatches', GS_WOO_SWATCHES_PLUGIN_URI . '/assets/js/public.min.js', array( 'jquery', 'gs-tippyjs' ), GS_WOO_SWATCHES_VERSION, $is_defer );
		}

		do_action( GS_WOO_SWATCHES_HOOK_PREFIX . 'public-scripts' );

		if ( \is_product() || \is_shop() ) {

			wp_enqueue_style( 'gs-woo-variation-swatches' );
			wp_enqueue_script( 'gs-woo-variation-swatches' );

			$this->add_inline_style();

			$data = [
				'nonce'                   => wp_create_nonce( 'gs_variation_swatches' ),
				'ajax_url'                => admin_url( 'admin-ajax.php' ),
				'admin_url'               => admin_url(),
				'site_url'                => home_url(),
				'settings_options'        => Helpers::get_settings()
			];
	
			wp_localize_script( 'gs-woo-variation-swatches', '_swatches_data', $data );
		}
	
	}

	public function style_settings() {
		$style_settings = (array) plugin()->modules->get_module_settings( 'style-settings' );
		return $style_settings;
	}

	public function add_inline_style() {
		$style  = $this->implode_css_property_value( $this->inline_style_declaration() );
		$style = sprintf( 'body .gs-variable-items-wrapper > li {%s}', $style );
		wp_add_inline_style( 'gs-woo-variation-swatches', minimize_css_simple( $style ) );
	}

	public function implode_css_property_value( $raw_properties ) {
		$properties = array();
		foreach ( $raw_properties as $prop => $value ) {
			$properties[] = esc_attr( $prop ) . ':' . esc_attr( $value );
		}
		return implode( ";\n", $properties );
	}

	public function inline_style_declaration() {

		$styles    = $this->style_settings();

		$width     = absint( $styles['width'] );
		$height    = absint( $styles['height'] );
		$font_size = absint( $styles['font_size'] );

		$tick_color  = $styles['tick_color'];
		$cross_color = $styles['cross_color'];

		$text_color = $styles['text_color'];
		$text_color_selected = $styles['text_color_selected'];
		$text_color_disabled = $styles['text_color_disabled'];
		$text_color_selected_disabled = $styles['text_color_selected_disabled'];

		$bg_color = $styles['bg_color'];
		$bg_color_selected = $styles['bg_color_selected'];
		$bg_color_disabled = $styles['bg_color_disabled'];
		$bg_color_selected_disabled = $styles['bg_color_selected_disabled'];

		$br_color = $styles['br_color'];
		$br_color_selected = $styles['br_color_selected'];
		$br_color_disabled = $styles['br_color_disabled'];
		$br_color_selected_disabled = $styles['br_color_selected_disabled'];

		$declaration = array();

		if ( !empty($font_size) ) $declaration['--gs-wvs--field-font-size'] = sprintf( '%spx', $font_size );

		if ( !empty($width) && !empty($height) ) {
			$declaration['--gs-wvs--field-width'] = sprintf( '%spx', $width );
			$declaration['--gs-wvs--field-height'] = sprintf( '%spx', $height );
		}

		if ( !empty($tick_color) ) $declaration['--gs-wvs--tick-color'] = $tick_color;
		if ( !empty($cross_color) ) $declaration['--gs-wvs--close-color'] = $cross_color;

		if ( !empty($text_color) ) $declaration['--gs-wvs--field-color'] = $text_color;
		if ( !empty($text_color_selected) ) $declaration['--gs-wvs--field-color--selected'] = $text_color_selected;
		if ( !empty($text_color_disabled) ) $declaration['--gs-wvs--field-color--disabled'] = $text_color_disabled;
		if ( !empty($text_color_selected_disabled) ) $declaration['--gs-wvs--field-color--disabled-selected'] = $text_color_selected_disabled;

		if ( !empty($bg_color) ) $declaration['--gs-wvs--field-bg-color'] = $bg_color;
		if ( !empty($bg_color_selected) ) $declaration['--gs-wvs--field-bg-color--selected'] = $bg_color_selected;
		if ( !empty($bg_color_disabled) ) $declaration['--gs-wvs--field-bg-color--disabled'] = $bg_color_disabled;
		if ( !empty($bg_color_selected_disabled) ) $declaration['--gs-wvs--field-bg-color--disabled-selected'] = $bg_color_selected_disabled;

		if ( !empty($br_color) ) $declaration['--gs-wvs--field-br-color'] = $br_color;
		if ( !empty($br_color_selected) ) $declaration['--gs-wvs--field-br-color--selected'] = $br_color_selected;
		if ( !empty($br_color_disabled) ) $declaration['--gs-wvs--field-br-color--disabled'] = $br_color_disabled;
		if ( !empty($br_color_selected_disabled) ) $declaration['--gs-wvs--field-br-color--disabled-selected'] = $br_color_selected_disabled;
		
		return apply_filters( GS_WOO_SWATCHES_HOOK_PREFIX . 'inline_style_declaration', $declaration );
	}

}