package shell

import (
	"fmt"
	"os"

	"go.wit.com/log"
)

// not sure why I wrote this or what it is for
// this is because I'm crazy. how crazy you ask? fucking crazy!
// hehe. If you haven't ever written code that you look back
// at like this and go 'what the fuck is this shit', then
// realize that you wrote it, then go 'what the fuck is this shit'
// and then still look at it and wonder, "how early was this. how much did I not know when I wrote this"
// then, if you haven't done those kinds of things, then don't
// ever fucking come up to me and tell me that I'm nuts
// because, you are not as good as me then. It's very complicated
// to work on very complicated things. I don't care how smart you are,
// you can totally forget about shit you wrote and then have to come back to it later

// also, what the fuck was I thinking with the function 'scanToParent()' as a function that takes
// a pid arg? what? is this like pstree? I'm not sure what I wanted this for
// but it sounds interesting.
func scanToParent(pid int) (bool, string) {
	ppid, err := GetPPID(pid)
	if err != nil {
		fmt.Println("Error getting PPID:", err)
		return true, ""
	}

	comm, err := GetComm(ppid)
	if err != nil {
		fmt.Println("Error getting comm:", err)
		return true, ""
	}

	log.Info("scanToParent() found:", comm)

	// if this is an xterm
	switch comm {
	case "xterm":
		return true, "xterm"
	case "mate-terminal":
		return true, "mate-terminal"
	case "bash":
		// keep digging for the parent xterm
		return scanToParent(ppid)
	case "make":
		// keep digging for the parent xterm
		return scanToParent(ppid)
	}
	if comm == "bash" {
	}

	ok, better := scanToParent(ppid)
	if ok {
		return ok, better
	}
	if better == "" {
		// if the parent was blank, at least return the something that we have
		// since something is better than nothing
		return false, comm
	}
	return ok, better
}

// returns a string of the xterm the user is using
// for example, "xterm" or "mate-terminal"
func Terminal() string {
	pid := os.Getpid()
	_, comm := scanToParent(pid)
	/*
		ppid, err := GetPPID(pid)
		if err != nil {
			fmt.Println("Error getting PPID:", err)
			return ""
		}

		comm, err := GetComm(ppid)
		if err != nil {
			fmt.Println("Error getting comm:", err)
			return ""
		}
	*/

	return comm
}

// returns a string of the shell the user is using
func Shell() string {
	envsh := os.Getenv("SHELL")
	switch envsh {
	case "/bin/bash":
		return "bash"
	}
	return envsh
}

func getXtermCmd(cmd []string) []string {
	var term string
	// doesn't work yet
	// term = Terminal()
	term = "xterm"
	switch term {
	case "mate-terminal":
		newcmd := []string{"mate-terminal", "-e"}
		newcmd = append(newcmd, cmd...)
		return newcmd
	default:
		// unknown terminal. use xterm
		newcmd := []string{"xterm", "-geometry", "140x32", "-e", "bash", "-c"}
		// fix/shell escape sequence this for quote chars, etc
		// tmp := "\"" + strings.Join(cmd, " ") + ";bash\""
		newcmd = append(newcmd, cmd...)
		// newcmd = append(newcmd, cmd...)
		return newcmd
	}
}

// spawns an xterm with something you can run at a command line
func XtermCmd(path string, cmd []string) {
	go XtermCmdWait(path, cmd)
}

// runs an xterm
// waits until xterm exits
func XtermCmdWait(path string, cmd []string) {
	// var argsXterm = getXtermCmd(cmd)

	log.Info("XtermCmd() path =", path, "cmd =", cmd)

	// keeps git diff from exiting on small diffs
	os.Setenv("LESS", "-+F -+X -R")

	PathRunLog(path, cmd, INFO)
}

/*
// spawns an xterm with something you can run at a command line
// then executes bash
func XtermCmdBash(path string, cmd []string) {
	var tmp []string
	var argsXterm = getXtermCmd(cmd)
	bash := "\"-c '"
	bash += strings.Join(cmd, " ")
	bash += "'; bash\""
	tmp = append(argsXterm, "bash", bash)
	log.Info("XtermCmd() path =", path, "cmd =", tmp)
	go PathRunLog(path, tmp, INFO)
}
*/