package door

import "strings"

type BoxStyle struct {
	Top_Left     string
	Top_Right    string
	Top          string
	Side         string
	Bottom_Left  string
	Bottom_Right string
	Middle_Left  string
	Middle_Right string
}

// FUTURE:
// Once we detect CP437 or Unicode, set BOXES to point to
// BOXES_CP437 or BOXES_UNICODE.  That'll save us from having
// to if Unicode when building boxes.  ;)

var BOXES [4]BoxStyle

var BOXES_CP437 = [4]BoxStyle{
	/*
		┌──┐
		│  │
		├──┤
		└──┘
	*/
	BoxStyle{"\xda", "\xbf", "\xc4", "\xb3", "\xc0", "\xd9", "\xc3", "\xb4"},
	/*
		╔══╗
		║  ║
		╠══╣
		╚══╝
	*/
	BoxStyle{"\xc9", "\xbb", "\xcd", "\xba", "\xc8", "\xbc", "\xcc", "\xb9"},
	/*
		╒══╕
		│  │
		╞══╡
		╘══╛
	*/
	BoxStyle{"\xd5", "\xb8", "\xcd", "\xb3", "\xd4", "\xbe", "\xc6", "\xb5"},
	/*
		╓──╖
		║  ║
		╟──╢
		╙──╜
	*/
	BoxStyle{"\xd6", "\xb7", "\xc4", "\xba", "\xd3", "\xbd", "\xc7", "\xb6"},
}

var BOXES_UNICODE = [4]BoxStyle{
	BoxStyle{"\u250c", "\u2510", "\u2500", "\u2502", "\u2514", "\u2518", "\u251c", "\u2524"},
	BoxStyle{"\u2554", "\u2557", "\u2550", "\u2551", "\u255a", "\u255d", "\u2560", "\u2563"},
	BoxStyle{"\u2552", "\u2555", "\u2550", "\u2502", "\u2558", "\u255b", "\u255e", "\u2561"},
	BoxStyle{"\u2553", "\u2556", "\u2500", "\u2551", "\u2559", "\u255c", "\u255f", "\u2562"},
}

type Box struct {
	Width int
	Style int
}

func (b *Box) Top() string {
	var style *BoxStyle = &BOXES[b.Style]
	return style.Top_Left + strings.Repeat(style.Top, b.Width) + style.Top_Right
}

func (b *Box) Row(text string) string {
	var style *BoxStyle = &BOXES[b.Style]
	return style.Side + text + style.Side
}

func (b *Box) Middle() string {
	var style *BoxStyle = &BOXES[b.Style]
	return style.Middle_Left + strings.Repeat(style.Top, b.Width) + style.Middle_Right
}

func (b *Box) Bottom() string {
	var style *BoxStyle = &BOXES[b.Style]
	return style.Bottom_Left + strings.Repeat(style.Top, b.Width) + style.Bottom_Right
}

func AlertBox(text string, style int) []string {
	var results []string
	b := Box{Width: StringLen([]byte(text)), Style: style}
	results = append(results, b.Top())
	results = append(results, b.Row(text))
	results = append(results, b.Bottom())
	return results
}