package config

import (
	"fmt"
	"kode-starter/utils"

	"github.com/spf13/cobra"
)

type Config struct {
	DebugMode bool
	LogLevel  string
}

var author string = "Bruno Charest"

var modifDate string = "2023-06-12"

var ConfigFileYmlName string = "config.yml"

var tokenEnv string = utils.GetGitToken()

var verboseFlag bool = false

var jsonFlag bool = false

var versionFlag bool = false

var helpFlag bool = false

// variables for URL
// -----------------
var urlBase string = "https://git.bcmaison.cf"

var urlApiBase string = urlBase + "/api/v1"

var urlApiOrgs string = urlApiBase + "/orgs"

// variables for list command
// --------------------------
var listOrganization string

var listOrganizationFlag bool = false

// variables for create command
// ----------------------------
var createName string

var createOrganisation string

var createDescription string

var createPrivatge string

// variables for env command
// -------------------------
var EnvVariablesFlag bool = false

var envVariablesFilterFlag bool = false

var ennVariablesListFlag bool = false

var envVariablesFilter string

var envVariablesYmlFlag bool = false

var envVariablesCategoryFlag bool = false

var envVariablesCategory string

var envVariablesSetFlag bool = false

// general functions
// -----------------
func GetAuthor() string {
	return author
}

func GetBuildDate() string {
	return modifDate
}

func GetConfigFileName() string {
	return ConfigFileYmlName
}

func GetToken() string {
	return tokenEnv
}

func SetToken(token string) {
	tokenEnv = token
}

func GetJsonFlag() bool {
	return jsonFlag
}

func SetJsonFlag(flag bool) {
	jsonFlag = flag
}

func GetUrlBase() string {
	return urlBase
}

func GetUrlApiBase() string {
	return urlApiBase
}

func GetUrlApiOrgs() string {
	return urlApiOrgs
}

func GetVerboseFlag() bool {
	return verboseFlag
}

func SetVerboseFlag(flag bool) {
	verboseFlag = flag
}

func SetVersionFlag(flag bool) {
	versionFlag = flag
}

func GetVersionFlag() bool {
	return versionFlag
}

func SetHelpFlag(flag bool) {
	helpFlag = flag
}

func GetHelpFlag() bool {
	return helpFlag
}

// functions for list command
// --------------------------
func SetListOrganisation(name string) {
	listOrganization = name
}

func GetListOrganisation() string {
	return listOrganization
}

func SetListOrganisationFlag(flag bool) {
	listOrganizationFlag = flag
}

func GetListOrganisationFlag() bool {
	return listOrganizationFlag
}

// functions for create command
// ----------------------------
func GetCreateName() string {
	return createName
}

func SetCreateName(name string) {
	createName = name
}

func GetCreateOrganisation() string {
	return createOrganisation
}

func SetCeateOrganisation(organisation string) {
	createOrganisation = organisation
}

func GetCreateDescription() string {
	return createDescription
}

func SetCeateDescription(description string) {
	createDescription = description
}

func GetCreatePrivate() string {
	return createPrivatge
}

func SetCreatePrivate(private string) {
	createPrivatge = private
}

// function for env command
// ------------------------
func GetEnvVariablesFlag() bool {
	return EnvVariablesFlag
}

func SetEnvVariablesFlag(flag bool) {
	EnvVariablesFlag = flag
}

func GetEnvVariablesFilterFlag() bool {
	return envVariablesFilterFlag
}

func SetEnvVariablesFilterFlag(flag bool) {
	envVariablesFilterFlag = flag
}

func GetEnvVariablesFilter() string {
	return envVariablesFilter
}

func SetEnvVariablesFilter(filter string) {
	envVariablesFilter = filter
}

func GetEnvVariablesListFlag() bool {
	return ennVariablesListFlag
}

func SetEnvVariablesListFlag(flag bool) {
	ennVariablesListFlag = flag
}

func GetEnvVariablesYmlFlag() bool {
	return envVariablesYmlFlag
}

func SetEnvVariablesYmlFlag(flag bool) {
	envVariablesYmlFlag = flag
}

func GetEnvVariablesCategoryFlag() bool {
	return envVariablesCategoryFlag
}

func SetEnvVariablesCategoryFlag(flag bool) {
	envVariablesCategoryFlag = flag
}

func GetEnvVariablesCategory() string {
	return envVariablesCategory
}

func SetEnvVariablesCategory(category string) {
	envVariablesCategory = category
}

func GetEnvVariablesSetFlag() bool {
	return envVariablesSetFlag
}

func SetEnvVariablesSetFlag(flag bool) {
	envVariablesSetFlag = flag
}

