//! # strartrust::stinstr
//!
//! Write the INSTRUCTIONS to the user

use std::io::BufRead;

use termcolor::WriteColor;

use crate::interaction::{clrscr, yesno};
use crate::StResult;

/// Show program title
pub fn show_title<W: WriteColor>(sout: &mut W) -> StResult<()> {
    clrscr(sout)?;
    writeln!(sout, "STAR TREK!!")?;
    writeln!(sout, "===========\n").map_err(|e| e.into()) // The extra '\n' is intentional
} /* End title */

pub fn show_instructions<R: BufRead, W: WriteColor>(sin: &mut R, sout: &mut W) -> StResult<()> {
    write!(sout, "DO YOU NEED INSTRUCTIONS Y/N)? ")?;
    sout.flush()?;
    loop {
        let ans = yesno(sin)?;
        if ans != 'Y' {
            return Ok(());
        }
        clrscr(sout)?;

        write!(sout, "{}", INSTRUCTIONS)?;

        write!(sout, "\n\nREPEAT THESE INSTRUCTIONS (Y/N)?")?;
        sout.flush()?
    }
}

const INSTRUCTIONS: &str = r"   IT IS STARDATE 3421 AND THE FEDERATION IS BEING INVADED
BY A BAND OF KLINGON 'PIRATES' WHOSE OBJECTIVE IS TO TEST
OUR DEFENSES.  IF EVEN ONE SURVIVES THE TRIAL PERIOD,
KLINGON HEADQUARTERS WILL LAUNCH AN ALL-OUT ATTACK.
AS CAPTAIN OF THE FEDERATION STARSHIP 'ENTERPRISE', YOUR
MISSION IS TO FIND AND DESTROY THE INVADERS BEFORE THE TIME
RUNS OUT.

   THE KNOWN GALAXY IS DIVIDED INTO 64 QUADRANTS ARRANGED
LIKE A SQUARE CHECKERBOARD, 8 ON A SIDE.  EACH QUADRANT IS
LIKEWISE DIVIDED INTO 64 SECTORS ARRANGED AS AN 8 BY 8 SQUARE.
EACH SECTOR CAN CONTAIN A KLINGON (K), STAR (*), STARBASE (B),
THE ENTERPRISE HERSELF (E), OR EMPTY SPACE (.).  EACH SECTOR
IS ALSO NUMBERED; A STARBASE IN SECTOR 3-5 IS 3 ROWS DOWN
FROM THE TOP OF THE SHORT RANGE SCAN PRINT-OUT, AND 5 SECTORS
TO THE RIGHT.  DOCKING AT A STARBASE IS DONE BY OCCUPYING
AN ADJACENT SECTOR, AND REPROVISIONS YOUR STARSHIP WITH
ENERGY AND PHOTON TORPEDOES, AS WELL AS REPAIRING ALL DAMAGES.

   YOUR STARSHIP WILL ACT ON THE FOLLOWING COMMANDS:
