<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\ProductFields\Ceneo;

use DOMElement;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\ProductFields\CustomProductField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductField;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\Value\ImageProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldValue;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldXml;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductFields\ProductFieldImgLocationValue;
class ImagesProductField extends CustomProductField implements ProductField, ProductFieldXml, ProductFieldImgLocationValue
{
    public const SINGLE_TAG_NAME = 'main';
    public const SINGLE_TAG_GALLERY_NAME = 'i';
    public const SINGLE_TAG_ATTR_NAME = 'url';
    public const TAG_NAME = 'imgs';
    private $field_name;
    const SEPARATOR = ',';
    private ProductFieldValue $product_value;
    public function __construct(ProductFieldValue $product_value)
    {
        $this->product_value = $product_value;
    }
    public function map_xml_value(DOMElement $element): DOMElement
    {
        $root = $element->ownerDocument;
        $images = $this->get_values_as_array($this->product_value);
        if (\is_array($images) && !empty($images)) {
            $imgs = $element->appendChild($root->createElement(self::TAG_NAME));
            foreach ($images as $index => $image_url) {
                if (!\is_string($image_url) || empty($image_url)) {
                    continue;
                }
                // Pierwszy obrazek jest w tagu "main", pozostałe w tagu "i"
                $tag_name = $index === 0 ? self::SINGLE_TAG_NAME : self::SINGLE_TAG_GALLERY_NAME;
                $image_element = $imgs->appendChild($root->createElement($tag_name));
                $image_element->setAttribute(self::SINGLE_TAG_ATTR_NAME, $image_url);
            }
        }
        return $element;
    }
    public static function get_default_class_value(): string
    {
        return ImageProductFieldValue::class;
    }
    public static function is_allowed_to_change(): bool
    {
        return \true;
    }
    public static function get_default_field_name(): string
    {
        return 'ProductImages';
    }
    public static function get_default_field_nice_name(): string
    {
        return sprintf(esc_html__('Product Images %s', 'dropshipping-export-free'), '(' . self::TAG_NAME . ')');
    }
    public function get_field_name(): string
    {
        $default = self::get_default_field_name();
        return !empty($this->field_name) ? $this->field_name : $default;
    }
    public function set_field_name(string $string)
    {
        $this->field_name = trim($string);
    }
    public function get_file_locations(): ?array
    {
        if ($this->product_value instanceof ProductFieldImgLocationValue) {
            return $this->product_value->get_file_locations();
        }
        return null;
    }
}
