<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\Categories;

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Config\Config;
use SimpleXMLElement;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\PriceCompareAttribute;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\PriceCompareCategory;
class CeneoCategories implements PriceCompareCategory, PriceCompareAttribute
{
    public const SEPARATOR = '/';
    private Config $config;
    private SimpleXMLElement $xml_categories;
    public function __construct(Config $config)
    {
        $this->config = $config;
    }
    public function get_category_full_name(int $category_id): string
    {
        $this->load_array_of_category();
        $categories = $this->xml_categories->xpath("//Id[.={$category_id}]/parent::*");
        $c_name = [];
        while (!empty($categories[0])) {
            if (!empty($categories[0]->Name)) {
                $c_name[] = (string) $categories[0]->Name;
            }
            $categories = $categories[0]->xpath('parent::*');
        }
        if (!empty($c_name)) {
            return implode(self::SEPARATOR, array_reverse($c_name));
        } else {
            return '';
        }
    }
    public function get_category_attributes(int $category_id): array
    {
        $this->load_array_of_category();
        $attributes = $this->xml_categories->xpath("//Id[.={$category_id}]/parent::*/Subcategories/Attributes/Attribute");
        $result = is_array($attributes) ? $attributes : [];
        $result = array_map(fn($val) => (array) $val, $result);
        return $result;
    }
    public function get_categories(): array
    {
        $this->load_array_of_category();
        $options = $this->get_categories_recursive($this->xml_categories);
        return $options;
    }
    private function get_categories_recursive(SimpleXMLElement $xml_root, array $parent_name_array = []): array
    {
        $options = [];
        foreach ($xml_root->children() as $xml_category) {
            if (empty($xml_category->Id)) {
                continue;
            }
            $options[(string) $xml_category->Id] = (string) $xml_category->Name;
            if (!empty($xml_category->Subcategories)) {
                $options[(string) $xml_category->Id] = implode(self::SEPARATOR, \array_merge($parent_name_array, [(string) $xml_category->Name]));
                $options = $options + $this->get_categories_recursive($xml_category->Subcategories, \array_merge($parent_name_array, [(string) $xml_category->Name]));
            }
        }
        return $options;
    }
    public function find_category_by_name(string $term): array
    {
        $result = [];
        $categories_options = $this->get_categories();
        foreach ($categories_options as $key => $value) {
            if (stripos($value, $term) !== \false) {
                $result[] = ['value' => $key, 'text' => str_replace('-', self::SEPARATOR, $categories_options[$key])];
            }
        }
        return $result;
    }
    private function load_array_of_category(): void
    {
        if (empty($this->xml_categories)) {
            $data = file_get_contents(str_replace(['/class', '\class'], '', $this->config->get_param('assets.data.ceneo')->getAsString()));
            $this->xml_categories = new \SimpleXMLElement(trim($data));
        }
    }
}
