package main

import (
	"fmt"
	"strconv"
	"strings"

	"github.com/manifoldco/promptui"
)

func showMainMenu(cfg *Config) (string, error) {
	hasSettings := len(cfg.JupiterApiKeys) > 0

	items := []string{"Settings", "Start", "Exit"}
	if !hasSettings {
		items = []string{"Settings (configure API keys first)", "Exit"}
	}

	mainMenu := promptui.Select{
		Label: "Main Menu",
		Items: items,
		Templates: &promptui.SelectTemplates{
			Label:    "{{ . }}",
			Active:   "▸ {{ . | cyan }}",
			Inactive: "  {{ . }}",
			Selected: "{{ . | green }}",
		},
	}

	idx, _, err := mainMenu.Run()
	if err != nil {
		return "", err
	}

	if !hasSettings {
		if idx == 0 {
			return "settings", nil
		}
		return "exit", nil
	}

	switch idx {
	case 0:
		return "settings", nil
	case 1:
		return "start", nil
	case 2:
		return "exit", nil
	default:
		return "exit", nil
	}
}

func showSettingsMenu(cfg *Config) error {
	for {
		items := []string{
			fmt.Sprintf("Jupiter API Keys (%d configured)", len(cfg.JupiterApiKeys)),
			fmt.Sprintf("Threads: %d", cfg.Threads),
			"",
			fmt.Sprintf("Solana Check: %s", tern(cfg.EnableSolanaCheck, "✓ enabled", "✗ disabled")),
			fmt.Sprintf("Axiom Module: %s", tern(cfg.EnableAxiomModule, "✓ enabled", "✗ disabled")),
			fmt.Sprintf("EVM Module: %s", tern(cfg.EnableEVMModule, "✓ enabled", "✗ disabled")),
			"",
			"Back",
		}

		settingsMenu := promptui.Select{
			Label: "Settings",
			Items: items,
			Templates: &promptui.SelectTemplates{
				Label:    "{{ . }}",
				Active:   "▸ {{ . | cyan }}",
				Inactive: "  {{ . }}",
				Selected: "{{ . | green }}",
			},
		}

		idx, _, err := settingsMenu.Run()
		if err != nil {
			return err
		}

		switch idx {
		case 0:
			if err := configureJupiterKeys(cfg); err != nil {
				return err
			}
		case 1:
			if err := configureThreads(cfg); err != nil {
				return err
			}
		case 2:
			continue
		case 3:
			cfg.EnableSolanaCheck = !cfg.EnableSolanaCheck
			saveConfig(cfg)
		case 4:
			cfg.EnableAxiomModule = !cfg.EnableAxiomModule
			saveConfig(cfg)
		case 5:
			cfg.EnableEVMModule = !cfg.EnableEVMModule
			saveConfig(cfg)
		case 6:
			continue
		case 7:
			return nil
		}
	}
}

func configureJupiterKeys(cfg *Config) error {
	prompt := promptui.Prompt{
		Label:   "Jupiter API Keys (comma-separated)",
		Default: strings.Join(cfg.JupiterApiKeys, ","),
	}

	result, err := prompt.Run()
	if err != nil {
		return err
	}

	if result == "" {
		cfg.JupiterApiKeys = []string{}
	} else {
		keys := strings.Split(result, ",")
		for i := range keys {
			keys[i] = strings.TrimSpace(keys[i])
		}
		cfg.JupiterApiKeys = keys
	}

	return saveConfig(cfg)
}

func configureThreads(cfg *Config) error {
	prompt := promptui.Prompt{
		Label:   "Threads",
		Default: strconv.Itoa(cfg.Threads),
		Validate: func(s string) error {
			_, err := strconv.Atoi(s)
			return err
		},
	}

	result, err := prompt.Run()
	if err != nil {
		return err
	}

	cfg.Threads, _ = strconv.Atoi(result)
	return saveConfig(cfg)
}

func printConfigSummary(cfg *Config) {
	colorPrint(ColorCyan, "Jupiter API keys:    ")
	fmt.Printf("%d\n", len(cfg.JupiterApiKeys))

	colorPrint(ColorCyan, "Threads:             ")
	fmt.Printf("%d\n", cfg.Threads)
}
