package tui

import "fmt"

// Color holds the numeric part of an ANSI sequence (e.g. 31, 32, 313, ...)
type Color struct{ code int }

func NewColor(code int) Color { return Color{code} }

// Text returns the basic foreground sequence, e.g. "\033[31m"
func (c Color) Text() string { return fmt.Sprintf("\033[%dm", c.code) }

// Underline returns the sequence with underline: "\033[31;4m"
func (c Color) Underline() string { return fmt.Sprintf("\033[%d;4m", c.code) }

// Blink returns the sequence with blink: "\033[31;5m" (kept for completeness)
func (c Color) Blink() string { return fmt.Sprintf("\033[%d;5m", c.code) }

// Inverse returns the sequence with inverse/swap: "\033[31;7m"
// (this matches your original usage of ;7m as "background"/inverse)
func (c Color) Inverse() string { return fmt.Sprintf("\033[%d;7m", c.code) }

// Bg returns a real background color sequence (code + 10 -> 40..47 etc).
// e.g. if code==31 -> Bg() -> "\033[41m"
func (c Color) Bg() string { return fmt.Sprintf("\033[%dm", c.code+10) }

// Attr lets you compose any extra attribute numbers (e.g. 1 for bold)
func (c Color) Attr(attrs ...int) string {
	if len(attrs) == 0 {
		return c.Text()
	}
	s := fmt.Sprintf("\033[%d", c.code)
	for _, a := range attrs {
		s += fmt.Sprintf(";%d", a)
	}
	s += "m"
	return s
}

// String implements fmt.Stringer (defaults to Text())
func (c Color) String() string { return c.Text() }

type Palette struct {
	Base00 Color
	Base01 Color
	Base02 Color
	Base03 Color
	Base04 Color
	Base05 Color
	Base06 Color
	Base07 Color
	Base08 Color
	Base09 Color
	Base10 Color
	Base11 Color
	Base12 Color
	Base13 Color
	Base14 Color
	Base15 Color
	Reset string // usually reset
}

// Helper methods on the palette for convenience:
func (p Palette) Background(c Color) string { return c.Inverse() }   // keeps your current ;7m semantics
func (p Palette) Underlined(c Color) string { return c.Underline() } // convenience
func (p Palette) Text(c Color) string       { return c.Text() }

var Colors = Palette{
	Base00: NewColor(31),
	Base01: NewColor(32),
	Base02: NewColor(33),
	Base03: NewColor(34),
	Base04: NewColor(35),
	Base05: NewColor(36),
	Base06: NewColor(37),
	Base07: NewColor(38),
	Base08: NewColor(39),
	Base09: NewColor(310),
	Base10: NewColor(311),
	Base11: NewColor(312),
	Base12: NewColor(313),
	Base13: NewColor(314),
	Base14: NewColor(315),
	Base15: NewColor(316),
	Reset: "\033[0m", // reset
}
