package config

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

	"github.com/spf13/cobra"
)

var author string = "Bruno Charest"

var modifDate string = "2023-06-12"

var tokenEnv string = utils.GetGitToken()

var verboseFlag bool = false

var jsonFlag 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

func GetAuthor() string {
	return author
}

func GetBuildDate() string {
	return modifDate
}

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
}

// 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
}

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

	// fmt.Println("SetInformations - start:")

	// 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 cmd come from list or create
	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.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())
	}

	// 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.")
		}
	}
}
