<?php

namespace Doom\Image;

use Doom\Picture\DoomPalette;
use Doom\Picture\Palette;
use Intervention\Image\Drivers\Gd\Driver as GD;
use Intervention\Image\ImageManager;
use Intervention\Image\Interfaces\ImageInterface;

final class Renderer
{
    private ImageManager $manager;

    private Palette $palette;

    public function __construct(Palette|array|null $palette = null)
    {
        if (is_array($palette)) {
            $palette = new Palette($palette);
        }

        $this->palette = $palette ?? DoomPalette::default();
        $this->manager = new ImageManager(new GD());
    }

    public function create(int $width, int $height): ImageInterface
    {
        $image = $this->manager->createImage($width, $height);
        $image->fill('rgba(0, 0, 0, 0)');
        return $image;
    }

    public function createPicture(): PictureRenderer
    {
        return new PictureRenderer($this->manager, $this->palette);
    }

    public function createTexture(int $width, int $height): TextureRenderer
    {
        return new TextureRenderer($this, $this->createPicture(), $width, $height);
    }
}
