<?php

declare(strict_types=1);

namespace ProPresenter\Parser;

use Rv\Data\MacrosDocument\Macro as MacroProto;

/**
 * Wraps a protobuf Macro, exposing its identifying fields (UUID, name) plus
 * convenience metadata (color, action count, image type, startup flag).
 *
 * Macros live in the global ProPresenter `Macros` document and may belong to
 * one or more {@see MacroCollection}s. Membership is resolved by
 * {@see MacroLibrary}, not by this wrapper.
 */
class Macro
{
    public function __construct(
        private readonly MacroProto $macro,
    ) {
    }

    /**
     * Get the macro's UUID as an upper-case string (empty when unset).
     */
    public function getUuid(): string
    {
        return $this->macro->getUuid()?->getString() ?? '';
    }

    /**
     * Get the macro's display name.
     */
    public function getName(): string
    {
        return $this->macro->getName();
    }

    /**
     * Get the macro's color as an associative array, or null when unset.
     *
     * @return array{r: float, g: float, b: float, a: float}|null
     */
    public function getColor(): ?array
    {
        if (!$this->macro->hasColor()) {
            return null;
        }

        $color = $this->macro->getColor();

        return [
            'r' => $color->getRed(),
            'g' => $color->getGreen(),
            'b' => $color->getBlue(),
            'a' => $color->getAlpha(),
        ];
    }

    /**
     * Whether the macro is configured to fire on application startup.
     */
    public function getTriggerOnStartup(): bool
    {
        return $this->macro->getTriggerOnStartup();
    }

    /**
     * Number of action entries attached to this macro.
     *
     * Action payloads are not exposed by this wrapper; use {@see getProto()} to
     * inspect them via the generated protobuf classes.
     */
    public function getActionCount(): int
    {
        return count($this->macro->getActions());
    }

    /**
     * Icon enum value (see {@see \Rv\Data\MacrosDocument\Macro\ImageType}).
     */
    public function getImageType(): int
    {
        return $this->macro->getImageType();
    }

    /**
     * Get the underlying protobuf Macro message.
     */
    public function getProto(): MacroProto
    {
        return $this->macro;
    }
}
