package action

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

type BufferBuilder struct {
	buffer Buffer
}

// NewBufferBuilder: Creates a new buffer builder. The buffer builder implements a
// builder pattern to create a buffer with the defined properties and values.
func NewBufferBuilder() *BufferBuilder {
	return &BufferBuilder{
		buffer: Buffer{
			Id:       0, // This is set when built
			Filename: "",
			Filetype: "",
			Lines:    []string{""},
			Modified: false,
			Loaded:   false,
			Listed:   false,
		},
	}
}

// BufferBuilder.WithFilename: Attaches a file name to the buffer that is being built.
func (b *BufferBuilder) WithFilename(filename string) *BufferBuilder {
	b.buffer.Filename = filename
	return b
}

// BufferBuilder.WithFiletype: Attaches a file type to the buffer that is being built.
func (b *BufferBuilder) WithFiletype(filetype string) *BufferBuilder {
	b.buffer.Filetype = filetype
	return b
}

// BufferBuilder.WithLines: Attaches a lines to the buffer that is being built.
func (b *BufferBuilder) WithLines(lines []string) *BufferBuilder {
	b.buffer.Lines = lines
	return b
}

// BufferBuilder.Modified: Sets the modified flag of the buffer being built. By default,
// buffers are built with the modified flag being false.
func (b *BufferBuilder) Modified() *BufferBuilder {
	b.buffer.Modified = true
	return b
}

// BufferBuilder.Loaded: Sets the loaded flag of the buffer being built. By default,
// buffers are built with the loaded flag being false.
func (b *BufferBuilder) Loaded() *BufferBuilder {
	b.buffer.Loaded = true
	return b
}

// BufferBuilder.Listed: Sets the listed flag of the buffer being built. By default,
// buffers are built with the listed flag being false.
func (b *BufferBuilder) Listed() *BufferBuilder {
	b.buffer.Listed = true
	return b
}

// BufferBuilder.Build: Build the final buffer and return it to the caller. Final
// step in the process. This is where the ID is set, so many buffers can be "in-progress"
// but the ID will be set when they are built. Meaning, this is not thread safe.
func (b *BufferBuilder) Build() Buffer {
	b.buffer.Id = CurrentBufferId
	CurrentBufferId++

	return b.buffer
}
