<?php
/**
 * Northmill Product Tab Content.
 *
 * @package flo-sync
 */

namespace NMFS\Products\Product_Fields;

use NMFS\Sync\Items\Articles\Article_Sync_Meta_Helpers;
use WC_Product_Factory;
use WP_Post;

defined( 'ABSPATH' ) || exit;

/**
 * Northmill Product Tab Content.
 */
class Wc_Product_Tab_Content {

	/**
	 * Class constructor.
	 */
	public function __construct() {
		add_action( 'woocommerce_product_data_panels', array( &$this, 'add_custom_product_meta' ) );
		add_action(
			'woocommerce_variation_options_pricing',
			array(
				&$this,
				'add_variation_custom_product_meta',
			),
			10,
			3
		);
	}

	/**
	 * Add meta fields for product edit page.
	 *
	 * @return void
	 */
	public function add_custom_product_meta(): void {
		global $post;
		$product_type = WC_Product_Factory::get_product_type( $post->ID );
		echo '<div id="mf_tab" class="panel woocommerce_options_panel">';
		echo '<div class="options_group">';

		if ( Wc_Product_Tab::PRODUCT_TYPE_VARIABLE !== $product_type ) {
			$args = array(
				'label'             => __( 'Northmill Sync Status', 'flo-sync' ),
				'placeholder'       => __( 'Not synced', 'flo-sync' ),
				'id'                => Article_Sync_Meta_Helpers::META_KEY_SYNC_STATUS,
				'desc_tip'          => true,
				'custom_attributes' => array( 'readonly' => 'readonly' ),
				'description'       => __( 'Sync status with Hub.', 'flo-sync' ),
			);
			woocommerce_wp_text_input( $args );

			$args = array(
				'label'             => __( 'Northmill Sync Data', 'flo-sync' ),
				'placeholder'       => __( 'No sync data available', 'flo-sync' ),
				'id'                => Article_Sync_Meta_Helpers::META_KEY_LAST_SYNC_RESPONSE,
				'desc_tip'          => true,
				'custom_attributes' => array( 'readonly' => 'readonly' ),
				'description'       => __( 'Last sync data from Hub.', 'flo-sync' ),
			);
			woocommerce_wp_textarea_input( $args );
		}

		do_action( 'nmf_wc_product_tab_content', $post );

		echo '</div>';
		echo '</div>';
	}


	/**
	 * Add meta-fields for product variation edit page.
	 *
	 * @param int     $loop           Position in the loop.
	 * @param array   $variation_data Variation data.
	 * @param WP_Post $variation      Post data.
	 *
	 * @return void
	 * @noinspection PhpUnusedParameterInspection WP action/filter function.
	 */
	public function add_variation_custom_product_meta( int $loop, array $variation_data, WP_Post $variation ): void {
		woocommerce_wp_text_input(
			array(
				'id'                => 'mf_sync_status[' . $loop . ']',
				'class'             => 'short',
				'label'             => __( 'Northmill Sync Status', 'flo-sync' ),
				'custom_attributes' => array( 'readonly' => 'readonly' ),
				'value'             => Article_Sync_Meta_Helpers::get_sync_status( $variation->ID ),
			)
		);

		woocommerce_wp_textarea_input(
			array(
				'id'                => 'mf_last_sync_response[' . $loop . ']',
				'class'             => 'short',
				'label'             => __( 'Northmill Sync Data', 'flo-sync' ),
				'placeholder'       => __( 'No sync data available', 'flo-sync' ),
				'description'       => __( 'Last sync data from Hub.', 'flo-sync' ),
				'desc_tip'          => true,
				'custom_attributes' => array(
					'readonly' => 'readonly',
					'cols'     => 20,
					'rows'     => 2,
				),
				'value'             => Article_Sync_Meta_Helpers::get_last_sync_response( $variation->ID ),
			)
		);
	}
}
