package main

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

// runGuided is what happens when somebody double-clicks CleanVault instead of
// typing its name at a prompt.
//
// Explorer opens a console, runs the program with no arguments, and destroys
// the window the instant the process exits — so printing usage and quitting
// looks exactly like a crash. When we know we were double-clicked we ask the
// one thing CleanVault needs and stay on screen until the reader is done.
//
// The guided session only ever LOOKS. It runs the read-only scan and nothing
// else: no file is moved into quarantine here, whatever the scan turns up.
// Quarantining stays where it belongs, on the command line, where the operator
// has to name a quarantine folder and ask for it explicitly.
func runGuided() {
	in := bufio.NewScanner(os.Stdin)

	fmt.Println()
	fmt.Println("  CleanVault")
	fmt.Println("  See what your browsers and apps have quietly kept about you.")
	fmt.Println()
	fmt.Println("  It knows where the traces live — browsing history, cookies, caches,")
	fmt.Println("  saved sessions, thumbnails, recent-file lists — and it checks which")
	fmt.Println("  of those places exist on this machine, how big they are, and how")
	fmt.Println("  revealing they are.")
	fmt.Println()
	fmt.Println("  This is a look, not a clean-up. Nothing is moved, changed or deleted.")
	fmt.Println()

	suggested := suggestedHome()
	for {
		fmt.Println("  Whose home folder shall I check?")
		if suggested != "" {
			fmt.Printf("  (press Enter for %s — your own)\n", suggested)
		}
		fmt.Println("  Tip: you can drag a folder from Explorer onto this window to")
		fmt.Println("  paste its location, then press Enter.")
		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 home folder to look in. 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()
			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("  Looking. This checks every place in the catalog and adds up what")
		fmt.Println("  is actually there.")
		fmt.Println()
		if err := cmdScan([]string{answer}); err != nil {
			fmt.Println()
			fmt.Printf("  I could not finish that scan: %v\n", err)
		}
		break
	}

	fmt.Println()
	fmt.Println("  Done. Everything listed above is still exactly where it was — this")
	fmt.Println("  session only reads.")
	fmt.Println("  Moving traces into a quarantine folder is a separate, deliberate step")
	fmt.Println("  on the command line, and even there CleanVault never deletes: run")
	fmt.Println("  cleanvault --help to see how.")
	pause(in)
}

// suggestedHome offers the reader's own home folder, which is the answer
// almost everybody wants and is certain to exist when it can be found at all.
func suggestedHome() string {
	home, err := os.UserHomeDir()
	if err != nil {
		return ""
	}
	if info, err := os.Stat(home); err != nil || !info.IsDir() {
		return ""
	}
	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()
}
