package display

import (
	"image"
	"image/draw"

	"git.dvdt.dev/david/bitcoin-ticker-pi/fonts"
	rgbmatrix "git.dvdt.dev/david/bitcoin-ticker-pi/go-rpi-rgb-led-matrix"
)

type Display struct {
	canvas *rgbmatrix.Canvas

	topleft, topright, main, bottom *Slot
}

func New(canvas *rgbmatrix.Canvas, topleft, topright, main, bottom *Slot) *Display {
	return &Display{
		canvas:   canvas,
		topleft:  topleft,
		topright: topright,
		main:     main,
		bottom:   bottom,
	}
}

func (d *Display) UpdateTopLeft(text []Segment) error {
	err := d.topleft.Update(text)
	if err != nil {
		return err
	}
	d.topleft.Draw(d.canvas)
	return nil
}

func (d *Display) UpdateTopRight(text []Segment) error {
	err := d.topright.Update(text)
	if err != nil {
		return err
	}
	d.topright.Draw(d.canvas)
	return nil
}

func (d *Display) UpdateMain(text []Segment) error {
	err := d.main.Update(text)
	if err != nil {
		return err
	}
	d.main.Draw(d.canvas)
	return nil
}

func (d *Display) UpdateBottom(text []Segment) error {
	err := d.bottom.Update(text)
	if err != nil {
		return err
	}
	d.bottom.Draw(d.canvas)
	return nil
}

func (d *Display) Render() error {
	return d.canvas.Render()
}

func (d *Display) Clear() {
	draw.Draw(d.canvas, d.canvas.Bounds(), &image.Uniform{fonts.Black}, image.ZP, draw.Src)
}
