package uv

import (
	
	

	
	
)

// EmptyCell is a cell with a single space, width of 1, and no style or link.
var EmptyCell = Cell{Content: " ", Width: 1}

// Cell represents a single cell in the terminal screen.
type Cell struct {
	// Content is the [Cell]'s content, which consists of a single grapheme
	// cluster. Most of the time, this will be a single rune as well, but it
	// can also be a combination of runes that form a grapheme cluster.
	Content string

	// The style of the cell. Nil style means no style. Zero value prints a
	// reset sequence.
	Style Style

	// Link is the hyperlink of the cell.
	Link Link

	// Width is the mono-spaced width of the grapheme cluster.
	Width int
}

// NewCell creates a new cell from the given string grapheme. It will only use
// the first grapheme in the string and ignore the rest. The width of the cell
// is determined using the given width method.
func ( WidthMethod,  string) *Cell {
	if len() == 0 {
		return &Cell{}
	}
	if  == " " {
		return EmptyCell.Clone()
	}
	return &Cell{
		Content: ,
		Width:   .StringWidth(),
	}
}

// String returns the string content of the cell excluding any styles, links,
// and escape sequences.
func ( *Cell) () string {
	return .Content
}

// Equal returns whether the cell is equal to the other cell.
func ( *Cell) ( *Cell) bool {
	return  != nil &&
		.Width == .Width &&
		.Content == .Content &&
		.Style.Equal(&.Style) &&
		.Link.Equal(&.Link)
}

// IsZero returns whether the cell is an empty cell.
func ( *Cell) () bool {
	return * == Cell{}
}

// isWidePlaceholder reports whether the cell is the continuation column of a
// wide cell, marked with a zero display width.
func ( *Cell) () bool {
	return  != nil && .Width == 0
}

// Clone returns a copy of the cell.
func ( *Cell) () ( *Cell) {
	 = new(Cell)
	* = *
	return
}

// Empty makes the cell an empty cell by setting its content to a single space
// and width to 1.
func ( *Cell) () {
	.Content = " "
	.Width = 1
}

// NewLink creates a new hyperlink with the given URL and parameters.
func ( string,  ...string) Link {
	return Link{
		URL:    ,
		Params: strings.Join(, ":"),
	}
}

// Link represents a hyperlink in the terminal screen.
type Link struct {
	URL    string
	Params string
}

// String returns a string representation of the hyperlink.
func ( *Link) () string {
	return .URL
}

// Equal returns whether the hyperlink is equal to the other hyperlink.
func ( *Link) ( *Link) bool {
	return  != nil && * == *
}

// IsZero returns whether the hyperlink is empty.
func ( *Link) () bool {
	return * == Link{}
}

// These are the available text attributes that can be combined to create
// different styles.
const (
	AttrBold = 1 << iota
	AttrFaint
	AttrItalic
	AttrBlink
	AttrRapidBlink // Not widely supported
	AttrReverse
	AttrConceal
	AttrStrikethrough

	AttrReset = 0
)

// AttrSlowBlink is an alias for AttrBlink.
//
// Deprecated: Use [AttrBlink] instead.
const (
	AttrSlowBlink = AttrBlink
)

// Underline is the style of underline to use for text.
type Underline = ansi.Underline

// UnderlineStyle is the style of underline to use for text.
//
// Deprecated: Use [Underline] instead.
type UnderlineStyle = ansi.Underline

// These are the available underline styles.
const (
	UnderlineNone   = ansi.UnderlineNone
	UnderlineSingle = ansi.UnderlineSingle
	UnderlineDouble = ansi.UnderlineDouble
	UnderlineCurly  = ansi.UnderlineCurly
	UnderlineDotted = ansi.UnderlineDotted
	UnderlineDashed = ansi.UnderlineDashed
)

