package door

import (
	"fmt"
	"strings"
	"testing"
)

func TestPanel(t *testing.T) {
	var p Panel = Panel{X: 5, Y: 7, Width: 10, Style: DOUBLE, Title: "Test"}
	p.Lines = append(p.Lines, Line{Text: "1234567890"})
	p.Lines = append(p.Lines, Line{Text: "abcdefghij"})

	var expected string = Goto(5, 7) + "╔Test══════╗" + Goto(5, 8) + "║1234567890║" + Goto(5, 9) + "║abcdefghij║" + Goto(5, 10) + "╚══════════╝"
	var got string = p.Output()

	var es []string = strings.SplitAfter(expected, "5H")
	var gs []string = strings.SplitAfter(got, "5H")

	for idx, exp := range es {
		var g string = gs[idx]
		if g != exp {
			t.Errorf("Panel expected %#v, got %#v", exp, g)
		}
	}

	p.TitleOffset = 3
	got = p.Output()
	gs = strings.SplitAfter(got, "5H")

	es[0] = "╔═══Test═══╗" + Goto(5, 8)
	if gs[1] != es[0] {
		t.Errorf("Panel TitleOffset=3 expected %#v, got %#v", es[0], gs[1])
	}
}

func TestPanelSpacer(t *testing.T) {
	var p Panel = Panel{X: 1, Y: 1, Width: 5, Style: SINGLE}
	p.Lines = append(p.Lines, p.Spacer())
	var expected string = Goto(1, 1) + "┌─────┐" + Goto(1, 2) + "├─────┤" + Goto(1, 3) + "└─────┘"
	var got string = p.Output()
	if expected != got {
		t.Errorf("Panel Spacer expected %#v, got %#v", expected, got)
	}
}

func TestPanelUpdate(t *testing.T) {
	var p Panel = Panel{X: 2, Y: 2, Width: 3, Style: DOUBLE_SINGLE}
	var x int = 0
	var updater func() string = func() string {
		return fmt.Sprintf("%3d", x)
	}
	var l Line = Line{UpdateF: updater}
	l.Update()
	p.Lines = append(p.Lines, l)

	var expected string = Goto(2, 2) + "╒═══╕" + Goto(2, 3) + "│  0│" + Goto(2, 4) + "╘═══╛"
	var got string = p.Output()

	if expected != got {
		t.Errorf("Panel Update expected %#v, got %#v", expected, got)
	}

	x += 3
	got = p.Update()
	expected = Goto(3, 3) + "  3"

	if expected != got {
		t.Errorf("Panel Update expected %#v, got %#v", expected, got)
	}

	got = p.Update()

	if got != "" {
		t.Errorf("Panel Update expected '', got %#v", got)
	}

	got = p.UpdateLine(0)

	if expected != got {
		t.Errorf("Panel UpdateLine expected %#v, got %#v", expected, got)
	}

	got = p.GotoEnd()
	expected = Goto(7, 4)
	if got != expected {
		t.Errorf("Panel GotoEnd expected %#v, got %#v", expected, got)
	}
}