package kitty

import (
	
	
	
	
)

var (
	_ encoding.TextMarshaler   = Options{}
	_ encoding.TextUnmarshaler = &Options{}
)

// Stringish is a type constraint for types that are either a string or a
// byte slice.
type Stringish interface{ string | []byte }

// Options represents a Kitty Graphics Protocol options.
type Options struct {
	// Common options.

	// Action (a=t) is the action to be performed on the image. Can be one of
	// [Transmit], [TransmitDisplay], [Query], [Put], [Delete], [Frame],
	// [Animate], [Compose].
	Action byte

	// Quite is the kitty graphics quiet mode.
	//
	// Deprecated: misspelled field, use [Options.Quiet] instead. Any non-zero
	// Quite overrides Quiet.
	Quite byte

	// Quiet mode (q=0) is the quiet mode. Can be either zero, one, or two
	// where zero is the default, 1 suppresses OK responses, and 2 suppresses
	// both OK and error responses.
	Quiet byte

	// Transmission options.

	// ID (i=) is the image ID. The ID is a unique identifier for the image.
	// Must be a positive integer up to [math.MaxUint32].
	ID int

	// PlacementID (p=) is the placement ID. The placement ID is a unique
	// identifier for the placement of the image. Must be a positive integer up
	// to [math.MaxUint32].
	PlacementID int

	// Number (I=0) is the number of images to be transmitted.
	Number int

	// Format (f=32) is the image format. One of [RGBA], [RGB], [PNG].
	Format int

	// ImageWidth (s=0) is the transmitted image width.
	ImageWidth int

	// ImageHeight (v=0) is the transmitted image height.
	ImageHeight int

	// Compression (o=) is the image compression type. Can be [Zlib] or zero.
	Compression byte

	// Transmission (t=d) is the image transmission type. Can be [Direct], [File],
	// [TempFile], or[SharedMemory].
	Transmission byte

	// File is the file path to be used when the transmission type is [File].
	// If [Options.Transmission] is omitted i.e. zero and this is non-empty,
	// the transmission type is set to [File].
	File string

	// Size (S=0) is the size to be read from the transmission medium.
	Size int

	// Offset (O=0) is the offset byte to start reading from the transmission
	// medium.
	Offset int

	// Chunk (m=) whether the image is transmitted in chunks. Can be either
	// zero or one. When true, the image is transmitted in chunks. Each chunk
	// must be a multiple of 4, and up to [MaxChunkSize] bytes. Each chunk must
	// have the m=1 option except for the last chunk which must have m=0.
	Chunk bool

	// ChunkFormatter is the function used to format each chunk when
	// [Options.Chunk] is true. If nil, the chunks are sent as is.
	ChunkFormatter func(chunk string) string

	// Display options.

	// X (x=0) is the pixel X coordinate of the image to start displaying.
	X int

	// Y (y=0) is the pixel Y coordinate of the image to start displaying.
	Y int

	// Z (z=0) is the Z coordinate of the image to display.
	Z int

	// Width (w=0) is the width of the image to display.
	Width int

	// Height (h=0) is the height of the image to display.
	Height int

	// OffsetX (X=0) is the OffsetX coordinate of the cursor cell to start
	// displaying the image. OffsetX=0 is the leftmost cell. This must be
	// smaller than the terminal cell width.
	OffsetX int

	// OffsetY (Y=0) is the OffsetY coordinate of the cursor cell to start
	// displaying the image. OffsetY=0 is the topmost cell. This must be
	// smaller than the terminal cell height.
	OffsetY int

	// Columns (c=0) is the number of columns to display the image. The image
	// will be scaled to fit the number of columns.
	Columns int

	// Rows (r=0) is the number of rows to display the image. The image will be
	// scaled to fit the number of rows.
	Rows int

	// VirtualPlacement (U=0) whether to use virtual placement. This is used
	// with Unicode [Placeholder] to display images.
	VirtualPlacement bool

	// DoNotMoveCursor (C=0) whether to move the cursor after displaying the
	// image.
	DoNotMoveCursor bool

	// ParentID (P=0) is the parent image ID. The parent ID is the ID of the
	// image that is the parent of the current image. This is used with Unicode
	// [Placeholder] to display images relative to the parent image.
	ParentID int

	// ParentPlacementID (Q=0) is the parent placement ID. The parent placement
	// ID is the ID of the placement of the parent image. This is used with
	// Unicode [Placeholder] to display images relative to the parent image.
	ParentPlacementID int

	// Delete options.

	// Delete (d=a) is the delete action. Can be one of [DeleteAll],
	// [DeleteID], [DeleteNumber], [DeleteCursor], [DeleteFrames],
	// [DeleteCell], [DeleteCellZ], [DeleteRange], [DeleteColumn], [DeleteRow],
	// [DeleteZ].
	Delete byte

	// DeleteResources indicates whether to delete the resources associated
	// with the image.
	DeleteResources bool
}

