package uv

import (
	
	
	

	
)

var isWindows = runtime.GOOS == "windows"

// File is an interface that represents a file with a file descriptor.
//
// This is typically an [os.File] like [os.Stdin] and [os.Stdout].
type File interface {
	io.ReadWriteCloser
	term.File

	// Name returns the name of the file.
	Name() string
}

// Winsize represents the size of a terminal in cells and pixels.
//
// This is the same as [unix.Winsize], but defined here for cross-platform compatibility.
type Winsize struct {
	Row    uint16
	Col    uint16
	Xpixel uint16
	Ypixel uint16
}

// Console represents a cross-platform console I/O interface.
type Console interface {
	io.ReadWriteCloser

	// Environ returns the console's environment variables.
	Environ() []string

	// Getenv retrieves the value of the environment variable named by the key.
	Getenv(key string) string

	// LookupEnv retrieves the value of the environment variable named by the key
	// and a boolean indicating whether the variable is present.
	LookupEnv(key string) (string, bool)

	// Reader returns the input reader of the console.
	Reader() io.Reader

	// Writer returns the output writer of the console.
	Writer() io.Writer

	// MakeRaw puts the console input side into raw mode.
	MakeRaw() (state *term.State, err error)

	// Restore restores the console to its previous state.
	Restore() error

	// GetSize returns the current size of the console.
	GetSize() (width, height int, err error)

	// GetWinsize returns the current size of the console in cells and pixels.
	GetWinsize() (*Winsize, error)
}

// TTY represents a Unix TTY device. It implements the [Console] interface.
type TTY struct {
	*console
}

var _ Console = (*TTY)(nil)

// WinCon represents a Windows Console. It implements the [Console] interface.
type WinCon struct {
	*console
}

var _ Console = (*WinCon)(nil)

// Console is a cross-platform console I/O.
type console struct {
	input       File
	inputState  *term.State
	output      File
	outputState *term.State
	environ     Environ
}

// DefaultConsole returns a new default console instance that uses standard I/O
// [os.Stdin], [os.Stdout], and [os.Environ].
//
// To use [os.Stderr] as the output, you can create a new console with
// [NewConsole] and pass [os.Stderr] as the output parameter.
func () Console {
	return NewConsole(os.Stdin, os.Stdout, os.Environ())
}

// ControllingConsole returns a new console instance that uses the current
// controlling terminal's input and output file descriptors.
func () (Console, error) {
	, ,  := OpenTTY()
	if  != nil {
		return nil, 
	}
	return NewConsole(, , os.Environ()), nil
}

// NewConsole creates a new [Console] with the given input, output, and
// environment variables.
//
// You can use [OpenTTY] to open the current controlling console files and pass
// them to this function. Use [ControllingConsole] for a convenience function
// that does this for you.
//
// Use this to create a new terminal for PTY processes by passing the PTY slave
// file as the input and output and any environment variables the process
// needs.
func (,  File,  []string) Console {
	if  == nil {
		 = os.Stdin
	}
	if  == nil {
		 = os.Stdout
	}
	if  == nil {
		 = os.Environ()
	}
	return newConsole(, , )
}

// Environ returns the console's environment variables.
func ( *console) () []string {
	return .environ
}

// Writer returns the output writer of the console.
func ( *console) () io.Writer {
	return .output
}

// Reader returns the input reader of the console.
func ( *console) () io.Reader {
	return .input
}

// Write writes data to the console's output.
func ( *console) ( []byte) ( int,  error) {
	return .output.Write()
}

// Read reads data from the console's input.
func ( *console) ( []byte) ( int,  error) {
	return .input.Read()
}

// Getenv retrieves the value of the environment variable named by the key.
func ( *console) ( string) string {
	return .environ.Getenv()
}

// LookupEnv retrieves the value of the environment variable named by the key
// and a boolean indicating whether the variable is present.
func ( *console) ( string) (string, bool) {
	return .environ.LookupEnv()
}

// MakeRaw puts the console input side into raw mode.
func ( *console) () ( *term.State,  error) {
	, ,  := makeRaw(.input, .output)
	if  != nil {
		return nil, 
	}
	.inputState = 
	.outputState = 
	if  != nil {
		return , nil
	}
	return , nil
}

// Restore restores the console to its previous state.
func ( *console) () error {
	if .inputState != nil {
		if  := term.Restore(.input.Fd(), .inputState);  != nil {
			return 
		}
		.inputState = nil
	}
	if .outputState != nil {
		if  := term.Restore(.output.Fd(), .outputState);  != nil {
			return 
		}
		.outputState = nil
	}
	return nil
}

// Close restores the console to its previous state and releases resources.
func ( *console) () error {
	if  := .Restore();  != nil {
		return 
	}
	return nil
}

// GetSize returns the current size of the console.
func ( *console) () (,  int,  error) {
	return getSize(.input, .output)
}

// GetWinsize returns the current size of the console in cells and pixels.
func ( *console) () (*Winsize, error) {
	,  := getWinsize(.input, .output)
	if  != nil {
		return nil, 
	}
	return &, nil
}