<?php

namespace HulkPlugins\Woo\ProductConditionManager\Services;

use HulkPlugins\Woo\ProductConditionManager\Models\ProductModel;
use HulkPlugins\Woo\ProductConditionManager\Models\SettingsModel;
use HulkPlugins\Woo\ProductConditionManager\Traits\SingletonTrait;
use HulkPlugins\Woo\ProductConditionManager\Utils\Helper;

defined( 'ABSPATH' ) || exit;

class FrontendService {
	use SingletonTrait;

	private array $cache = [];

	private ProductModel $productModel;
	private SettingsModel $settingsModel;

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

	public function isProductConditionEnabled( $post_id ): bool {
		$data = $this->productModel->getMeta( $post_id, 'hulk_woo_pcm_productCondition' );
		return ! empty( $data );
	}

	public function isSettingsConditionEnabled(): bool {
		$data = $this->settingsModel->getSettings();
		return ! empty( $data['productCondition'] );
	}

	/**
	 * Get product condition label
	 *
	 * @param $post_id
	 * @param bool $isShopPage
	 *
	 * @return string
	 */
	public function getProductConditionLabel( $post_id, bool $isShopPage ): string {

		$key = $isShopPage ? 'productConditionShopLabel' : 'productConditionLabel';

		// Check if cached
		if ( isset( $this->cache['getProductConditionLabel'][ $post_id ][ $key ] ) ) {
			return $this->cache['getProductConditionLabel'][ $post_id ][ $key ];
		}

		$defaultLabel = (string) apply_filters(
			'hulk_woo_pcm_label',
			esc_attr__( 'Product Condition:', 'product-condition-manager-for-woocommerce' )
		);

		// Get from product meta
		if ( $this->isProductConditionEnabled( $post_id ) ) {
			$productConditionLabel = (string) $this->productModel->getMeta( $post_id, 'hulk_woo_pcm_' . $key );

			if ( ! empty( $productConditionLabel ) ) {
				// Store in cache
				$this->cache['getProductConditionLabel'][ $post_id ][ $key ] = $productConditionLabel;
				return $productConditionLabel;
			}
		}

		// Get from settings
		if ( $this->isSettingsConditionEnabled() ) {
			$settings = $this->settingsModel->getSettings();

			if ( ! empty( $settings[ $key ] ) ) {
				// Store in cache
				$newLabel = (string) $settings[ $key ];
				$this->cache['getProductConditionLabel'][ $post_id ][ $key ] = $newLabel;
				return $newLabel;
			}
		}

		// Store in cache
		$this->cache['getProductConditionLabel'][ $post_id ][ $key ] = $defaultLabel;
		return $defaultLabel;
	}

	/**
	 * Get product condition
	 *
	 * @param $post_id
	 *
	 * @return array
	 */
	public function getProductCondition( $post_id ): array {

		// Check if cached
		if ( isset( $this->cache['getProductCondition'][ $post_id ] ) ) {
			return $this->cache['getProductCondition'][ $post_id ];
		}

		// Get from product meta
		if ( $this->isProductConditionEnabled( $post_id ) ) {
			$productCondition = (string) $this->productModel->getMeta( $post_id, 'hulk_woo_pcm_productCondition' );

			// Get group and product condition name
			$data = Helper::getConditionByKey( $productCondition );

			// Get custom condition
			if ( $productCondition === 'other' ) {
				$data['group'] = (string) $this->productModel->getMeta( $post_id, 'hulk_woo_pcm_productConditionCustomGroup' );
				$data['condition'] = (string) $this->productModel->getMeta( $post_id, 'hulk_woo_pcm_productConditionCustom' );
			}

			// Store in cache
			$this->cache['getProductCondition'][ $post_id ] = $data;
			return $data;
		}

		// Get from settings
		if ( $this->isSettingsConditionEnabled() ) {
			$settings = $this->settingsModel->getSettings();
			$productCondition = (string) $settings['productCondition'];

			// Get group and product condition name
			$data = Helper::getConditionByKey( $productCondition );

			// Get custom condition
			if ( $productCondition === 'other' ) {
				$data['group'] = (string) $settings['productConditionCustomGroup'];
				$data['condition'] = (string) $settings['productConditionCustom'];
			}

			// Store in cache
			$this->cache['getProductCondition'][ $post_id ] = $data;
			return $data;
		}

		return [
			'group'     => '',
			'condition' => '',
		];
	}
}
