<?php

namespace WooSwatches;

defined( 'ABSPATH' ) || exit;

class Attributes {

	public function __construct() {

		// Add product option terms
		add_action( 'woocommerce_product_option_terms', array( $this, 'gs_wc_option_terms' ), 10, 3 );

		// Add custom tab
		add_filter( 'woocommerce_product_data_tabs', array( $this, 'product_data_tab' ) );

		// Add Custom Settings Field on Custom Tab
		add_action( 'woocommerce_product_data_panels', array( $this, 'product_data_panel' ) );

		// Type Selector on attributes page
		add_filter( 'product_attributes_type_selector', array( $this, 'select_attr_types' ) );

		// Add attr meta
		add_action( 'admin_init', array( $this, 'add_attr_meta' ) );

	}

	public function select_attr_types() {
		return Helpers::select_attr_types();
	}

	public function product_data_tab( $tabs ) {
		$tabs['woo_variation_swatches'] = array(
			'label'    => esc_html__( 'Swatches Settings', 'gs-woo-variation' ),
			'target'   => 'gs-variation-swatches',
			'class'    => array( 'show_if_variable' ),
			'priority' => 69
		);

		return $tabs;
	}

	public function product_data_panel() {
		global $product_object;

		$product_id = $product_object->get_id();
			
		?>

		<div data-product_id="<?php echo esc_attr( $product_id ); ?>" id="gs-variation-swatches" class="panel gs-settings-panel hidden wc-metaboxes-wrapper">
			<div class="gs-woo-swatches--upgrade-message" style="padding: 40px; background: #e7e8eb;">
				<h3><?php _e( 'Unlock the advanced features!', 'gs-woo-variation' ); ?></h3>
				<p style="font-size: 14px; max-width: 700px; line-height: 1.65;"><?php _e( "With our Premium version, you will get advanced features like Disabled Attributes, Exclude Categories, Auto Generate Variation URLs, etc. Don't be late to upgrade the plugin today!", 'gs-woo-variation' ); ?></p>
				<a class="button button-primary button-large" href="https://www.gsplugins.com/product/woocommerce-variation-swatches/" target="_blank"><?php _e( 'Upgrade Now', 'gs-woo-variation' ); ?></a>
			</div>
		</div>

		<?php

	}

	public function add_attr_meta() {
		$meta_fields     = $this->attr_meta_fields();
		$attr_taxonomies = wc_get_attribute_taxonomies();

		if ( $attr_taxonomies ) {
			foreach ( $attr_taxonomies as $taxonomy ) {
				$attr_name = wc_attribute_taxonomy_name( $taxonomy->attribute_name );
				$attr_type = $taxonomy->attribute_type;
				if ( in_array( $attr_type, array_keys( $meta_fields ) ) ) {
					new Term_Meta( $attr_name, 'product', $meta_fields[ $attr_type ] );
				}
			}
		}
	}

	public function attr_meta_fields() {
		$meta_fields = array();

		$meta_fields['button'] = array();

		$meta_fields['radio'] = array();

		$meta_fields['color'] = array(
			array(
				'label' => esc_html__( 'Color', 'gs-woo-variation' ),
				'desc'  => esc_html__( 'Choose a color', 'gs-woo-variation' ),
				'id'    => 'product_attribute_color',
				'type'  => 'color'
			)
		);


		if ( ! is_pro_active() ) {
			$duel_color = array(
				'label'   => esc_html__( 'Is Dual Color', 'gs-woo-variation-pro' ) . ' <strong>Pro</strong>',
				'desc'    => esc_html__( 'Make dual color', 'gs-woo-variation-pro' ),
				'id'      => 'isdu_c',
				'type'    => 'select2',
				'options' => array(
					'no'  => esc_html__( 'No', 'gs-woo-variation-pro' ),
				),
				'is_pro_field' => true
			);

			array_push( $meta_fields['color'], $duel_color );
		}

		$meta_fields['image'] = array(
			array(
				'label' => esc_html__( 'Image', 'gs-woo-variation' ),
				'desc'  => esc_html__( 'Image', 'gs-woo-variation' ),
				'id'    => 'product_attribute_image',
				'type'  => 'image'
			)
		);

		if ( ! is_pro_active() ) {

			$all_types = array( 'color', 'image', 'button', 'radio' );

			$common_fields = array(
				array(
					'label'   => esc_html__( 'Show Tooltip', 'gs-woo-variation-pro' ) . ' <strong>Pro</strong>',
					'desc'    => esc_html__( 'Individually show or hide tooltip.', 'gs-woo-variation-pro' ),
					'id'      => '_shw_tlp',
					'type'    => 'select2',
					'options' => array(
						'text'  => esc_html__( 'Text', 'gs-woo-variation-pro' )
					),
					'is_pro_field' => true
				),
				array(
					'label'      => esc_html__( 'Tooltip Text', 'gs-woo-variation-pro' ) . ' <strong>Pro</strong>',
					'desc'       => esc_html__( 'Tooltip text. Default tooltip text will be term name.', 'gs-woo-variation-pro' ),
					'id'         => '_tlp_txt',
					'type'       => 'text',
					'is_pro_field' => true
				)
			);
	
			foreach ( $all_types as $type ) {
				foreach ( $common_fields as $field ) {
					array_push( $meta_fields[ $type ], $field );
				}
			}

		}

		$meta_fields = apply_filters( 'woo_variation_swatches_attr_meta_fields', $meta_fields );

		return $meta_fields;
	}

	public function gs_wc_option_terms( $attribute_taxonomy, $i, $attribute ) {
		if ( 'select' !== $attribute_taxonomy->attribute_type && in_array( $attribute_taxonomy->attribute_type, array_keys( $this->select_attr_types() ) ) ) {

			$name = sprintf( 'attribute_values[%s][]', esc_attr( $i ) );

			?>

			<select multiple="multiple" data-placeholder="<?php esc_attr_e( 'Select terms', 'gs-woo-variation-pro' ); ?>" class="multiselect attribute_values wc-enhanced-select" name="<?php echo esc_attr( $name ); ?>">

				<?php

				$args = array(
					'orderby' => ! empty( $attribute_taxonomy->attribute_orderby ) ? $attribute_taxonomy->attribute_orderby : 'name',
					'hide_empty' => 0,
				);

				$all_terms = get_terms( $attribute->get_taxonomy(), $args );

				if ( $all_terms ) {
					foreach ( $all_terms as $term ) {
						$options = $attribute->get_options();
						$options = ! empty( $options ) ? $options : array();
						echo '<option value="' . esc_attr( $term->term_id ) . '"' . wc_selected( $term->term_id, $options ) . '>' . esc_html( $term->name, $term ) . '</option>';
					}
				}

				?>

			</select>
			
			<button class="button plus select_all_attributes"><?php esc_html_e( 'Select all', 'gs-woo-variation-pro' ); ?></button>
			<button class="button minus select_no_attributes"><?php esc_html_e( 'Select none', 'gs-woo-variation-pro' ); ?></button>
			<button class="button fr plus add_new_attribute"><?php esc_html_e( 'Add new', 'gs-woo-variation-pro' ); ?></button>
			
			<?php
		}
	}

}
