package action

type BufferOptions struct {
	// tabstop expandtab
}

type Buffer struct {
	// Buffer data
	Id int

	// File data
	Filename string
	Filetype string
	Lines    []string

	// Flags (not used yet)
	Modified bool
	Loaded   bool
	Listed   bool

	// Options BufferOptions
	// UndoTree TODO: This will be big
}

// Not great, but maybe the best way
var CurrentBufferId int = 1

func NewEmptyBuffer(lines []string) *Buffer {
	buf := Buffer{
		Id:       CurrentBufferId,
		Filename: "",
		Filetype: "",
		Lines:    lines,
		Modified: false,
		Loaded:   true,
		Listed:   true,
	}

	CurrentBufferId++

	return &buf
}

// Get the line at an index
func (b *Buffer) Line(idx int) string {
	if idx < 0 || idx >= len(b.Lines) {
		return ""
	}
	return b.Lines[idx]
}

// Set the content at an index.
func (b *Buffer) SetLine(idx int, content string) {
	if idx >= 0 && idx < len(b.Lines) {
		b.Lines[idx] = content
	}
}

// Insert a line with content at an index
func (b *Buffer) InsertLine(idx int, content string) {
	if idx < 0 {
		idx = 0
	}
	if idx > len(b.Lines) {
		idx = len(b.Lines)
	}
	b.Lines = append(b.Lines[:idx], append([]string{content}, b.Lines[idx:]...)...)
}

// Delete a line at an index
func (b *Buffer) DeleteLine(idx int) {
	if idx >= 0 && idx < len(b.Lines) {
		b.Lines = append(b.Lines[:idx], b.Lines[idx+1:]...)
	}
}

// Get the number of lines in the buffer
func (b *Buffer) LineCount() int {
	return len(b.Lines)
}