// SetInformations - set informations from command line
func SetInformations(cmd *cobra.Command, args []string) {

	// Check if the version flag is set
	if cmd.Flags().Changed("version") || cmd.Flags().Changed("V") {
		SetVersionFlag(true)
	}

	// Check if the verbose flag is set
	if cmd.Flags().Changed("verbose") || cmd.Flags().Changed("v") {
		SetVerboseFlag(true)
	} else {
		SetVerboseFlag(false)
	}

	// Check if the json flag is set
	if cmd.Flags().Changed("json") || cmd.Flags().Changed("j") {
		SetJsonFlag(true)
	} else {
		SetJsonFlag(false)
	}

	// check if the help flag is set
	if cmd.Flags().Changed("help") || cmd.Flags().Changed("h") {
		SetHelpFlag(true)
	}

	if cmd.Flags().Changed("name") || cmd.Flags().Changed("n") {
		SetCreateName(cmd.Flag("name").Value.String())
	}

	if cmd.Flags().Changed("org") || cmd.Flags().Changed("o") {
		if cmd.Name() == "list" {
			SetListOrganisation(cmd.Flag("org").Value.String())
			SetListOrganisationFlag(true)
		} else {
			SetCeateOrganisation(cmd.Flag("org").Value.String())
		}

	}

	if cmd.Flags().Changed("desc") || cmd.Flags().Changed("d") {
		SetCeateDescription(cmd.Flag("desc").Value.String())
	}

	if cmd.Flags().Changed("private") || cmd.Flags().Changed("p") {
		SetCreatePrivate(cmd.Flag("private").Value.String())
	}

	// set env variables flag if set
	if cmd.Flags().Changed("env") || cmd.Flags().Changed("e") {
		SetEnvVariablesFlag(true)

		// if flag list is set, set filter flag
		if cmd.Flags().Changed("list") || cmd.Flags().Changed("l") {
			SetEnvVariablesListFlag(true)

			// set env variables filter flag if set
			if cmd.Flags().Changed("filter") || cmd.Flags().Changed("f") {
				SetEnvVariablesFilter(cmd.Flag("filter").Value.String())
			}

		} else if cmd.Flags().Changed("yml") || cmd.Flags().Changed("y") {
			SetEnvVariablesYmlFlag(true)

			// set env variables category flag if set
			if cmd.Flags().Changed("category") || cmd.Flags().Changed("c") {
				SetEnvVariablesCategoryFlag(true)
				SetEnvVariablesCategory(cmd.Flag("category").Value.String())
			}

		} else if cmd.Flags().Changed("set") || cmd.Flags().Changed("s") {
			SetEnvVariablesSetFlag(true)
		}

	}

	// If verbose flag is set, print informations
	if GetVerboseFlag() {
		if cmd.Name() == "list" {
			fmt.Println("SetInformations - progress list option:")
			fmt.Println("SetInformations - org:" + cmd.Flag("org").Value.String())
			fmt.Println("SetInformations - cmd Usertoken:" + cmd.Flag("token").Value.String())
		}
		if cmd.Name() == "create" {
			fmt.Println("SetInformations - progress create option:")
			fmt.Println("SetInformations - name:" + cmd.Flag("name").Value.String())
			fmt.Println("SetInformations - description:" + cmd.Flag("desc").Value.String())
			fmt.Println("SetInformations - private:" + cmd.Flag("private").Value.String())
			fmt.Println("SetInformations - cmd Usertoken:" + cmd.Flag("token").Value.String())
			fmt.Println("SetInformations - org:" + cmd.Flag("org").Value.String())
		}
		if cmd.Name() == "env" {
			fmt.Println("SetInformations - progress env option:")
			// fmt.Println("SetInformations - cmd Usertoken:" + cmd.Flag("token").Value.String())
			fmt.Println("SetInformations - env:" + cmd.Flag("env").Value.String())
			fmt.Println("SetInformations - filter:" + cmd.Flag("filter").Value.String())
			fmt.Println("SetInformations - list:" + cmd.Flag("list").Value.String())
			fmt.Println("SetInformations - yml:" + cmd.Flag("yml").Value.String())
			if GetEnvVariablesListFlag() {
				fmt.Println("SetInformations - category:" + cmd.Flag("category").Value.String())
			}
			fmt.Println("SetInformations - set:" + cmd.Flag("set").Value.String())
		}
	}

	// Check if token come from flag or env
	if cmd.Flags().Changed("token") || cmd.Flags().Changed("t") {
		if utils.IsValidToken(cmd.Flag("token").Value.String()) {
			SetToken(cmd.Flag("token").Value.String())
		} else {
			utils.ExitWithError(10, "Invalid token, format must be 40 characters UUID.")
		}

	} else {
		if utils.IsValidToken(utils.GetGitToken()) {
			SetToken(utils.GetGitToken())
		} else {
			utils.ExitWithError(10, "Invalid token, format must be 40 characters UUID.")
		}
	}
}
