<?php

namespace OtomaticAi\Content\Video;

use DOMDocument;
use DOMElement;
use OtomaticAi\Content\Contracts\ShouldDisplay;
use OtomaticAi\Utils\Support;

class Youtube implements ShouldDisplay
{
    public string $id;
    public string $title;
    public string $description;

    public function __construct(string $id, string $title = "", string $description = "")
    {
        $this->id = $id;
        $this->title = $title;
        $this->description = $description;
    }

    public function toHtmlElement(DOMDocument $document): ?DOMElement
    {
        $el = $document->createElement("otoyoutube");
        $el->setAttribute("videoid", $this->id);
        $el->setAttribute("title", mb_convert_encoding(Support::removeEmojis($this->title), "UTF-8"));
        $el->setAttribute("description", mb_convert_encoding(Support::removeEmojis($this->description), "UTF-8"));
        $el->setAttribute("uid", uniqid("youtube-"));

        return $el;
    }

    public function toDisplayElement(DOMElement $node): ?DOMElement
    {
        return $this->toHtmlElement($node->ownerDocument);
    }

    static public function fromHtmlElement(DOMElement $node)
    {
        $id = $node->getAttribute("videoid");

        return new self($id, $node->getAttribute("title"), $node->getAttribute("description"));
    }
}
