package steps

import (
	"miasma-installer/tui/styles"
	"strings"

	"github.com/charmbracelet/bubbles/textinput"
	tea "github.com/charmbracelet/bubbletea"
)

type EncryptionModel struct {
	EnableEncryption bool
	Password         string
	ConfirmPassword  string
	passwordInput    textinput.Model
	confirmInput     textinput.Model
	focusIndex       int
	Finished         bool
	showInputs       bool
}

func NewEncryptionModel() EncryptionModel {
	pi := textinput.New()
	pi.Placeholder = "Enter encryption password"
	pi.EchoMode = textinput.EchoPassword
	pi.Width = 40
	pi.Focus()

	ci := textinput.New()
	ci.Placeholder = "Confirm password"
	ci.EchoMode = textinput.EchoPassword
	ci.Width = 40

	return EncryptionModel{
		EnableEncryption: true,
		passwordInput:    pi,
		confirmInput:     ci,
		showInputs:       true,
	}
}

func (m EncryptionModel) Init() tea.Cmd {
	return textinput.Blink
}

func (m EncryptionModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	if m.Finished {
		return m, nil
	}

	switch msg := msg.(type) {
	case tea.KeyMsg:
		switch msg.String() {
		case "enter":
			if !m.showInputs {
				m.Finished = true
				return m, nil
			}

			if m.focusIndex == 0 {
				m.focusIndex = 1
				m.passwordInput.Blur()
				return m, m.confirmInput.Focus()
			} else if m.focusIndex == 1 {
				if m.passwordInput.Value() == m.confirmInput.Value() && m.passwordInput.Value() != "" {
					m.Password = m.passwordInput.Value()
					m.Finished = true
					return m, nil
				}
			}

		case "y":
			if !m.showInputs {
				m.EnableEncryption = true
				m.showInputs = true
				return m, m.passwordInput.Focus()
			}

		case "n":
			if !m.showInputs {
				m.EnableEncryption = false
				m.Finished = true
				return m, nil
			}

		case "tab", "shift+tab":
			if m.showInputs {
				if m.focusIndex == 0 {
					m.focusIndex = 1
					m.passwordInput.Blur()
					return m, m.confirmInput.Focus()
				} else {
					m.focusIndex = 0
					m.confirmInput.Blur()
					return m, m.passwordInput.Focus()
				}
			}
		}
	}

	var cmd tea.Cmd
	if m.showInputs {
		if m.focusIndex == 0 {
			m.passwordInput, cmd = m.passwordInput.Update(msg)
		} else {
			m.confirmInput, cmd = m.confirmInput.Update(msg)
		}
	}

	return m, cmd
}

func (m EncryptionModel) View() string {
	var content strings.Builder

	content.WriteString(styles.TitleStyle.Render("🔒 Disk Encryption"))
	content.WriteString("\n\n")

	if !m.showInputs {
		info := "LUKS2 encryption protects your data if your device is lost or stolen.\n"
		info += "You will need to enter the password every time you boot."
		content.WriteString(styles.InfoBoxStyle.Render(info))
		content.WriteString("\n\n")

		content.WriteString("Enable disk encryption? (recommended)\n\n")
		content.WriteString(styles.RenderHelp("y", "Yes", "n", "No"))
	} else {
		content.WriteString(styles.LabelStyle.Render("Password:"))
		content.WriteString("\n")
		
		inputStyle := styles.InputStyle
		if m.focusIndex == 0 {
			inputStyle = styles.FocusedInputStyle
		}
		content.WriteString(inputStyle.Render(m.passwordInput.View()))
		content.WriteString("\n\n")

		content.WriteString(styles.LabelStyle.Render("Confirm Password:"))
		content.WriteString("\n")
		
		confirmStyle := styles.InputStyle
		if m.focusIndex == 1 {
			confirmStyle = styles.FocusedInputStyle
		}
		content.WriteString(confirmStyle.Render(m.confirmInput.View()))
		content.WriteString("\n\n")

		if m.passwordInput.Value() != "" && m.confirmInput.Value() != "" {
			if m.passwordInput.Value() != m.confirmInput.Value() {
				content.WriteString(styles.ErrorStyle.Render("✗ Passwords do not match"))
				content.WriteString("\n\n")
			} else {
				content.WriteString(styles.SuccessStyle.Render("✓ Passwords match"))
				content.WriteString("\n\n")
			}
		}

		content.WriteString(styles.RenderHelp("Tab", "Switch fields", "Enter", "Continue"))
	}

	return styles.AppStyle.Render(content.String())
}
