package terminal

import (
	"fmt"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
	"log"
)

func (m *Model) Init() tea.Cmd {
	return m.questions[m.index].Input.Blink
}

func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	current := &m.questions[m.index]
	var cmd tea.Cmd
	switch msg := msg.(type) {
	case tea.WindowSizeMsg:
		m.width = msg.Width
		m.height = msg.Height
	case tea.KeyMsg:
		switch msg.String() {
		case "ctrl+c", tea.KeyEsc.String(), tea.KeyEscape.String():
			return m, tea.Quit
		case tea.KeyEsc.String():
			return m, tea.Quit
		case "enter":
			if m.index == len(m.questions)-1 {
				m.done = true
			}
			current.Answer = current.Input.Value()
			m.Next()
			if m.done {
				return m, tea.Quit
			}
			return m, current.Input.Blur
		}
	}
	current.Input, cmd = current.Input.Update(msg)
	return m, cmd
}

func (m *Model) View() string {
	current := m.questions[m.index]
	if m.done {
		var output string
		for _, q := range m.questions {
			output += fmt.Sprintf("%s: %s\n", q.Question, q.Answer)
		}
		return output
	}
	if m.width == 0 {
		return "loading..."
	}
	// stack some left-aligned strings together in the center of the window
	return lipgloss.Place(
		m.width,
		m.height,
		lipgloss.Center,
		lipgloss.Center,
		lipgloss.JoinVertical(
			lipgloss.Left,
			current.Question,
			m.styles.InputField.Render(current.Input.View()),
		),
	)
}

func (m *Model) Next() {
	if m.index < len(m.questions)-1 {
		m.index++
	} else {
		m.index = 0
	}
}

func StartInteractiveTerminal(questions []ProjectQuestion) {
	m := New(questions)
	p := tea.NewProgram(m, tea.WithAltScreen())
	if _, err := p.Run(); err != nil {
		log.Fatal(err)
	}
}
