package uv

import (
	
	
	

	
	
)

var (
	// ErrNotTerminal is an error that indicates that the file is not a terminal.
	ErrNotTerminal = fmt.Errorf("not a terminal")
	// ErrPlatformNotSupported is an error that indicates that the platform is not supported.
	ErrPlatformNotSupported = fmt.Errorf("platform not supported")
)

// Drawable represents a drawable component on a [Screen].
type Drawable interface {
	// Draw renders the component on the screen for the given area.
	Draw(scr Screen, area Rectangle)
}

// DrawableFunc is a function that implements the [Drawable] interface.
type DrawableFunc func(scr Screen, rect Rectangle)

var _ Drawable = (DrawableFunc)(nil)

// Draw implements the [Drawable] interface.
func ( DrawableFunc) ( Screen,  Rectangle) {
	(, )
}

// WidthMethod determines how many columns a grapheme occupies on the screen.
type WidthMethod interface {
	StringWidth(s string) int
}

// Screen represents a screen that can be drawn to.
type Screen interface {
	// Bounds returns the bounds of the screen. This is the rectangle that
	// includes the start and end points of the screen.
	Bounds() Rectangle

	// CellAt returns the cell at the given position. If the position is out of
	// bounds, it returns nil. Otherwise, it always returns a cell, even if it
	// is empty (i.e., a cell with a space character and a width of 1).
	CellAt(x, y int) *Cell

	// SetCell sets the cell at the given position. A nil cell is treated as an
	// empty cell with a space character and a width of 1.
	SetCell(x, y int, c *Cell)

	// WidthMethod returns the width method used by the screen.
	WidthMethod() WidthMethod
}

// Cursor represents a cursor on the terminal screen.
type Cursor struct {
	// Position is a [Position] that determines the cursor's position on the
	// screen relative to the top left corner of the frame.
	Position

	// Color is a [color.Color] that determines the cursor's color.
	Color color.Color

	// Shape is a [CursorShape] that determines the cursor's shape.
	Shape CursorShape

	// Blink is a boolean that determines whether the cursor should blink.
	Blink bool

	// Hidden is a boolean that determines whether the cursor is hidden. You
	// can use this if you want to hide the cursor but still want to change its
	// position.
	Hidden bool
}

// NewCursor returns a new cursor with the default settings and the given
// position.
func (,  int) *Cursor {
	return &Cursor{
		Position: Position{X: , Y: },
		Color:    nil,
		Shape:    CursorBlock,
		Blink:    true,
	}
}

// ProgressBarState represents the state of the progress bar.
type ProgressBarState int

// Progress bar states.
const (
	ProgressBarNone ProgressBarState = iota
	ProgressBarDefault
	ProgressBarError
	ProgressBarIndeterminate
	ProgressBarWarning
)

// String return a human-readable value for the given [ProgressBarState].
func ( ProgressBarState) () string {
	return [...]string{
		"None",
		"Default",
		"Error",
		"Indeterminate",
		"Warning",
	}[]
}

// ProgressBar represents the terminal progress bar.
//
// Support depends on the terminal.
//
// See https://learn.microsoft.com/en-us/windows/terminal/tutorials/progress-bar-sequences
type ProgressBar struct {
	// State is the current state of the progress bar. It can be one of
	// [ProgressBarNone], [ProgressBarDefault], [ProgressBarError],
	// [ProgressBarIndeterminate], and [ProgressBarWarning].
	State ProgressBarState
	// Value is the current value of the progress bar. It should be between
	// 0 and 100.
	Value int
}

// NewProgressBar returns a new progress bar with the given state and value.
// The value is ignored if the state is [ProgressBarNone] or
// [ProgressBarIndeterminate].
func ( ProgressBarState,  int) *ProgressBar {
	return &ProgressBar{
		State: ,
		Value: min(max(, 0), 100),
	}
}

