package sdlui

import (
	"image"
	"image/color"

	"github.com/veandco/go-sdl2/sdl"
	"opslag.de/schobers/geom"
	"opslag.de/schobers/zntg/ui"
)

type sdlTexture interface {
	Native() *sdl.Texture
	SetColor(color.Color)
	Size() (int32, int32, error)
}

type Texture struct {
	*sdl.Texture
}

var _ ui.Texture = &Texture{}

func (t *Texture) Height() float32 {
	_, _, _, height, err := t.Texture.Query()
	if err != nil {
		return geom.NaN32()
	}
	return float32(height)
}

func (t *Texture) Native() *sdl.Texture { return t.Texture }

func (t *Texture) SetColor(c color.Color) {
	color := ColorSDL(c)
	t.SetColorMod(color.R, color.G, color.B)
}

func (t *Texture) Size() (int32, int32, error) {
	_, _, width, height, err := t.Texture.Query()
	if err != nil {
		return 0, 0, err
	}
	return width, height, err
}

func (t *Texture) Width() float32 {
	_, _, width, _, err := t.Texture.Query()
	if err != nil {
		return geom.NaN32()
	}
	return float32(width)
}

var _ ui.ImageSource = &TextureImageSource{}

type TextureImageSource struct {
	*Texture

	source ui.ImageSource
}

func (s TextureImageSource) CreateImage() (image.Image, error) {
	return s.source.CreateImage()
}