package helper

/**
Simplified OS functions
*/

import (
	"bufio"
	"log"
	"os"
	"syscall"

	"golang.org/x/term"
)

// FolderExists returns true if a folder exists
func FolderExists(folder string) bool {
	_, err := os.Stat(folder)
	if err == nil {
		return true
	}
	return !os.IsNotExist(err)
}

// FileExists returns true if a file exists
func FileExists(filename string) (bool, error) {
	info, err := os.Stat(filename)
	if os.IsNotExist(err) {
		return false, nil
	}
	if err != nil {
		return false, err
	}
	return !info.IsDir(), nil
}

// CreateDir creates the data folder if it does not exist
func CreateDir(name string) {
	if !FolderExists(name) {
		err := os.Mkdir(name, 0770)
		Check(err)
	}
}

// ReadLine reads a line from the terminal and returns it as a string
func ReadLine() string {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	text := scanner.Text()
	return text
}

// ReadPassword reads a line without displaying input from the terminal and returns it as a string
func ReadPassword() string {
	// int conversion is required for Windows systems
	pw, err := term.ReadPassword(int(syscall.Stdin))
	if err == nil {
		return string(pw)
	}
	return ReadLine()
}

// Check panics if err is not nil
func Check(err error) {
	if err != nil {
		panic(err)
	}
}

// CheckIgnoreTimeout panics if err is not nil and not a timeout
func CheckIgnoreTimeout(err error) {
	if err == nil {
		return
	}
	if os.IsTimeout(err) {
		log.Println(err)
		return
	}
	Check(err)
}

// IsInArray returns true if value is in array
func IsInArray(haystack []string, needle string) bool {
	for _, item := range haystack {
		if needle == item {
			return true
		}
	}
	return false
}

// GetFileSize returns the file size in bytes
func GetFileSize(file *os.File) (int64, error) {
	fileInfo, err := file.Stat()
	if err != nil {
		return 0, err
	}
	return fileInfo.Size(), nil
}
