package ui

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"opslag.de/schobers/geom"
)

var _ Control = &Mock{}

type Mock struct {
	ControlBase

	Size *geom.PointF32
}

func (m *Mock) DesiredSize(ctx Context, size geom.PointF32) geom.PointF32 {
	if m.Size != nil {
		return *m.Size
	}
	return m.ControlBase.DesiredSize(ctx, size)
}

func TestNoStretchFillsAvailableSpace(t *testing.T) {
	m := &Mock{}
	s := BuildSpacing(m, func(s *Spacing) {
		s.SetSize(0, 0)
	})
	s.Arrange(nil, geom.RectF32(31, 37, 73, 79), geom.ZeroPtF32, nil)
	assert.Equal(t, geom.RectF32(31, 37, 73, 79), m.bounds)
}

func TestStretch(t *testing.T) {
	m := &Mock{}
	s := BuildSpacing(m, func(s *Spacing) {
		s.SetSize(geom.NaN32(), geom.NaN32())
	})
	s.Arrange(nil, geom.RectF32(31, 37, 73, 79), geom.ZeroPtF32, nil)
	assert.Equal(t, geom.RectF32(31, 37, 73, 79), m.bounds)
}

func TestCenter(t *testing.T) {
	m := &Mock{Size: &geom.PointF32{X: 2, Y: 3}}
	s := BuildSpacing(m, func(s *Spacing) {
		s.Center()
	})
	s.Arrange(nil, geom.RectF32(31, 37, 73, 79), geom.ZeroPtF32, nil)
	assert.Equal(t, geom.RectF32(51, 56.5, 53, 59.5), m.bounds)
}

func TestFixedMargin(t *testing.T) {
	m := &Mock{Size: &geom.PointF32{X: geom.NaN32(), Y: geom.NaN32()}}
	s := BuildSpacing(m, func(s *Spacing) {
		s.Margin.Left = Fixed(7)
		s.Margin.Top = Fixed(2)
		s.Margin.Right = Fixed(3)
		s.Margin.Bottom = Fixed(5)
	})
	s.Arrange(nil, geom.RectF32(31, 37, 73, 79), geom.ZeroPtF32, nil)
	assert.Equal(t, geom.RectF32(38, 39, 70, 74), m.bounds)
}

func TestRightAlign(t *testing.T) {
	m := &Mock{Size: &geom.PointF32{X: 2, Y: 3}}
	s := Margins(m, geom.NaN32(), 0, 0, 0)
	s.Arrange(nil, geom.RectF32(31, 37, 73, 79), geom.ZeroPtF32, nil)
	assert.Equal(t, geom.RectF32(71, 37, 73, 79), m.bounds)
}

func TestRightAlignFixedWidth(t *testing.T) {
	m := &Mock{Size: &geom.PointF32{X: 2, Y: 3}}
	s := BuildSpacing(m, func(s *Spacing) {
		s.Margin.Left = Infinite()
		s.Width = Fixed(5)
	})
	s.Arrange(nil, geom.RectF32(31, 37, 73, 79), geom.ZeroPtF32, nil)
	assert.Equal(t, geom.RectF32(68, 37, 73, 79), m.bounds)
}