<?php

declare(strict_types=1);

namespace App\Framework\Svg\ValueObjects\Styling;

use App\Domain\Common\ValueObject\RGBColor;

/**
 * Value Object representing color in SVG context
 * Supports RGB, hex, and named colors
 */
final readonly class SvgColor
{
    private function __construct(
        public RGBColor $rgbColor,
        public ?NamedColor $namedColor = null
    ) {
    }

    public static function fromRgb(int $r, int $g, int $b): self
    {
        return new self(RGBColor::fromRgb($r, $g, $b));
    }

    public static function fromRgbColor(RGBColor $color): self
    {
        return new self($color);
    }

    public static function fromHex(string $hex): self
    {
        return new self(RGBColor::fromHex($hex));
    }

    public static function fromNamed(NamedColor $color): self
    {
        return new self($color->toRgb(), $color);
    }

    /**
     * Common SVG colors as static factory methods
     */
    public static function black(): self
    {
        return self::fromNamed(NamedColor::BLACK);
    }

    public static function white(): self
    {
        return self::fromNamed(NamedColor::WHITE);
    }

    public static function transparent(): self
    {
        return self::fromNamed(NamedColor::TRANSPARENT);
    }

    public static function red(): self
    {
        return self::fromNamed(NamedColor::RED);
    }

    public static function green(): self
    {
        return self::fromNamed(NamedColor::GREEN);
    }

    public static function blue(): self
    {
        return self::fromNamed(NamedColor::BLUE);
    }

    public static function yellow(): self
    {
        return self::fromNamed(NamedColor::YELLOW);
    }

    public static function orange(): self
    {
        return self::fromNamed(NamedColor::ORANGE);
    }

    public static function gray(): self
    {
        return self::fromNamed(NamedColor::GRAY);
    }

    /**
     * Convert to SVG color attribute value
     * Prefers named color if available, falls back to hex
     */
    public function toSvgValue(): string
    {
        return $this->namedColor?->toSvgValue() ?? $this->rgbColor->toHex();
    }

    /**
     * Always returns hex representation
     */
    public function toHex(): string
    {
        return $this->rgbColor->toHex();
    }

    /**
     * Returns RGB string representation
     */
    public function toRgbString(): string
    {
        return sprintf(
            'rgb(%d, %d, %d)',
            $this->rgbColor->red,
            $this->rgbColor->green,
            $this->rgbColor->blue
        );
    }

    /**
     * Check if this is a transparent color
     */
    public function isTransparent(): bool
    {
        return $this->namedColor?->isTransparent() ?? false;
    }

    public function toArray(): array
    {
        return [
            'rgb' => [
                'r' => $this->rgbColor->red,
                'g' => $this->rgbColor->green,
                'b' => $this->rgbColor->blue,
            ],
            'hex' => $this->rgbColor->toHex(),
            'named' => $this->namedColor?->value,
        ];
    }

    public function __toString(): string
    {
        return $this->toSvgValue();
    }
}
