package main

import (
	"bufio"
	"fmt"
	"os"
	"path/filepath"
	"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.
//
// SnapBeam's guided run is unusual in the Techlosoft line because the program
// it leads to does not finish: `start` waits. So there is exactly one question
// — where should things be saved — and then the receiver opens and the window
// stays full of the banner, the QR code and every arrival, which is precisely
// what a person who double-clicked wanted to see.
func runGuided() {
	in := bufio.NewScanner(os.Stdin)

	fmt.Println()
	fmt.Println("  SnapBeam")
	fmt.Println("  Get a screenshot or a bit of text off your phone and onto this computer.")
	fmt.Println()
	fmt.Println("  This window becomes a receiver. Leave it open. Anything your phone")
	fmt.Println("  sends lands in a folder here, and text also goes on the clipboard so")
	fmt.Println("  you can paste it straight away.")
	fmt.Println()
	fmt.Println("  Two ways to send from the phone:")
	fmt.Println("    - the LocalSend app, which will find this computer by itself, or")
	fmt.Println("    - the phone's camera, pointed at the square SnapBeam is about to draw.")
	fmt.Println()
	fmt.Println("  Nothing is uploaded anywhere. Your phone talks to this computer over")
	fmt.Println("  your own Wi-Fi and nowhere else. Nothing here is ever deleted.")
	fmt.Println()

	suggested := defaultSaveDir()
	folder := suggested
	for {
		fmt.Println("  Where shall I save what arrives?")
		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
		}

		abs, err := filepath.Abs(answer)
		if err != nil {
			abs = answer
		}
		if info, err := os.Stat(abs); err == nil && !info.IsDir() {
			fmt.Println()
			fmt.Printf("  %q is a file, not a folder. Give me a folder to save into.\n", answer)
			fmt.Println()
			continue
		}
		if err := os.MkdirAll(abs, 0o755); err != nil {
			fmt.Println()
			fmt.Printf("  I cannot create %q: %v\n", abs, err)
			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
		}
		folder = abs
		break
	}

	fmt.Println()
	fmt.Println("  Opening the receiver. Press Ctrl+C, or close this window, to stop.")

	runStart(startOptions{
		Port:      defaultLocalSendPort,
		Discovery: defaultLocalSendPort,
		Dir:       folder,
		Alias:     defaultAlias(),
		MaxBytes:  defaultMaxBytes,
		LocalSend: true,
		QRStyle:   "blocks",
	})

	fmt.Println()
	fmt.Println("  Stopped. Everything that arrived is still in:")
	fmt.Printf("    %s\n", folder)
	fmt.Println()
	fmt.Println("  There is a command-line version too, with more options:")
	fmt.Printf("    %s help\n", appName)
	pause(in)
}

// 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()
}
