package test

import (
	"bufio"
	"fmt"
	"strings"

	"charm.land/lipgloss/v2"
)

type part int

const (
	normal = iota
	id
	author
	bookmark
)

var styles = map[part]lipgloss.Style{
	normal:   lipgloss.NewStyle(),
	id:       lipgloss.NewStyle().Foreground(lipgloss.Color("1")),
	author:   lipgloss.NewStyle().Foreground(lipgloss.Color("2")),
	bookmark: lipgloss.NewStyle().Foreground(lipgloss.Color("3")),
}

type LogBuilder struct {
	w strings.Builder
}

func (l *LogBuilder) String() string {
	return l.w.String()
}

func (l *LogBuilder) Write(line string) {
	scanner := bufio.NewScanner(strings.NewReader(line))
	scanner.Split(bufio.ScanWords)
	for scanner.Scan() {
		text := scanner.Text()
		if after, ok := strings.CutPrefix(text, "short_id="); ok {
			text = after
			l.ShortId(text)
			continue
		}
		if after, ok := strings.CutPrefix(text, "id="); ok {
			text = after
			l.Id(text[:1], text[1:])
			continue
		}
		if after, ok := strings.CutPrefix(text, "author="); ok {
			l.Author(after)
			continue
		}
		if after, ok := strings.CutPrefix(text, "bookmarks="); ok {
			text = after
			values := strings.Split(text, ",")
			l.Bookmarks(strings.Join(values, " "))
			continue
		}
		l.Append(text)
	}
	l.w.WriteString("\n")
}

func (l *LogBuilder) Append(value string) {
	fmt.Fprintf(&l.w, "%s ", styles[normal].Render(value))
}

func (l *LogBuilder) ShortId(sid string) {
	fmt.Fprintf(&l.w, " %s ", styles[id].Render(sid))
}

func (l *LogBuilder) Id(short string, rest string) {
	fmt.Fprintf(&l.w, " %s%s ", styles[id].Render(short), styles[id].Render(rest))
}

func (l *LogBuilder) Author(value string) {
	fmt.Fprintf(&l.w, " %s ", styles[author].Render(value))
}

func (l *LogBuilder) Bookmarks(value string) {
	fmt.Fprintf(&l.w, " %s ", styles[bookmark].Render(value))
}
