<?php

namespace WooSwatches;

defined( 'ABSPATH' ) || exit;

class Helpers {

	static function get_color_field_style() {
		return array(
			array(
				'label' => __( 'Style One', 'gs-woo-variation' ),
				'value' => 'style_one'
			),
			array(
				'label' => __( 'Style Two', 'gs-woo-variation' ),
				'value' => 'style_two'
			),
			array(
				'label' => __( 'Style Three', 'gs-woo-variation' ),
				'value' => 'style_three'
			),
			array(
				'label' => __( 'Style Four', 'gs-woo-variation' ),
				'value' => 'style_four'
			),
		);
	}

	static function get_image_field_style() {
		return array(
			array(
				'label' => __( 'Style One', 'gs-woo-variation' ),
				'value' => 'style_one',
			),
			array(
				'label' => __( 'Style Two', 'gs-woo-variation' ),
				'value' => 'style_two',
			),
			array(
				'label' => __( 'Style Three', 'gs-woo-variation' ),
				'value' => 'style_three',
			)
		);
	}

	static function get_button_field_style() {
		return array(
			array(
				'label' => __( 'Style One', 'gs-woo-variation' ),
				'value' => 'style_one',
			),
			array(
				'label' => __( 'Style Two', 'gs-woo-variation' ),
				'value' => 'style_two',
			),
			array(
				'label' => __( 'Style Three', 'gs-woo-variation' ),
				'value' => 'style_three',
			)
		);
	}

	static function get_shape_styles() {
		return array(
			array(
				'label' => __( 'Rounded Shape', 'gs-woo-variation' ),
				'value' => 'rounded_shape',
			),
			array(
				'label' => __( 'Squared Shape', 'gs-woo-variation' ),
				'value' => 'square_shape',
			),
		);
	}

	static function get_attr_styles() {
		return array(
			array(
				'label' => __( 'Fade With cross', 'gs-woo-variation' ),
				'value' => 'fade_with_cross',
			),
			array(
				'label' => __( 'Fade Without cross', 'gs-woo-variation' ),
				'value' => 'fade_without_cross',
			),
			array(
				'label' => __( 'Hide', 'gs-woo-variation' ),
				'value' => 'hide',
			),
		);
	}

	static function get_img_sizes() {
		$sizes = get_intermediate_image_sizes();

		if ( empty( $sizes ) ) {
			return array();
		}

		$result = array();

		foreach ( $sizes as $size ) {
			$result[] = array(
				'label' => ucwords( preg_replace( '/_|-/', ' ', $size ) ),
				'value' => $size,
			);
		}

		return $result;
	}

	static function get_class_name( $file_name ) {
		$parts = explode( '-', $file_name );
		$parts = array_map( 'ucfirst', $parts );
		return implode( '_', $parts );
	}

	static function get_attribute_taxonomy( $attribute_name ) {
		$taxonomy_attributes = wc_get_attribute_taxonomies();

		$attribute_name = str_ireplace( 'pa_', '', wc_sanitize_taxonomy_name( $attribute_name ) );

		foreach ( $taxonomy_attributes as $attribute ) {
			if ( false === stripos( $attribute->attribute_name, $attribute_name ) ) {
				continue;
			}

			return $attribute;
		}

		return false;
	}

	static function get_settings() {
		return plugin()->modules->get_settings();
	}

	static function get_product_attr_image( $term, $data = array() ) {
		$term_id = 0;
		if ( is_numeric( $term ) ) {
			$term_id = $term;
		}

		if ( is_object( $term ) ) {
			$term_id = $term->term_id;
		}

		return get_term_meta( $term_id, 'product_attribute_image', true );
	}

	static function get_product_attr_color( $term ) {
		$term_id = 0;
		if ( is_numeric( $term ) ) {
			$term_id = $term;
		}

		if ( is_object( $term ) ) {
			$term_id = $term->term_id;
		}

		return get_term_meta( $term_id, 'product_attribute_color', true );
	}

	static function select_attr_types() {
		return array(
			'select' => __( 'Select', 'gs-woo-variation' ),
			'color'  => __( 'Color', 'gs-woo-variation' ),
			'image'  => __( 'Image', 'gs-woo-variation' ),
			'button' => __( 'Button', 'gs-woo-variation' ),
			'radio'  => __( 'Radio', 'gs-woo-variation' ),
		);
	}

	static function sanitize_name( $value ) {
		return wc_clean( rawurldecode( sanitize_title( wp_unslash( $value ) ) ) );
	}

	static function get_product_default_settings() {
		return [
			'dropdown_to_button' => 'global',
			'color_field_style'  => 'global',
			'color_shape_style'  => 'global',
			'image_field_style'  => 'global',
			'image_shape_style'  => 'global',
			'button_field_style' => 'global',
			'button_shape_style' => 'global'
		];
	}

	static function get_product_settings( $product_id ) {
		$product_settings = get_post_meta( $product_id, 'variable_swatches_settings', true ) ?: [];
		return array_merge( self::get_product_default_settings(), $product_settings );
	}

	static function get_product_settings_for_display( $product_id ) {

		$settings = self::get_product_settings( $product_id );
		$global  = plugin()->modules->get_module_settings( 'general-settings' );

		foreach ( $settings as $key => $value ) {
			if ( $value === 'global' ) $settings[ $key ] = $global[ $key ];
		}

		return $settings;

	}
	
}