// These are the available underline styles.
//
// Deprecated: Use the constants from [Underline] instead.
const (
	UnderlineStyleNone   = ansi.UnderlineNone
	UnderlineStyleSingle = ansi.UnderlineSingle
	UnderlineStyleDouble = ansi.UnderlineDouble
	UnderlineStyleCurly  = ansi.UnderlineCurly
	UnderlineStyleDotted = ansi.UnderlineDotted
	UnderlineStyleDashed = ansi.UnderlineDashed
)

// Style represents the style of a cell.
type Style struct {
	Fg             color.Color
	Bg             color.Color
	UnderlineColor color.Color
	Underline      Underline
	Attrs          uint8
}

// Equal returns true if the style is equal to the other style.
func ( *Style) ( *Style) bool {
	return .Attrs == .Attrs &&
		.Underline == .Underline &&
		colorEqual(.Fg, .Fg) &&
		colorEqual(.Bg, .Bg) &&
		colorEqual(.UnderlineColor, .UnderlineColor)
}

// Styled wraps the given string with the style's ANSI sequences and resets.
func ( *Style) ( string) string {
	if .IsZero() {
		return 
	}
	return .String() +  + ansi.ResetStyle
}

// String returns the ANSI SGR sequence for the style.
func ( *Style) () string {
	if .IsZero() {
		return ansi.ResetStyle
	}

	var  ansi.Style

	if .Attrs != 0 { //nolint:nestif
		if .Attrs&AttrBold != 0 {
			 = .Bold()
		}
		if .Attrs&AttrFaint != 0 {
			 = .Faint()
		}
		if .Attrs&AttrItalic != 0 {
			 = .Italic(true)
		}
		if .Attrs&AttrBlink != 0 {
			 = .Blink(true)
		}
		if .Attrs&AttrRapidBlink != 0 {
			 = .RapidBlink(true)
		}
		if .Attrs&AttrReverse != 0 {
			 = .Reverse(true)
		}
		if .Attrs&AttrConceal != 0 {
			 = .Conceal(true)
		}
		if .Attrs&AttrStrikethrough != 0 {
			 = .Strikethrough(true)
		}
	}
	if .Underline != UnderlineStyleNone {
		switch .Underline {
		case UnderlineStyleSingle:
			 = .Underline(true)
		case UnderlineStyleDouble:
			 = .UnderlineStyle(UnderlineStyleDouble)
		case UnderlineStyleCurly:
			 = .UnderlineStyle(UnderlineStyleCurly)
		case UnderlineStyleDotted:
			 = .UnderlineStyle(UnderlineStyleDotted)
		case UnderlineStyleDashed:
			 = .UnderlineStyle(UnderlineStyleDashed)
		}
	}
	if .Fg != nil {
		 = .ForegroundColor(.Fg)
	}
	if .Bg != nil {
		 = .BackgroundColor(.Bg)
	}
	if .UnderlineColor != nil {
		 = .UnderlineColor(.UnderlineColor)
	}

	return .String()
}

// Diff returns the ANSI sequence that sets the style as a diff from
// another style.
func ( *Style) ( *Style) string {
	return StyleDiff(, )
}