COMMAND 1 - WARP ENGINE CONTROL IS USED TO MOVE THE ENTERPRISE.
            YOU WILL BE ASKED TO SET THE DISTANCE (MEASURED
            IN WARPS), AND THE COURSE FOR THE MOVE.  EACH
            MOVE THAT YOU MAKE WITH THE ENTERPRISE FROM
            ONE SECTOR TO ANOTHER, OR FROM ONE QUADRANT
            TO ANOTHER, COSTS YOU ONE STARDATE (ONE YEAR).
            THEREFORE, A 30-YEAR GAME MEANS YOU HAVE
            30 MOVES TO WIN IN.

            COURSE - A NUMBER FROM 1 TO          4   3   2
            8.999 INDICATING A DIR-                \ ' /
            ECTION (STARTING WITH                5 - * - 1
            A 1 TO THE RIGHT AND IN-               / ' \
            CREASING COUNTERCLOCKWISE).          6   7   8
            TO MOVE TO THE LEFT, USE A
            COURSE OF 5.  (A COURSE OF 3.5 IS HALFWAY BETWEEN
            3 AND 4; A COURSE OF 8.75 IS THREE-QUARTERS OF THE
            WAY FROM 8 TO 1.)

            WARP - ONE WARP MOVES YOU THE WIDTH OF A QUADRANT.
            A WARP OF .5 WILL MOVE YOU HALFWAY THROUGH A
            QUADRANT; MOVING DIAGONALLY ACROSS A QUADRANT
            TO THE NEXT WILL REQUIRE 1.414 WARPS.  WARP 3
            WILL MOVE YOU 3 QUADRANTS PROVIDING NOTHING
            IN YOUR PRESENT QUADRANT BLOCKS YOUR EXIT.
            ONCE YOU LEAVE THE QUADRANT THAT YOU WERE
            IN, YOU WILL ENTER HYPERSPACE; COMING OUT
            OF HYPERSPACE WILL PLACE YOU RANDOMLY IN
            THE NEW QUADRANT.  KLINGONS IN A GIVEN QUADRANT
            WILL FIRE AT YOU WHENEVER YOU LEAVE, ENTER,
            OR MOVE WITHIN THE QUADRANT.  ENTERING A
            COURSE OR WARP OF ZERO CAN BE USED TO RETURN
            TO THE COMMAND MODE.

COMMAND 2 - A SHORT RANGE SENSOR SCAN WILL PRINT OUT THE
            QUADRANT YOU PRESENTLY OCCUPY SHOWING THE
            CONTENT OF EACH OF THE 64 SECTORS, AS WELL
            AS OTHER PERTINENT INFORMATION.

COMMAND 3 - THE LONG RANGE SENSOR SCAN SUMMARIZES THE QUADRANT
            YOU ARE IN, AND THE ADJOINING ONES.  EACH
            QUADRANT IS REPRESENTED AS A 3-DIGIT NUMBER;
            THE FIRST (HUNDREDS) DIGIT IS THE NUMBER OF
            KLINGONS IN THAT QUADRANT WHILE THE MIDDLE
            DIGIT IS THE NUMBER OF STARBASES, AND THE
            UNITS DIGIT IS THE NUMBER OF STARS.  AN ENTRY
            OF 305 MEANS 3 KLINGONS, NO STARBASES, AND
            5 STARS.

COMMAND 4 - FIRE PHASERS; THE PORTION OF THE ENTERPRISE'S
            ENERGY THAT YOU SPECIFY WILL BE DIVIDED EVENLY 
            AMONG THE KLINGONS IN THE QUADRANT AND FIRED
            AT THEM.  SURVIVING KLINGONS WILL RETALIATE.
            PHASER FIRE BYPASSES STARS AND STARBASES, BUT
            IS ATTENUATED BY THE DISTANCE IT TRAVELS.
            THE ARRIVING ENERGY DEPLETES THE SHIELD POWER
            OF ITS TARGET.  ENERGY IS AUTOMATICALLY DIVERTED
            TO THE SHIELDS AS NEEDED, BUT IF YOU RUN OUT
            OF ENERGY YOU'LL GET FRIED.

COMMAND 5 - PHOTON TORPEDO CONTROL WILL LAUNCH A TORPEDO
            ON A COURSE YOU SPECIFY WHICH WILL DESTROY
            ANY OBJECT IN ITS PATH.  RANGE IS LIMITED TO
            THE LOCAL QUADRANT.  EXPECT RETURN FIRE FROM
            SURVIVING KLINGONS.

COMMAND 6 - THE GALACTIC RECORDS SECTION OF THE SHIP'S
            COMPUTER RESPONDS TO THIS COMMAND BY PRINTING
            OUT A GALACTIC MAP SHOWING THE RESULTS OF ALL
            PREVIOUS SENSOR SCANS.
";