// KeyboardEnhancements defines different keyboard enhancement features that
// can be requested from the terminal.
type KeyboardEnhancements struct {
	// DisambiguateEscapeCodes requests the terminal to report ambiguous keys
	// such as Ctrl+i and Tab, and Ctrl+m and Enter, and others as distinct key
	// code sequences.
	// If supported, your program will receive distinct [KeyPressEvent]s for
	// these keys.
	DisambiguateEscapeCodes bool

	// ReportEventTypes requests the terminal to report key repeat and release
	// events.
	// If supported, your program will receive [KeyReleaseEvent]s and
	// [KeyPressEvent] with the [Key.IsRepeat] field set indicating that this
	// is a it's part of a key repeat sequence.
	ReportEventTypes bool

	// ReportAlternateKeys requests the terminal to report alternate key values
	// in addition to the main ones.
	// Note that only key events represented as escape codes will affected by
	// this enhancement.
	ReportAlternateKeys bool

	// ReportAllKeysAsEscapeCodes requests the terminal to report all key
	// events, including plain text keys, as escape codes.
	// When this is enabled, text won't be sent as plain text but instead as
	// escape codes that encode the key value and modifiers.
	ReportAllKeysAsEscapeCodes bool

	// ReportAssociatedText requests the terminal to report the text associated
	// with key events.
	// Note that this is an enhancement to
	// [KeyboardEnhancements.ReportAllKeysAsEscapeCodes] and only has an effect
	// if that is enabled.
	ReportAssociatedText bool
}

// NewKeyboardEnhancements returns a new [KeyboardEnhancements] with the given
// options as flags.
//
// A zero, or negative, flags value is treated as no enhancements.
//
// See [ansi.KittyKeyboard] for more details on the supported keyboard enhancements.
func ( int) *KeyboardEnhancements {
	if  <= 0 {
		return &KeyboardEnhancements{}
	}

	return &KeyboardEnhancements{
		DisambiguateEscapeCodes: &ansi.KittyDisambiguateEscapeCodes != 0,
		ReportEventTypes:        &ansi.KittyReportEventTypes != 0,
	}
}

// Flags returns the keyboard enhancements as bits that can be used to set the
// appropriate terminal modes.
func ( KeyboardEnhancements) () int {
	 := 0
	if .DisambiguateEscapeCodes {
		 |= ansi.KittyDisambiguateEscapeCodes
	}
	if .ReportEventTypes {
		 |= ansi.KittyReportEventTypes
	}
	if .ReportAlternateKeys {
		 |= ansi.KittyReportAlternateKeys
	}
	if .ReportAllKeysAsEscapeCodes {
		 |= ansi.KittyReportAllKeysAsEscapeCodes
	}
	if .ReportAssociatedText {
		 |= ansi.KittyReportAssociatedKeys
	}

	return 
}

// EncodeBackgroundColor encodes the background color to the given writer. Use
// nil to reset the background color to the default.
func ( io.Writer,  color.Color) error {
	var  string
	if  == nil {
		 = ansi.ResetBackgroundColor
	} else if ,  := colorful.MakeColor();  {
		 = ansi.SetBackgroundColor(.Hex())
	} else {
		return fmt.Errorf("invalid color: %v", )
	}

	,  := io.WriteString(, )
	if  != nil {
		return fmt.Errorf("failed to set background color: %w", )
	}

	return nil
}

// EncodeForegroundColor encodes the foreground color to the given writer. Use
// nil to reset the foreground color to the default.
func ( io.Writer,  color.Color) error {
	var  string
	if  == nil {
		 = ansi.ResetForegroundColor
	} else if ,  := colorful.MakeColor();  {
		 = ansi.SetForegroundColor(.Hex())
	} else {
		return fmt.Errorf("invalid color: %v", )
	}

	,  := io.WriteString(, )
	if  != nil {
		return fmt.Errorf("failed to set foreground color: %w", )
	}

	return nil
}

// EncodeCursorColor encodes the cursor color to the given writer. Use nil to
// reset the cursor color to the default.
func ( io.Writer,  color.Color) error {
	var  string
	if  == nil {
		 = ansi.ResetCursorColor
	} else if ,  := colorful.MakeColor();  {
		 = ansi.SetCursorColor(.Hex())
	} else {
		return fmt.Errorf("invalid color: %v", )
	}

	,  := io.WriteString(, )
	if  != nil {
		return fmt.Errorf("failed to set cursor color: %w", )
	}

	return nil
}

