package main

import (
	"kode-starter/cmdCreate"
	"kode-starter/cmdEnv"
	"kode-starter/cmdLists"
	"kode-starter/config"
	"kode-starter/utils"
	"kode-starter/version"

	"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
	Use:   "kode-creator.exe",
	Short: "Simple cli app create startup project",
	Long:  `A simple CLI app to work with Github, Gitea`,
	Run: func(cmd *cobra.Command, args []string) {

		config.SetInformations(cmd, args)
		mainProgram(cmd, args)
	},
}

var token, org, name, private, description, envGroup, envCategory string
var verbose, createFlag, versionFlag bool
var listEnvFlag, listEnvCfgFlag, setEnvFlag, envVariablesFlag, envGroupFlag bool

func init() {

	// var config = &config.Config{}

	// root menu section
	rootCmd.Flags().BoolP("help", "h", false, "Show help for create command")
	rootCmd.Flags().BoolVarP(&versionFlag, "version", "V", false, "Show version")
	rootCmd.CompletionOptions.DisableDefaultCmd = true
	rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "mode verbose")
	rootCmd.PersistentFlags().BoolVarP(&createFlag, "json", "j", false, "Print output as json format")

	// List menu section
	cmdLists.ListCmd.Flags().StringVarP(&token, "token", "t", "", "Github token")
	cmdLists.ListCmd.Flags().StringVarP(&org, "org", "o", "", "Github organization")

	// Create menu section
	cmdCreate.CreateCmd.Flags().StringVarP(&token, "token", "t", "", "Github token")
	cmdCreate.CreateCmd.Flags().StringVarP(&org, "org", "o", "", "Github organization")
	cmdCreate.CreateCmd.Flags().StringVarP(&name, "name", "n", "", "Project name")
	cmdCreate.CreateCmd.Flags().StringVarP(&description, "desc", "d", "", "Description")
	cmdCreate.CreateCmd.Flags().StringVarP(&private, "private", "p", "", "true/false")

	// Env menu section
	cmdEnv.EnvCmd.Flags().BoolVarP(&envVariablesFlag, "env", "e", false, "environnement variables management")
	cmdEnv.EnvCmd.Flags().BoolVarP(&listEnvFlag, "list", "l", false, "List environnement variables")
	cmdEnv.EnvCmd.Flags().StringVarP(&envGroup, "filter", "f", "", "Highlight a specific word in environnement variables")
	cmdEnv.EnvCmd.Flags().BoolVarP(&listEnvCfgFlag, "yml", "y", false, "List environnement variables from config.yml")
	cmdEnv.EnvCmd.Flags().StringVarP(&envCategory, "cat", "c", "", "filter for a specific category")
	cmdEnv.EnvCmd.Flags().BoolVarP(&setEnvFlag, "set", "s", false, "set environnement variable available from config.yml")

	// Add subcommands
	rootCmd.AddCommand(cmdLists.ListCmd)
	rootCmd.AddCommand(cmdCreate.CreateCmd)
	rootCmd.AddCommand(cmdEnv.EnvCmd)

}

func mainProgram(cmd *cobra.Command, args []string) {

	config.SetInformations(cmd, args)

	// if version flag is set, print version and exit
	if config.GetVersionFlag() {
		utils.PrintVersion(version.GetFullVersion(), config.GetAuthor(), config.GetBuildDate())
		return

		// if no flag is set, print help and exit
	} else {

		//configFile.Listcategory()
		utils.PrintHelpFormated(version.GetFullVersion(), config.GetAuthor(), config.GetBuildDate(), cmd)
		return
	}

}

func main() {

	// var config = &config.Config{}

	// print header
	utils.PrintHeader()

	// execute root command
	rootCmd.Execute()

}
