package door

import (
	"fmt"
	"testing"
)

func TestLine(t *testing.T) {
	var line Line = Line{Text: "Test Me"}
	var output string = line.Output()
	var expect string = "Test Me"

	if output != expect {
		t.Errorf("Line: Expected %#v, got %#v", expect, output)
	}

	if line.Update() {
		t.Error("Line: No updater, should return false")
	}

	line.DefaultColor = Color(0)
	line.Width = 8
	output = line.Output()
	expect = "\x1b[0mTest Me "

	if output != expect {
		t.Errorf("Line: Expected %#v, got %#v", expect, output)
	}

	// leave the default color, it is ignored when there's a render function
	// line.DefaultColor = ""
	line.RenderF = RenderBlueYellow
	output = line.Output()
	var blue string = ColorText("BOLD BLUE")
	var yellow string = ColorText("BOLD YELLOW")

	expect = blue + "T" + yellow + "est " + blue + "M" + yellow + "e "
	if output != expect {
		t.Errorf("Line: Expected %#v, got %#v", expect, output)
	}

}

func TestLineUpdate(t *testing.T) {
	var counter int = 0
	uf := func() string {
		return fmt.Sprintf("Count: %d", counter)
	}
	var line Line = Line{Text: "", UpdateF: uf}
	line.Update()
	var output string = line.Output()
	var expect string = "Count: 0"

	if output != expect {
		t.Errorf("LineUpdate: Expected %#v, got %#v", expect, output)
	}

	if line.Update() {
		t.Error("Unexpected Update: should have returned false. (no change)")
	}

	counter++

	if !line.Update() {
		t.Error("Missing Update: value was changed, Text should have changed")
	}

	output = line.Output()
	expect = "Count: 1"
	if output != expect {
		t.Errorf("LineUpdate: Expected %#v, got %#v", expect, output)
	}
}

func TestLineUnicode(t *testing.T) {
	Unicode = true
	var line Line = Line{Text: "Howdy \U0001f920"}
	var output string = line.Output()
	var expect string = "Howdy 🤠"

	if output != expect {
		t.Errorf("LineUnicode: Expected %#v, got %#v", expect, output)
	}
}

func TestLineCP437(t *testing.T) {
	Unicode = false
	var tests []string = []string{"\xdb TEXT \xdb",
		"\xf1 F1", "\xf2 F2", "\xf3 F3"}
	for _, test := range tests {
		var line Line = Line{Text: test}
		var output string = line.Output()
		var expect string = test

		if output != expect {
			t.Errorf("LineCP437: Expected %#v, got %#v", expect, output)
		}
	}
}