package main

import (
	"fmt"
	"os"
	"strings"

	prompt "github.com/c-bata/go-prompt"
	"github.com/deranjer/ForlornProjectManager/engine"
)

func executor(in string) {
	in = strings.TrimSpace(in)

	blocks := strings.Split(in, " ")
	switch blocks[0] {
	case "exit":
		fmt.Println("Exiting!")
		os.Exit(0)

	case "rebuild":
		if len(blocks) > 1 {
			fmt.Println("Subcommand is: ", blocks[1])
			switch blocks[1] {
			case "--rider":
				fmt.Println("Enabling rider plugin when cleaning")
				engine.RebuildProject(true, true)
			}
		} else {
			fmt.Println("Cleaning project with default settings...")
			engine.RebuildProject(false, true)
		}
	case "clean":
		if len(blocks) > 1 {
			fmt.Println("Flag is: ", blocks[1])
			switch blocks[1] {
			case "--rider":
				fmt.Println("Enabling rider plugin when cleaning")
				engine.RebuildProject(true, false)
			}
		} else {
			fmt.Println("Cleaning project with default settings...")
			engine.RebuildProject(false, false)
		}
	case "help":
		fmt.Println("Use 'clean' or 'rebuild' with the optional flag '--rider' if you need the rider plugin enabled.")
		fmt.Println("Example Usage: 'clean --rider' or 'rebuild --rider'.")
	default:
		fmt.Println("Sorry, I don't understand")
	}
}

func completer(in prompt.Document) []prompt.Suggest {
	s := []prompt.Suggest{
		{Text: "rebuild", Description: "cleans the project folder and regenerates all files, then builds"},
		{Text: "clean", Description: "only cleans the project folder and regenerates files, no build"},
		{Text: "help", Description: "Shows the help for Unreal Project Manager"},
		{Text: "exit", Description: "Exit the program"},
		{Text: "--rider", Description: "FLAG: Enables the rider plugin after 'clean' or 'rebuild'"},
	}
	return prompt.FilterHasPrefix(s, in.GetWordBeforeCursor(), true)
}

func main() {
	fmt.Println("Welcome to the Unreal Project Manager, run 'clean' to run a basic clean/regenerate or 'rebuild' to do a full rebuild.")
	fmt.Println("Make sure Unreal and Visual Studio are closed, this cannot run if they are open.")
	fmt.Println("NOTE: By default the rider plugin is disabled on rebuild user '--rider' flag to enable it on build or clean")
	fmt.Println("Type 'help' to display the full information")
	p := prompt.New(
		executor,
		completer,
		prompt.OptionPrefix(">>> "),
		//prompt.OptionParser()
	)
	p.Run()
}
