package engine

import (
	"bufio"
	"fmt"
	"os"
	"path/filepath"
)

var version = "4.25" //Need to manually set the UEVersion, don't know how to guess this

// RebuildProject is the main function to clean up the project, generate files and build
func RebuildProject(riderEnabled bool, doBuild bool) {
	_, err := os.Getwd()
	if err != nil {
		fmt.Println("Unable to get working dir, returned error: ", err)
		bufio.NewReader(os.Stdin).ReadBytes('\n')
		os.Exit(1)
	}
	projectFile, err := os.Stat("ForlornOutcast.uproject")
	if err != nil {
		if os.IsNotExist(err) {
			fmt.Println("unable to locate project file, are you in the right directory?")
			bufio.NewReader(os.Stdin).ReadBytes('\n')
			os.Exit(1)
		} else {
			fmt.Println("Unknown error when trying to open ForlornOutcast.uproject... exiting")
		}
	}
	fmt.Println("Found project file... continuing ")
	projectABSPath, err := filepath.Abs("ForlornOutcast.uproject")
	if err != nil {
		fmt.Println("Unable to generate absolute file path to project file: ", err)
		bufio.NewReader(os.Stdin).ReadBytes('\n')
		os.Exit(1)
	}
	checkPath(projectABSPath) // Verifying path, will exit if fail
	fmt.Println("Using Project Absolute Path: ", projectABSPath)
	// Setting the folders to remove //remove.go
	folderRemove := []string{".vs", "Binaries", "Intermediate", "Saved", ".idea"}
	fmt.Println("Removing Folders...")
	err = removeFolders(folderRemove)
	if err != nil {
		fmt.Println(err)
		bufio.NewReader(os.Stdin).ReadBytes('\n')
		os.Exit(1)
	}
	// Setting the files to remove //remove.go
	fileRemove := []string{"ForlornOutcast.sln"}
	fmt.Println("Removing Files...")
	err = removeFiles(fileRemove)
	if err != nil {
		fmt.Println(err)
		bufio.NewReader(os.Stdin).ReadBytes('\n')
		os.Exit(1)
	}
	// Editing the .uproject file //jsonEdit.go
	err = editProjectFile(projectFile, riderEnabled)
	if err != nil {
		fmt.Printf("Error reading/modifying project file: %s", err)
		bufio.NewReader(os.Stdin).ReadBytes('\n')
		os.Exit(1)
	}
	fmt.Println("Project file has been modified as requested")
	// Generating the vs files //generateFiles.go
	err = generateFiles(projectABSPath)
	if err != nil {
		fmt.Println("Unable to generate project files, please generate project files manually: ", err)
		bufio.NewReader(os.Stdin).ReadBytes('\n')
		os.Exit(1)
	}
	fmt.Println("Project Files should have been generated...")
	if !doBuild {
		fmt.Println("Finished, press <enter> to exit!")
		bufio.NewReader(os.Stdin).ReadBytes('\n')
		os.Exit(1)
	}
	// Building the project //compile.go
	err = buildProject(version, projectABSPath)
	if err != nil {
		fmt.Println("Unable to build project, please build the project manually: ", err)
		bufio.NewReader(os.Stdin).ReadBytes('\n')
		os.Exit(1)
	}
	fmt.Println("Finished, press <enter> to exit!")
	bufio.NewReader(os.Stdin).ReadBytes('\n')
	os.Exit(1)
}

// checkPath is a generic function to just verify file exists
func checkPath(path string) {
	_, err := os.Stat(path)
	if err != nil {
		fmt.Printf("Path for file %s does not appear to be correct, error: %s exiting...", path, err)
		bufio.NewReader(os.Stdin).ReadBytes('\n')
		os.Exit(1)
	}
}