// StyleDiff returns the SGR ANSI sequence necessary to transition from the
// "from" style to the "to" style.
func (,  *Style) string {
	if  == nil &&  == nil {
		return ""
	}
	if  != nil &&  != nil && .Equal() {
		return ""
	}
	if  == nil {
		return .String()
	}
	if  == nil || .IsZero() {
		// Resetting all styles is cheaper than calculating diffs.
		// "\x1b[m" is 3 bytes vs potentially much longer sequences.
		return ansi.ResetStyle
	}

	// TODO: Optimize further by checking if a full reset is cheaper than
	// calculating diffs. Often, it might be cheaper to reset everything and
	// then set the desired styles rather than calculating diffs. Also more
	// compatible with terminals that have buggy SGR implementations.

	var  ansi.Style

	if !colorEqual(.Fg, .Fg) {
		 = .ForegroundColor(.Fg)
	}

	if !colorEqual(.Bg, .Bg) {
		 = .BackgroundColor(.Bg)
	}

	if !colorEqual(.UnderlineColor, .UnderlineColor) {
		// TODO: Investigate this. For backward compatibility, we might want to
		// set this at the end instead. Because on terminals that don't support
		// underline color, this might mess up some other attributes if set in
		// the middle.
		 = .UnderlineColor(.UnderlineColor)
	}

	 := .Attrs&AttrBold != 0
	 := .Attrs&AttrFaint != 0
	 := .Attrs&AttrItalic != 0
	 := .Underline != UnderlineStyleNone
	 := .Attrs&AttrBlink != 0
	 := .Attrs&AttrRapidBlink != 0
	 := .Attrs&AttrReverse != 0
	 := .Attrs&AttrConceal != 0
	 := .Attrs&AttrStrikethrough != 0
	 := .Attrs&AttrBold != 0
	 := .Attrs&AttrFaint != 0
	 := .Attrs&AttrItalic != 0
	 := .Underline != UnderlineStyleNone
	 := .Attrs&AttrBlink != 0
	 := .Attrs&AttrRapidBlink != 0
	 := .Attrs&AttrReverse != 0
	 := .Attrs&AttrConceal != 0
	 := .Attrs&AttrStrikethrough != 0

	// We perform the resets first since they are single attributes and
	// shouldn't interfere with others being set.

	 :=  != 
	 :=  != 
	if  ||  {
		if  && ! ||  && ! {
			 = .Normal()
			 = true
			 = true
		}
	}

	 :=  != 
	if  && ! {
		 = .Italic(false)
	}

	 :=  !=  || .Underline != .Underline
	if  && ! {
		 = .Underline(false)
	}

	 :=  != 
	 :=  != 
	if  ||  {
		if  && ! ||  && ! {
			 = .Blink(false)
			 = true
			 = true
		}
	}

	 :=  != 
	if  && ! {
		 = .Reverse(false)
	}

	 :=  != 
	if  && ! {
		 = .Conceal(false)
	}

	 :=  != 
	if  && ! {
		 = .Strikethrough(false)
	}

	if  &&  {
		 = .Bold()
	}

	if  &&  {
		 = .Faint()
	}

	if  &&  {
		 = .Italic(true)
	}

	if  &&  && .Underline == UnderlineStyleSingle {
		// We only handle single underline here since others require more
		// specific handling at the end.
		 = .Underline(true)
	}

	if  &&  {
		 = .Blink(true)
	}

	if  &&  {
		 = .RapidBlink(true)
	}

	if  &&  {
		 = .Reverse(true)
	}

	if  &&  {
		 = .Conceal(true)
	}

	if  &&  {
		 = .Strikethrough(true)
	}

	// Handle special underline styles.
	if  &&  && .Underline > UnderlineStyleSingle {
		 = .UnderlineStyle(.Underline)
	}

	return .String()
}

func (,  color.Color) bool {
	if  == nil &&  == nil {
		return true
	}
	if  == nil ||  == nil {
		return false
	}
	, , ,  := .RGBA()
	, , ,  := .RGBA()
	return  ==  &&  ==  &&  ==  &&  == 
}

// IsZero returns true if the style is empty.
func ( *Style) () bool {
	return * == Style{}
}

// ConvertStyle converts a style to respect the given color profile.
func ( Style,  colorprofile.Profile) Style {
	switch  {
	case colorprofile.TrueColor:
		return 
	case colorprofile.ANSI, colorprofile.ANSI256:
	case colorprofile.Ascii:
		.Fg = nil
		.Bg = nil
		.UnderlineColor = nil
	case colorprofile.NoTTY:
		return Style{}
	}

	if .Fg != nil {
		.Fg = .Convert(.Fg)
	}
	if .Bg != nil {
		.Bg = .Convert(.Bg)
	}
	if .UnderlineColor != nil {
		.UnderlineColor = .Convert(.UnderlineColor)
	}
	return 
}

// ConvertLink converts a hyperlink to respect the given color profile.
func ( Link,  colorprofile.Profile) Link {
	if  == colorprofile.NoTTY {
		return Link{}
	}
	return 
}