package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

// runGuided is what happens when somebody double-clicks the program instead of
// typing its name at a prompt.
//
// Without this, Explorer opens a console, main() finds no arguments, prints
// the usage text to stderr and exits — and Windows destroys the window in the
// same instant. From the other side of the screen that is indistinguishable
// from a crash. So when we know we were double-clicked, we ask the one
// question the program actually needs and stay on screen until the reader is
// done.
//
// This path is entered ONLY when there are no arguments and both ends of the
// program are a real console. Any scripted or piped use takes exactly the same
// code path it always did.
//
// Guided mode runs "status", which measures and prints. It does not create or
// append to a history file: starting a log is a decision about where that log
// lives, and that belongs on the command line.
func runGuided() {
	in := bufio.NewScanner(os.Stdin)

	fmt.Println()
	fmt.Println("  DevicePulse")
	fmt.Println("  A quick reading of how this machine is doing right now.")
	fmt.Println()
	fmt.Println("  It reports how many processor cores this machine has, how much")
	fmt.Println("  memory this program itself is using, and exactly how much space a")
	fmt.Println("  folder you choose is taking up on disk.")
	fmt.Println()
	fmt.Println("  It only measures. No file is written, changed or deleted — not")
	fmt.Println("  even a log.")
	fmt.Println()

	suggested := suggestedFolder()
	for {
		fmt.Println("  Which folder shall I measure?")
		if suggested != "" {
			fmt.Printf("  (press Enter for %s)\n", suggested)
		}
		fmt.Print("  > ")

		if !in.Scan() {
			// stdin closed on us; there is nothing sensible left to ask.
			return
		}
		answer := strings.TrimSpace(in.Text())
		answer = strings.Trim(answer, `"`)
		if answer == "" {
			answer = suggested
		}
		if answer == "" {
			fmt.Println()
			fmt.Println("  I need a folder to measure. Try again, or close this window.")
			fmt.Println()
			continue
		}

		info, err := os.Stat(answer)
		switch {
		case err != nil:
			fmt.Println()
			fmt.Printf("  I cannot find %q.\n", answer)
			fmt.Println("  Tip: you can drag a folder from Explorer onto this window to")
			fmt.Println("  paste its location, then press Enter.")
			fmt.Println()
			continue
		case !info.IsDir():
			fmt.Println()
			fmt.Printf("  %q is a file, not a folder. Give me the folder it sits in.\n", answer)
			fmt.Println()
			continue
		}

		fmt.Println()
		fmt.Println("  Measuring. On a large folder this can take a minute.")
		fmt.Println()
		cmdStatus([]string{"--watch", answer})
		break
	}

	fmt.Println()
	fmt.Println("  Done. That is one reading, taken just now.")
	fmt.Println("  To keep a history and be warned when a folder starts growing")
	fmt.Println("  faster than usual, there is a command-line version:")
	fmt.Println("  devicepulse help")
	pause(in)
}

// suggestedFolder offers somewhere worth measuring that is certain to exist,
// so the reader can get a useful answer by pressing one key.
func suggestedFolder() string {
	home, err := os.UserHomeDir()
	if err != nil {
		return ""
	}
	// The home directory is the honest default here: DevicePulse watches
	// whatever tree the reader cares about growing, and that is usually
	// everything they own rather than one named sub-folder.
	return home
}

// pause keeps the console window open. Explorer closes it the moment the
// process exits, so without this the reader never sees the output.
func pause(in *bufio.Scanner) {
	fmt.Println()
	fmt.Print("  Press Enter to close this window. ")
	in.Scan()
}
