<?php

namespace OtomaticAi\Content\Image;

use DOMDocument;
use DOMElement;
use OtomaticAi\Utils\Image as UtilsImage;
use OtomaticAi\Utils\Support;
use OtomaticAi\Vendors\Illuminate\Support\Arr;

class Pexels extends Image
{
    const PROVIDER = "pexels";

    public ?string $search;

    /**
     * @param string $src
     * @param string $title
     * @param string $description
     * @param string $legend
     * @param string $search
     * @param string $uid
     * @return self
     */
    static public function make(string $src, string $title = null, string $description = null, string $legend = null, string $search = null, string $uid = null): self
    {
        $image = new self(UtilsImage::fromUrl($src), $title, $description, $legend, $uid);

        $image->search = $search;

        return $image;
    }

    /**
     * @return DOMElement|null
     */
    public function toHtmlElement(DOMDocument $document): ?DOMElement
    {
        if (($attachmentId = $this->image->save($this->title, $this->description, $this->legend)) !== null) {
            $el = $document->createElement("otoimage");
            $el->setAttribute("attachment", $attachmentId);
            $el->setAttribute("provider", self::PROVIDER);
            $el->setAttribute("search", mb_convert_encoding(Support::removeEmojis($this->search), "UTF-8"));
            $el->setAttribute("uid", $this->uid);

            return $el;
        }

        return null;
    }

    /**
     * @return array|null
     */
    public function toArray(): ?array
    {
        if (($attachmentId = $this->image->save($this->title, $this->description, $this->legend)) !== null) {
            return [
                "attachment" => $attachmentId,
                "provider" => self::PROVIDER,
                "search" => mb_convert_encoding(Support::removeEmojis($this->search), "UTF-8"),
                "uid" => $this->uid,
            ];
        }

        return null;
    }

    /**
     * @return self|null
     */
    static public function fromHtmlElement(DOMElement $node): ?self
    {
        $attachmentId = $node->getAttribute("attachment");

        $attachment = UtilsImage::fromAttachmentId($attachmentId);

        if ($attachment) {
            $image = new self($attachment, get_the_title($attachmentId), get_post_field("post_content", $attachmentId), get_post_field("post_excerpt", $attachmentId));
            $image->search = $node->getAttribute("search");
            $image->uid = $node->getAttribute("uid");

            return $image;
        }

        return null;
    }

    /**
     * @return self|null
     */
    static public function fromArray(array $data): ?self
    {
        $attachmentId = $data["attachment"];

        $attachment = UtilsImage::fromAttachmentId($attachmentId);

        if ($attachment) {
            $image = new self($attachment, get_the_title($attachmentId), get_post_field("post_content", $attachmentId), get_post_field("post_excerpt", $attachmentId));
            $image->search = $data["search"];
            $image->uid = Arr::get($data, "uid");

            return $image;
        }

        return null;
    }
}
