package main

import (
	"bufio"
	"fmt"
	"os"
	"path/filepath"
	"strings"
)

// runGuided is what happens when somebody double-clicks CleanGallery 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 CleanGallery 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.
func runGuided() {
	in := bufio.NewScanner(os.Stdin)

	fmt.Println()
	fmt.Println("  CleanGallery")
	fmt.Println("  Find out how much space duplicate files are wasting.")
	fmt.Println()
	fmt.Println("  Point it at a folder — a shared drive, a user's documents, a photo")
	fmt.Println("  library — and it works out which files are byte-for-byte identical")
	fmt.Println("  copies of each other, and how much room you would get back.")
	fmt.Println()
	fmt.Println("  It only reads. Nothing is moved, changed or deleted, ever.")
	fmt.Println()

	suggested := suggestedFolder()
	for {
		fmt.Println("  Which folder shall I audit?")
		if suggested != "" {
			fmt.Printf("  (press Enter for %s)\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 folder to look at. 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("  Working. Every file has to be read to be sure it really is a")
		fmt.Println("  duplicate, so a large folder can take a few minutes.")
		fmt.Println()
		cmdAudit([]string{answer})
		break
	}

	fmt.Println()
	fmt.Println("  Done. That is a report, not a change — everything listed is still")
	fmt.Println("  exactly where it was.")
	fmt.Println("  There is a command-line version too, which can audit several")
	fmt.Println("  folders at once and export the report: cleangallery --help")
	pause(in)
}

// suggestedFolder offers somewhere worth auditing 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 ""
	}
	// Downloads and Pictures are where duplicate copies pile up fastest.
	for _, name := range []string{"Downloads", "Pictures"} {
		candidate := filepath.Join(home, name)
		if info, err := os.Stat(candidate); err == nil && info.IsDir() {
			return candidate
		}
	}
	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()
}