// Options returns the options as a slice of a key-value pairs.
func ( *Options) () ( []string) {
	 = []string{}
	if .Format == 0 {
		.Format = RGBA
	}

	if .Action == 0 {
		.Action = Transmit
	}

	if .Delete == 0 {
		.Delete = DeleteAll
	}

	if .Transmission == 0 {
		if len(.File) > 0 {
			.Transmission = File
		} else {
			.Transmission = Direct
		}
	}

	if .Format != RGBA {
		 = append(, fmt.Sprintf("f=%d", .Format))
	}

	 := .Quiet
	if .Quite > 0 {
		// handle deprecated Quite field.
		 = .Quite
	}
	if  > 0 {
		 = append(, fmt.Sprintf("q=%d", ))
	}

	if .ID > 0 {
		 = append(, fmt.Sprintf("i=%d", .ID))
	}

	if .PlacementID > 0 {
		 = append(, fmt.Sprintf("p=%d", .PlacementID))
	}

	if .Number > 0 {
		 = append(, fmt.Sprintf("I=%d", .Number))
	}

	if .ImageWidth > 0 {
		 = append(, fmt.Sprintf("s=%d", .ImageWidth))
	}

	if .ImageHeight > 0 {
		 = append(, fmt.Sprintf("v=%d", .ImageHeight))
	}

	if .Transmission != Direct {
		 = append(, fmt.Sprintf("t=%c", .Transmission))
	}

	if .Size > 0 {
		 = append(, fmt.Sprintf("S=%d", .Size))
	}

	if .Offset > 0 {
		 = append(, fmt.Sprintf("O=%d", .Offset))
	}

	if .Compression == Zlib {
		 = append(, fmt.Sprintf("o=%c", .Compression))
	}

	if .VirtualPlacement {
		 = append(, "U=1")
	}

	if .DoNotMoveCursor {
		 = append(, "C=1")
	}

	if .ParentID > 0 {
		 = append(, fmt.Sprintf("P=%d", .ParentID))
	}

	if .ParentPlacementID > 0 {
		 = append(, fmt.Sprintf("Q=%d", .ParentPlacementID))
	}

	if .X > 0 {
		 = append(, fmt.Sprintf("x=%d", .X))
	}

	if .Y > 0 {
		 = append(, fmt.Sprintf("y=%d", .Y))
	}

	if .Z > 0 {
		 = append(, fmt.Sprintf("z=%d", .Z))
	}

	if .Width > 0 {
		 = append(, fmt.Sprintf("w=%d", .Width))
	}

	if .Height > 0 {
		 = append(, fmt.Sprintf("h=%d", .Height))
	}

	if .OffsetX > 0 {
		 = append(, fmt.Sprintf("X=%d", .OffsetX))
	}

	if .OffsetY > 0 {
		 = append(, fmt.Sprintf("Y=%d", .OffsetY))
	}

	if .Columns > 0 {
		 = append(, fmt.Sprintf("c=%d", .Columns))
	}

	if .Rows > 0 {
		 = append(, fmt.Sprintf("r=%d", .Rows))
	}

	if .Delete != DeleteAll || .DeleteResources {
		 := .Delete
		if .DeleteResources {
			 =  - ' ' // to uppercase
		}

		 = append(, fmt.Sprintf("d=%c", ))
	}

	if .Action != Transmit {
		 = append(, fmt.Sprintf("a=%c", .Action))
	}

	return  // complex function with multiple returns
}

// String returns the string representation of the options.
func ( Options) () string {
	return strings.Join(.Options(), ",")
}

// MarshalText returns the string representation of the options.
func ( Options) () ([]byte, error) {
	return []byte(.String()), nil
}

// UnmarshalText parses the options from the given string.
func ( *Options) ( []byte) error {
	 := strings.Split(string(), ",")
	for ,  := range  {
		 := strings.SplitN(, "=", 2)
		if len() != 2 || len([1]) == 0 {
			continue
		}

		switch [0] {
		case "a":
			.Action = [1][0]
		case "o":
			.Compression = [1][0]
		case "t":
			.Transmission = [1][0]
		case "d":
			 := [1][0]
			if  >= 'A' &&  <= 'Z' {
				.DeleteResources = true
				 =  + ' ' // to lowercase
			}
			.Delete = 
		case "i", "q", "p", "I", "f", "s", "v", "S", "O", "m", "x", "y", "z", "w", "h", "X", "Y", "c", "r", "U", "P", "Q":
			,  := strconv.Atoi([1])
			if  != nil {
				continue
			}

			switch [0] {
			case "i":
				.ID = 
			case "q":
				.Quiet = byte() //nolint:gosec
			case "p":
				.PlacementID = 
			case "I":
				.Number = 
			case "f":
				.Format = 
			case "s":
				.ImageWidth = 
			case "v":
				.ImageHeight = 
			case "S":
				.Size = 
			case "O":
				.Offset = 
			case "m":
				.Chunk =  == 0 ||  == 1
			case "x":
				.X = 
			case "y":
				.Y = 
			case "z":
				.Z = 
			case "w":
				.Width = 
			case "h":
				.Height = 
			case "X":
				.OffsetX = 
			case "Y":
				.OffsetY = 
			case "c":
				.Columns = 
			case "r":
				.Rows = 
			case "U":
				.VirtualPlacement =  == 1
			case "P":
				.ParentID = 
			case "Q":
				.ParentPlacementID = 
			}
		}
	}

	return nil
}