<?php

namespace Brandy\Integrations;

use Brandy\Traits\SingletonTrait;

class YaySwatchesIntegration {
	use SingletonTrait;

	public const COLLECTION_SETTINGS_OPTION_NAME = 'yay-swatches-collection-customize-settings';

	public const SWATCH_SETTINGS_OPTION_NAME = 'yay-swatches-swatch-customize-settings';

	public const BUTTON_SETTINGS_OPTION_NAME = 'yay-swatches-button-customize-settings';

	public const SOLD_OUT_SETTINGS_OPTION_NAME = 'yay-swatches-sold-out-customize-settings';

	public const TERM_SWATCH_COLOR_OPTION_PREFIX = 'yay-swatches-swatch-color-';

	public const TERM_SWATCH_DUAL_COLOR_OPTION_PREFIX = 'yay-swatches-swatch-dual-color-';

	public const TERM_SWATCH_IMAGE_OPTION_PREFIX = 'yay-swatches-swatch-image-';

	public const TERM_SWATCH_ATTRIBUTE_STYLE_OPTION_PREFIX = 'yay-swatches-attribute-style-';

	public const TERM_SWATCH_SHOW_HIDE_DUAL_COLOR_OPTION_PREFIX = 'yay-swatches-show-hide-color-';

	protected function __construct() {
		if ( ! defined( 'YAY_SWATCHES_VERSION' ) ) {
			return;
		}
		add_action( 'wp_head', array( $this, 'enqueue_scripts' ) );
	}

	public function enqueue_scripts() { ?>
		<style>
			.yay-swatches-archive-wrapper {margin-top: .75rem;}
			.wp-block-add-to-cart-form .variations_form.cart .variations .reset_variations[style="visibility: hidden;"] {display: none !important;}
			.wp-block-add-to-cart-form .yay-variant-wrapper {display: flex;flex-wrap: wrap;gap: var(--term-spacing, 10px);}
			.wp-block-add-to-cart-form .yay-variant-wrapper > * {margin: 0 !important;}
			.wp-block-add-to-cart-form .variations_form.cart .variations .value {margin: 0 !important;}
			.yayswatches-block-swatches .yay-swatches-archive-wrapper {margin-top:0; margin-bottom:0;}
			.yay-swatches-archive-reset-button {display: flex;}
			.wp-block-brandy-swatches.is-content-justification-center .yay-swatches-archive-reset-button {justify-content: center;}
		</style>
		<?php
	}

	public static function get_collection_customize_settings() {
		// Check if the static method exists before calling to avoid fatal error
		if ( class_exists( '\Yay_Swatches\Helpers\Helper' ) && method_exists( '\Yay_Swatches\Helpers\Helper', 'get_default_collection_customize_settings' ) ) {
			$default_settings = \Yay_Swatches\Helpers\Helper::get_default_collection_customize_settings();
		} else {
			$default_settings = array(
				'swatchSize'                => 'small',
				'swatchCustomSize'          => '50',
				'buttonSize'                => 'small',
				'pictureSize'               => 'thumbnail',
				'label'                     => 'show',
				'limit'                     => 'hide',
				'numberSwatches'            => 3,
				'numberButton'              => 3,
				'actionPlus'                => 'none',
				'collectionShowHideOptions' => 'show',
				'collectionShowStyle'       => 'cross',
				'collectionHideStyle'       => 'automatic',
			);
		}
		return get_option( self::COLLECTION_SETTINGS_OPTION_NAME, $default_settings );
	}

	public static function get_swatch_customize_settings() {
		$default_settings = array(
			'borderActiveColor'          => '#181818',
			'borderNormalColor'          => '#F5F5F5',
			'imagePosition'              => 'fit',
			'imageSize'                  => 'thumbnail',
			'swatchSize'                 => 'medium',
			'swatchCustomSize'           => '50',
			'swatchStyle'                => 'circle',
			'swatchTooltip'              => 'disable',
			'tickSelected'               => 'disable',
			'swatchDesign'               => 'design_3',
			'swatchTooltipAnimation'     => false,
			'swatchTooltipArrow'         => false,
			'swatchTooltipBoxShadow'     => false,
			'swatchTooltipImage'         => false,
			'clearButtonShowHideOptions' => 'show',
			'hideClearButtonOptions'     => 'all',
		);
		return get_option( self::SWATCH_SETTINGS_OPTION_NAME, $default_settings );
	}

	public static function get_button_customize_settings() {
		$default_settings = array(
			'borderActiveColor' => '#181818',
			'borderHoverColor'  => '#181818',
			'borderNormalColor' => '#d4d4d4',
			'borderWidth'       => '1px',
			'borderRadius'      => '4px',
			'buttonActiveColor' => '#181818',
			'buttonHoverColor'  => '#ffffff',
			'buttonNormalColor' => '#ffffff',
			'textActiveColor'   => '#ffffff',
			'textHoverColor'    => '#181818',
			'textNormalColor'   => '#181818',
			'buttonSize'        => 'medium',
		);
		return get_option( self::BUTTON_SETTINGS_OPTION_NAME, $default_settings );
	}

	public static function get_sold_out_customize_settings() {
		$default_settings = array(
			'soldOutShowHideOptions' => 'show',
			'soldOutShowStyle'       => 'cross',
			'soldOutHideStyle'       => 'automatic',
		);
		return get_option( self::SOLD_OUT_SETTINGS_OPTION_NAME, $default_settings );
	}
}
