package stringUtil

import (
	"bytes"
	"fmt"
)

type StringBuilder struct {
	buffer bytes.Buffer
}

func NewStringBuilder() *StringBuilder {
	var builder StringBuilder
	return &builder
}

func (builder *StringBuilder) Append(format string, args ...interface{}) *StringBuilder {
	if len(args) > 0 {
		format = fmt.Sprintf(format, args...)
	}

	builder.buffer.WriteString(format)
	return builder
}

func (builder *StringBuilder) AppendLine(format string, args ...interface{}) *StringBuilder {
	if len(args) > 0 {
		format = fmt.Sprintf(format, args...)
	}

	builder.buffer.WriteString(format + "\n")
	return builder
}

func (builder *StringBuilder) AppendStrings(ss ...string) *StringBuilder {
	for i := range ss {
		builder.buffer.WriteString(ss[i])
	}
	return builder
}

func (builder *StringBuilder) AppendLines(ss ...string) *StringBuilder {
	for i := range ss {
		builder.buffer.WriteString(ss[i] + "\n")
	}
	return builder
}

func (builder *StringBuilder) Clear() *StringBuilder {
	var buffer bytes.Buffer
	builder.buffer = buffer
	return builder
}

func (builder *StringBuilder) ToString() string {
	return builder.buffer.String()
}
