/*
 * MIT License
 * Author: Mark Allyn
 *
 * scope_marine_ppi.cpp — Marine PPI scope implementation.
 */

#include "scope_marine_ppi.h"
#include <GLFW/glfw3.h>
#include <cstring>

// ----------------------------------------------------------------

float MarinePPIScope::maxRangeM() const {
    return MARINE_RANGE_STEPS[maxRangeIdx_] * MILES_TO_METERS;
}

void MarinePPIScope::computeRingRadii() {
    int n = MARINE_RING_COUNT[maxRangeIdx_];
    float maxMi = MARINE_RANGE_STEPS[maxRangeIdx_];
    for (int i = 0; i < n; ++i)
        ringRadiiNorm_[i] = MARINE_RING_MILES[maxRangeIdx_][i] / maxMi;
}

void MarinePPIScope::handleKeyRange(int key, int /*action*/) {
    if (key == GLFW_KEY_U) {
        if (maxRangeIdx_ < MARINE_RANGE_STEP_COUNT - 1) {
            ++maxRangeIdx_;
            computeRingRadii();
        }
    } else if (key == GLFW_KEY_D) {
        if (maxRangeIdx_ > 0) {
            --maxRangeIdx_;
            computeRingRadii();
        }
    }
}

// ----------------------------------------------------------------

std::vector<std::string> MarinePPIScope::panelLines() const {
    return {
        "MARINE PPI SCOPE",
        "",
        "The PPI (Plan Position Indicator) became the",
        "standard radar display for ships from the late",
        "1950s onward. The antenna rotates clockwise and",
        "the sweep line rotates with it, painting a map",
        "of everything within range.",
        "",
        "Targets glow bright blue the instant the sweep",
        "passes over them, then fade through green to",
        "yellow before the sweep returns. This is P7",
        "phosphor persistence.",
        "",
        "The dotted range rings give distance reference.",
        "The incandescent bearing scale shows True",
        "direction (0 = North, clockwise to 359).",
        "",
        "The yellow overlay is the mechanical cursor.",
        "Use cursor keys to position it over a target;",
        "range and bearing read out below the scope.",
        "",
        "Location: Bellingham Bay, WA",
        "(Fictitious 100-ft mid-bay platform)",
        "Targets: Simulated vessels.",
        "",
        "Press s to advance.  Press S to go back.",
        "Auto-advance in 120 sec. Any key resets timer.",
        "  Next: Exhibit Introduction -->",
        "",
        "+------+----------------------------------+",
        "| KEY  | FUNCTION                         |",
        "+------+----------------------------------+",
        "| s/S  | Next / previous scope            |",
        "| u/d  | Max range up/down (2, 4, 6 mi)   |",
        "| r/l  | Cursor bearing right / left      |",
        "| t/y  | Cursor range increase / decrease |",
        "| k/j  | Display offset right / left      |",
        "|      | (0=North-up; match heading=Head-up)|",
        "| 1/2  | Gain increase / decrease         |",
        "| 3/4  | Rain filter increase / decrease  |",
        "| 5/6  | Wave filter increase / decrease  |",
        "+------+----------------------------------+",
    };
}
