<?php

namespace HulkPlugins\Woo\ProductConditionManager\Controllers;

use HulkPlugins\Woo\ProductConditionManager\Models\ProductModel;
use HulkPlugins\Woo\ProductConditionManager\Models\SettingsModel;
use HulkPlugins\Woo\ProductConditionManager\Traits\SingletonTrait;
use HulkPlugins\Woo\ProductConditionManager\Utils\Helper;
use function HulkPlugins\Woo\ProductConditionManager\hulk_woo_pcm_fs;
use const HulkPlugins\Woo\ProductConditionManager\DOCUMENTATION;
use const HulkPlugins\Woo\ProductConditionManager\PLUGIN_DIR_PATH;
use const HulkPlugins\Woo\ProductConditionManager\PLUGIN_DIR_URL;

defined( 'ABSPATH' ) || exit;

class WCFMFrontendManager {
	use SingletonTrait;

	private ProductModel $model;

	public function __construct() {
		$this->model = ProductModel::getInstance();
	}

	public function init_hooks(): void {
		add_action( 'wcfm_product_manager_left_panel_after', [ $this,'after_products_manage' ] );
		add_action( 'after_wcfm_products_manage_meta_save', [ $this, 'after_products_manage_meta_save' ], PHP_INT_MAX, 2 );
	}

	public function after_products_manage( $product_id ): void {
		?>
		<p class="description wcfm_title wcfm_full_ele">
			<strong>
				<?php echo esc_attr__( 'Product Condition', 'product-condition-manager-for-woocommerce' ); ?>
			</strong>
		</p>
		<div id="hulk_woo_pcm_settings"></div>
		<?php

		$settings_assets = require PLUGIN_DIR_PATH . 'build/admin/settings/index.asset.php';

		wp_enqueue_style(
			'hulk_woo_pcm_settings',
			PLUGIN_DIR_URL . 'build/admin/settings/index.css',
			[ 'wp-components' ],
			$settings_assets['version'],
		);

		wp_enqueue_script(
			'hulk_woo_pcm_settings',
			PLUGIN_DIR_URL . 'build/admin/settings/index.js',
			$settings_assets['dependencies'],
			$settings_assets['version'],
			true
		);

		wp_localize_script(
			'hulk_woo_pcm_settings',
			'hulk_woo_pcm_settings',
			[
				'DOCUMENTATION'  => DOCUMENTATION,
				'PLUGIN_DIR_URL' => PLUGIN_DIR_URL,
				'appName'        => 'ProductDataMetaSettings',
				'conditions'     => Helper::getDefaultConditions(),
				'settings'       => $this->model->getAllMeta( $product_id ),
				'globalSettings' => SettingsModel::getInstance()->getSettings(),
				'freemius'       => [
					'is_premium'           => hulk_woo_pcm_fs()->is_premium(),
					'is_not_paying'        => hulk_woo_pcm_fs()->is_not_paying(),
					'checkout_url'         => hulk_woo_pcm_fs()->checkout_url(),
					'get_upgrade_url'      => hulk_woo_pcm_fs()->get_upgrade_url(),
					'can_use_premium_code' => hulk_woo_pcm_fs()->can_use_premium_code(),
				],
			]
		);
	}

	public function after_products_manage_meta_save( $product_id, $form_data ): void {

		$data = [
			'hulk_woo_pcm_productCondition'            => isset( $form_data['hulk_woo_pcm_productCondition'] ) ? sanitize_text_field( wp_unslash( $form_data['hulk_woo_pcm_productCondition'] ) ) : null,
			'hulk_woo_pcm_productConditionCustomGroup' => isset( $form_data['hulk_woo_pcm_productConditionCustomGroup'] ) ? sanitize_text_field( wp_unslash( $form_data['hulk_woo_pcm_productConditionCustomGroup'] ) ) : null,
			'hulk_woo_pcm_productConditionCustom'      => isset( $form_data['hulk_woo_pcm_productConditionCustom'] ) ? sanitize_text_field( wp_unslash( $form_data['hulk_woo_pcm_productConditionCustom'] ) ) : null,
			'hulk_woo_pcm_productConditionLabel'       => isset( $form_data['hulk_woo_pcm_productConditionLabel'] ) ? sanitize_text_field( wp_unslash( $form_data['hulk_woo_pcm_productConditionLabel'] ) ) : null,
			'hulk_woo_pcm_productConditionShopLabel'   => isset( $form_data['hulk_woo_pcm_productConditionShopLabel'] ) ? sanitize_text_field( wp_unslash( $form_data['hulk_woo_pcm_productConditionShopLabel'] ) ) : null,
		];

		$this->model->updateAllMeta( $product_id, $data );
	}
}
