<?php

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

use Exception;
use WC_Product;
use WC_Product_Attribute;
use WC_Product_Variation;
use WC_Product_Factory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DataProvider\AttributeCeneoDataProvider;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldValueModificator;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DataProvider\DropshippingDataProvider;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\ProductFieldsValueFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\AttributesCeneoFormFields;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldArrayValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldStringValue;
class CeneoAttributesProductFieldModificator implements ProductFieldValueModificator, ProductFieldArrayValue
{
    private AttributeCeneoDataProvider $settigs_data_provider;
    private WC_Product $product;
    private ProductFieldsValueFactory $product_fields_value_factory;
    public function __construct(AttributeCeneoDataProvider $settings_data_provider, ProductFieldsValueFactory $product_fields_value_factory)
    {
        $this->settigs_data_provider = $settings_data_provider;
        $this->product_fields_value_factory = $product_fields_value_factory;
    }
    public function set_product(WC_Product $product): void
    {
        $this->product = $product;
    }
    public function has_array_value(): bool
    {
        return \is_array($this->get_array_value());
    }
    public function get_array_value(): ?array
    {
        $result = [];
        try {
            $all_data = $this->settigs_data_provider->get_all();
            $categories = $this->get_product_categories_ids();
            foreach ($all_data as $key => $value) {
                if (strpos($key, AttributesCeneoFormFields::FIELD_CATEGORIES_COMPONENT) === 0) {
                    $cat_id = \str_replace(AttributesCeneoFormFields::FIELD_CATEGORIES_COMPONENT, '', $key);
                    if (\in_array($cat_id, $categories)) {
                        //phpcs:ignore
                        $result = array_merge($result, $this->get_attributes($value));
                    }
                } elseif (strpos($key, AttributesCeneoFormFields::FIELD_INTEGRATION_COMPONENT) === 0) {
                    $result = array_merge($result, $this->get_attributes($value));
                }
            }
            return $result;
        } catch (Exception $e) {
            return null;
        }
    }
    private function get_product_categories_ids(): array
    {
        $result = [];
        $is_variation = $this->product instanceof WC_Product_Variation;
        if ($is_variation) {
            $categories = get_the_terms($this->product->get_parent_id(), 'product_cat');
        } else {
            $categories = get_the_terms($this->product->get_id(), 'product_cat');
        }
        foreach ($categories as $category) {
            $result[] = $category->term_id;
        }
        return $result;
    }
    private function get_attributes(array $attributes): array
    {
        $result = [];
        foreach ($attributes as $key => $value) {
            if (empty($value) && !\is_numeric($value)) {
                continue;
            }
            $value = $this->product_fields_value_factory->get($this->product, $value);
            if ($value instanceof ProductFieldStringValue) {
                $string_value = $value->get_string_value();
                if (empty($string_value) && !\is_numeric($string_value)) {
                    continue;
                }
                $result[$key] = $string_value;
            }
        }
        return $result;
    }
}
