package main

import (
	"errors"
	"fmt"
	"log"
	"os"

	"gopkg.in/yaml.v3"
)

type GoConfig struct {
	Server struct { // server specific settings
		ServDir   string `yaml:"serverFolder"`
		WorldFile string `yaml:"worldFile"`
		ServCfg   string `yaml:"serverSettings"`
		ServExec  string `yaml:"serverExec"`
		ServPort  int    `yaml:"port"`
	} `yaml:"server"`

	Factoryman struct { // factoryman settings
		BackupDir  string `yaml:"backupDir"`
		UseScreen  bool   `yaml:"screen"`
		ScreenName string `yaml:"screenName"`
		UserName   string `yaml:"username"`
		ApiToken   string `yaml:"apitoken"`
	} `yaml:"factoryman"`
}

func readCfg(factCfg string) GoConfig {
	//read config file (YAML)
	fileBytes, err := os.ReadFile(factCfg)
	if err != nil {
		log.Fatalf("Error reading config file: %v", err)
	}
	// return Struct
	var config GoConfig
	err = yaml.Unmarshal(fileBytes, &config)
	if err != nil {
		log.Fatalf("Error unmarshalling YAML file: %v", err)
	}
	return config
}
func isItReal(path string) bool { //check if path exists
	if _, err := os.Stat(path); err == nil {
		return true
	} else if errors.Is(err, os.ErrNotExist) {
		return false
	} else {
		return false
	}
}
func verifyConfig(config GoConfig) bool { // check each file/dir to see if it exists
	if !isItReal(config.Server.ServDir) {
		fmt.Printf("PATH NOT FOUND: %s", config.Server.ServDir)
		return false
	}
	if !isItReal(config.Server.WorldFile) {
		fmt.Printf("PATH NOT FOUND: %s", config.Server.WorldFile)
		return false
	}
	if !isItReal(config.Server.ServCfg) {
		fmt.Printf("PATH NOT FOUND: %s", config.Server.ServCfg)
		return false
	}
	if !isItReal(config.Server.ServExec) {
		fmt.Printf("PATH NOT FOUND: %s", config.Server.ServExec)
		return false
	}
	if !isItReal(config.Factoryman.BackupDir) {
		fmt.Printf("PATH NOT FOUND: %s", config.Factoryman.BackupDir)
		return false
	}
	return true

}
