package clientcmd

import (
	"fmt"

	clientconfig "github.com/deranjer/gvc/client/clientconfig"
	"github.com/deranjer/gvc/common"
)

//IgnoreFiles ignores file(s)/folder based on name/wildcard, etc
func IgnoreFiles(input string, inputType string, conf *clientconfig.Gvcconfig) error {
	err := validateFileType(input, inputType) // Making sure that if the file flag was used a folder was not supplied and vice versa //TODO: Not needed I don't think we need user to be able to add ignores before files are there
	if err != nil {
		return err
	}
	ignore := conf.Ignores
	switch inputType { // TODO: add default case for generic error handling
	case "file":
		err := common.CheckFileTypes(input, "file", ignore)
		if err != nil {
			return fmt.Errorf("%s already ignored: %s", input, err)
		}
		fmt.Println("Adding file to ignores: ", input)
		conf.Ignores.Files = append(conf.Ignores.Files, input)
		return nil
	case "folder":
		err := common.CheckFileTypes(input, "folder", ignore)
		if err != nil {
			return fmt.Errorf("%s is already ignored: %s", input, err)
		}
		fmt.Println("Adding folder to ignores: ", input)
		conf.Ignores.Folders = append(conf.Ignores.Folders, input)
		return nil
	case "wildcard":
		var wildcard string
		if input[:1] == "*" { // Removing the wildcard char since we don't store that or test with that char
			wildcard = input[1:]
		} else {
			wildcard = input
		}
		err := common.CheckFileTypes(wildcard, "wildcard", ignore)
		if err != nil {
			return fmt.Errorf("%s is already ignored: %s", input, err)
		}
		fmt.Println("Adding wildcard to ignores: ", wildcard)
		conf.Ignores.Exts = append(conf.Ignores.Exts, wildcard)
		return nil
	}
	return fmt.Errorf("This... should not have happened, some kind of internal error on IgnoreFiles function call, switch failure")
}

// RemoveIgnoreFiles removes files/folders/wildcards from the ignore list
func RemoveIgnoreFiles(input string, inputType string, conf *clientconfig.Gvcconfig) error {
	err := validateFileType(input, inputType) // Making sure that if the file flag was used a folder was not supplied and vice versa //TODO: Not needed I don't think we need user to be able to add ignores before files are there
	if err != nil {
		return err
	}
	ignore := conf.Ignores
	switch inputType { // TODO: add default case for generic error handling
	case "file":
		err := common.CheckFileTypes(input, "file", ignore)
		if err != nil {
			fmt.Println("Removing file from ignores: ", input)
			for i, fileIgnore := range ignore.Files {
				if input == fileIgnore {
					conf.Ignores.Files[i] = conf.Ignores.Files[len(conf.Ignores.Files)-1] // Deleting the element
					conf.Ignores.Files = conf.Ignores.Files[:len(conf.Ignores.Files)-1]   // redoing the slice
					fmt.Println("Removing file from ignores: ", input)
					return nil
				}
			}
		}
		fmt.Println("File not found in ingores, unable to remove: ", input)
		return nil
	case "folder":
		err := common.CheckFileTypes(input, "folder", ignore)
		if err != nil {
			for i, folderIgnore := range ignore.Folders {
				if input == folderIgnore {
					conf.Ignores.Folders[i] = conf.Ignores.Folders[len(conf.Ignores.Folders)-1]
					conf.Ignores.Folders = conf.Ignores.Files[:len(conf.Ignores.Folders)-1]
					fmt.Println("Removing folder from ignores: ", input)
					return nil
				}
			}
		}
		fmt.Println("Folder not found in ingores, unable to remove: ", input)
		return nil
	case "wildcard":
		var wildcard string
		if input[:1] == "*" { // Removing the wildcard char since we don't store that or test with that char
			wildcard = input[1:]
		} else {
			wildcard = input
		}
		err := common.CheckFileTypes(wildcard, "wildcard", ignore)
		if err != nil {
			for i, wildcardIgnore := range ignore.Exts {
				if input == wildcardIgnore {
					conf.Ignores.Exts[i] = conf.Ignores.Exts[len(conf.Ignores.Exts)-1]
					conf.Ignores.Exts = conf.Ignores.Exts[:len(conf.Ignores.Exts)-1]
					fmt.Println("Removing wildcard from ignores: ", wildcard)
					return nil
				}
			}
		}
		fmt.Println("Wildcard not found in ingores, unable to remove: ", wildcard)
		return nil
	}
	return fmt.Errorf("This... should not have happened, some kind of internal error on RemoveIgnoreFiles function call, switch failure")
}
