package lipgloss

import (
	uv 
	
)

// Canvas is a cell-buffer that can be used to compose and draw [uv.Drawable]s
// like [Layer]s.
//
// Composed drawables are drawn onto the canvas in the order they were
// composed, meaning later drawables will appear "on top" of earlier ones.
//
// A canvas can read, modify, and render its cell contents.
//
// It implements [uv.Screen] and [uv.Drawable].
type Canvas struct {
	scr uv.ScreenBuffer
}

var _ uv.Screen = (*Canvas)(nil)

// NewCanvas creates a new [Canvas] with the given size.
func (,  int) *Canvas {
	 := new(Canvas)
	.scr = uv.NewScreenBuffer(, )
	.scr.Method = ansi.GraphemeWidth
	return 
}

// Resize resizes the canvas to the given width and height.
func ( *Canvas) (,  int) {
	.scr.Resize(, )
}

// Clear clears the canvas.
func ( *Canvas) () {
	.scr.Clear()
}

// Bounds implements [uv.Screen].
func ( *Canvas) () uv.Rectangle {
	return .scr.Bounds()
}

// Width returns the width of the canvas.
func ( *Canvas) () int {
	return .scr.Width()
}

// Height returns the height of the canvas.
func ( *Canvas) () int {
	return .scr.Height()
}

// CellAt implements [uv.Screen].
func ( *Canvas) ( int,  int) *uv.Cell {
	return .scr.CellAt(, )
}

// SetCell implements [uv.Screen].
func ( *Canvas) ( int,  int,  *uv.Cell) {
	.scr.SetCell(, , )
}

// WidthMethod implements [uv.Screen].
func ( *Canvas) () uv.WidthMethod {
	return .scr.WidthMethod()
}

// Compose composes a [Layer] or any [uv.Drawable] onto the [Canvas].
func ( *Canvas) ( uv.Drawable) *Canvas {
	.Draw(, .Bounds())
	return 
}

// Draw draws the [Canvas] onto the given [uv.Screen] within the specified
// area.
//
// It implements [uv.Drawable].
func ( *Canvas) ( uv.Screen,  uv.Rectangle) {
	.scr.Draw(, )
}

// Render renders the canvas into a styled string. Any trailing spaces are
// trimmed.
func ( *Canvas) () string {
	return uv.TrimSpace(.scr.Render())
}