package uv

import 

// Window represents a rectangular area on the screen. It can be a root window
// with no parent, or a sub-window with a parent window. A window can have its
// own buffer or share the buffer of its parent window (view).
type Window struct {
	*Buffer

	method *WidthMethod
	parent *Window
	bounds Rectangle
}

var (
	_ Screen   = (*Window)(nil)
	_ Drawable = (*Window)(nil)
)

// HasParent returns whether the window has a parent window. This can be used
// to determine if the window is a root window or a sub-window.
func ( *Window) () bool {
	return .parent != nil
}

// Parent returns the parent window of the current window.
// If the window does not have a parent, it returns nil.
func ( *Window) () *Window {
	return .parent
}

// MoveTo moves the window to the specified x and y coordinates.
func ( *Window) (,  int) {
	 := .bounds.Size()
	.bounds.Min.X = 
	.bounds.Min.Y = 
	.bounds.Max.X =  + .X
	.bounds.Max.Y =  + .Y
}

// MoveBy moves the window by the specified delta x and delta y.
func ( *Window) (,  int) {
	.bounds.Min.X += 
	.bounds.Min.Y += 
	.bounds.Max.X += 
	.bounds.Max.Y += 
}

// Clone creates an exact copy of the window, including its buffer and values.
// The cloned window will have the same parent and method as the original
// window.
func ( *Window) () *Window {
	return .CloneArea(.Buffer.Bounds())
}

// CloneArea creates an exact copy of the window, including its buffer and
// values, but only within the specified area. The cloned window will have the
// same parent and method as the original window, but its bounds will be
// limited to the specified area.
func ( *Window) ( Rectangle) *Window {
	 := new(Window)
	.Buffer = .Buffer.CloneArea()
	.parent = .parent
	.method = .method
	.bounds = 
	return 
}

// Resize resizes the window to the specified width and height.
func ( *Window) (,  int) {
	// Only resize the buffer if this window owns its buffer.
	if .parent == nil || .Buffer != .parent.Buffer {
		.Buffer.Resize(, )
	}
	.bounds.Max.X = .bounds.Min.X + 
	.bounds.Max.Y = .bounds.Min.Y + 
}

// WidthMethod returns the method used to calculate the width of characters in
// the window.
func ( *Window) () WidthMethod {
	return *.method
}

// Bounds returns the bounds of the window as a rectangle.
func ( *Window) () Rectangle {
	return .bounds
}

// NewWindow creates a new window with its own buffer relative to the parent
// window at the specified position and size.
//
// This will panic if width or height is negative.
func ( *Window) (, , ,  int) *Window {
	return newWindow(, , , , , .method, false)
}

// NewView creates a new view into the parent window at the specified position
// and size. Unlike [Window.NewWindow], this view shares the same buffer as the
// parent window.
func ( *Window) (, , ,  int) *Window {
	return newWindow(, , , , , .method, true)
}

// NewWindow creates a new root [Window] with the given size and width method.
// If the method is nil, it defaults to [ansi.WcWidth].
//
// The [WidthMethod] is used to calculate the width of characters in the
// window, which is important for correctly rendering text, especially when
// dealing with wide characters, combining characters, emojis, and other
// Unicode characters that may have varying widths.
//
// Use [ansi.WcWidth] as the default width method, which is a common
// implementation that handles a wide range of Unicode characters according to
// the Unicode Standard. Use [ansi.GraphemeWidth] if you know that your
// terminal supports grapheme clusters and proper rendering of combining
// characters and emojis, and you want to ensure that the width calculations
// are based on grapheme clusters rather than individual code points.
//
// This will panic if width or height is negative.
func (,  int,  WidthMethod) *Window {
	if  == nil {
		 = ansi.WcWidth
	}
	return newWindow(nil, 0, 0, , , &, false)
}

// SetWidthMethod sets the width method for the window.
func ( *Window) ( WidthMethod) {
	.method = &
}

// newWindow creates a new [Window] with the specified parent, position,
// method, and size.
func ( *Window, , , ,  int,  *WidthMethod,  bool) *Window {
	 := new(Window)
	if  {
		.Buffer = .Buffer
	} else {
		.Buffer = NewBuffer(, )
	}
	.parent = 
	.method = 
	.bounds = Rect(, , , )
	return 
}