<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value\Modificator;

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldValueModificator;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DataProvider\DropshippingDataProvider;
use WC_Product;
use WC_Product_Variation;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\DataProviderFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldStringValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\PriceCompareConfig;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\PriceCompareCategoryConfig;
class CategoryProductFieldModificator implements ProductFieldValueModificator, ProductFieldStringValue
{
    public const CATEGORY_STRING_LIMIT = 255;
    private PriceCompareConfig $price_compare_config;
    private DataProviderFactory $dp_factory;
    private WC_Product $product;
    public function __construct(PriceCompareConfig $price_compare_config, DataProviderFactory $dp_factory)
    {
        $this->price_compare_config = $price_compare_config;
        $this->dp_factory = $dp_factory;
    }
    public function has_string_value(): bool
    {
        return \is_string($this->get_string_value());
    }
    public function get_string_value(): ?string
    {
        $string = '';
        $tmp = '';
        if (!$this->price_compare_config instanceof PriceCompareCategoryConfig) {
            return null;
        }
        $settings_data_provider = $this->dp_factory->create_by_class_name($this->price_compare_config->get_category_data_provider_class());
        $categories = $this->get_product_categories($this->product);
        $xml_category_id = -1;
        if (!empty($categories)) {
            foreach ($categories as $category) {
                if (!$settings_data_provider->has($category->term_id)) {
                    continue;
                }
                $new_xml_category_id = $settings_data_provider->get($category->term_id);
                if ($new_xml_category_id > $xml_category_id) {
                    $xml_category_id = $new_xml_category_id;
                    break;
                }
            }
        }
        if ($xml_category_id > -1 && $xml_category_id !== '' && $xml_category_id !== null) {
            return $this->price_compare_config->get_category()->get_category_full_name($xml_category_id);
        }
        if (!empty($categories)) {
            foreach ($categories as $cat) {
                $tmp .= empty($string) ? $cat->name : ' - ' . $cat->name;
                if (strlen($tmp) >= self::CATEGORY_STRING_LIMIT) {
                    break;
                } else {
                    $string = $tmp;
                }
            }
            return $string;
        }
        return null;
    }
    private function get_product_categories(WC_Product $product): array
    {
        $is_variation = $product instanceof WC_Product_Variation;
        if ($is_variation) {
            $categories = get_the_terms($product->get_parent_id(), 'product_cat');
        } else {
            $categories = get_the_terms($product->get_id(), 'product_cat');
        }
        return \is_array($categories) ? $categories : [];
    }
    public function set_product(WC_Product $product): void
    {
        $this->product = $product;
    }
}