// EncodeCursorStyle encodes the cursor style to the given writer.
func ( io.Writer,  CursorShape,  bool) error {
	 := ansi.SetCursorStyle(.Encode())
	,  := io.WriteString(, )
	if  != nil {
		return fmt.Errorf("failed to set cursor style: %w", )
	}

	return nil
}

// EncodeBracketedPaste encodes the bracketed paste mode to the given writer.
func ( io.Writer,  bool) error {
	var  string
	if  {
		 = ansi.SetModeBracketedPaste
	} else {
		 = ansi.ResetModeBracketedPaste
	}

	,  := io.WriteString(, )
	if  != nil {
		return fmt.Errorf("failed to set bracketed paste mode: %w", )
	}

	return nil
}

// EncodeMouseMode encodes the mouse tracking mode to the given writer.
func ( io.Writer,  MouseMode) error {
	var  string
	switch  {
	case MouseModeNone:
		 = ansi.ResetModeMouseX10 +
			ansi.ResetModeMouseNormal +
			ansi.ResetModeMouseButtonEvent +
			ansi.ResetModeMouseAnyEvent
	case MouseModePress:
		 = ansi.SetModeMouseX10
	case MouseModeClick:
		 = ansi.SetModeMouseNormal
	case MouseModeDrag:
		 = ansi.SetModeMouseButtonEvent
	case MouseModeMotion:
		 = ansi.SetModeMouseAnyEvent
	}

	,  := io.WriteString(, ) //nolint:errcheck
	if  != nil {
		return fmt.Errorf("failed to set mouse mode: %w", )
	}

	return nil
}

// EncodeMouseEncoding encodes the mouse encoding mode to the given writer.
// When enc is [MouseEncodingLegacy], all extended encodings are reset.
func ( io.Writer,  MouseEncoding) error {
	var  string
	switch  {
	case MouseEncodingLegacy:
		 = ansi.ResetModeMouseExtSgr +
			ansi.ResetModeMouseExtUrxvt +
			ansi.ResetModeMouseExtSgrPixel
	case MouseEncodingSGR:
		 = ansi.SetModeMouseExtSgr
	case MouseEncodingSGRPixel:
		 = ansi.SetModeMouseExtSgrPixel
	}

	,  := io.WriteString(, ) //nolint:errcheck
	if  != nil {
		return fmt.Errorf("failed to set mouse encoding: %w", )
	}

	return nil
}

// EncodeProgressBar encodes the progress bar to the given writer.
func ( io.Writer,  *ProgressBar) error {
	if  == nil {
		 = &ProgressBar{State: ProgressBarNone}
	}

	var  string
	 := clamp(.Value, 0, 100)
	switch .State {
	case ProgressBarNone:
		 = ansi.ResetProgressBar
	case ProgressBarDefault:
		 = ansi.SetProgressBar()
	case ProgressBarError:
		 = ansi.SetErrorProgressBar()
	case ProgressBarIndeterminate:
		 = ansi.SetIndeterminateProgressBar
	case ProgressBarWarning:
		 = ansi.SetWarningProgressBar()
	default:
		return fmt.Errorf("invalid progress bar state: %d", .State)
	}

	,  := io.WriteString(, )
	if  != nil {
		return fmt.Errorf("failed to set progress bar: %w", )
	}

	return nil
}

// EncodeKeyboardEnhancements encodes the keyboard enhancements to the given
// writer.
func ( io.Writer,  *KeyboardEnhancements) error {
	var  int
	if  != nil {
		 = .Flags()
	}
	,  := io.WriteString(, ansi.KittyKeyboard(, 1))
	if  != nil {
		return fmt.Errorf("failed to set keyboard enhancements: %w", )
	}
	return nil
}

// EncodeWindowTitle encodes the window title to the given writer.
func ( io.Writer,  string) error {
	,  := io.WriteString(, ansi.SetWindowTitle())
	if  != nil {
		return fmt.Errorf("failed to set window title: %w", )
	}
	return nil
}