/**
 * California at corridor scale: Los Angeles to San Francisco.
 *
 * This is deliberately sparse. San Francisco and Southern California remain
 * the detailed boards; this one is the connective tissue between them. A
 * roughly two-kilometre height cell and a handful of range-scale hills keep the
 * state readable without pretending a 600 km drive is one city mesh.
 */

import CALIFORNIA_TRANSPORT from "../transport/california.ts";
import type { City, LatLng } from "../engine/types.ts";

function routePath(routeId: string): LatLng[] {
  const route = CALIFORNIA_TRANSPORT.routes.find((candidate) => candidate.id === routeId);
  if (!route) throw new Error(`california: missing transport route "${routeId}"`);
  const segments = new Map(CALIFORNIA_TRANSPORT.segments.map((segment) => [segment.id, segment]));
  const nodes = new Map(CALIFORNIA_TRANSPORT.nodes.map((node) => [node.id, node]));
  const points: LatLng[] = [];
  for (const id of route.segmentIds) {
    const segment = segments.get(id);
    if (!segment) continue;
    const from = nodes.get(segment.fromNodeId)?.position;
    const to = nodes.get(segment.toNodeId)?.position;
    if (!from || !to) continue;
    if (points.length === 0) points.push([from.lat, from.lng]);
    points.push([to.lat, to.lng]);
  }
  return points;
}

export const CALIFORNIA_US_101 = routePath("la-sf-us-101");
export const CALIFORNIA_I_5 = routePath("la-sf-i-5");

/**
 * Original, hand-authored coast silhouette for this coarse board. It is a
 * visual boundary, not survey data; the eastern and northern edges close well
 * outside the route so the heightfield's coastal falloff stays off the road.
 */
export const CORRIDOR_LAND: LatLng[] = [
  [38.2, -123.35],
  [37.92, -122.74],
  [37.79, -122.5],
  [37.45, -122.43],
  [37.08, -122.28],
  [36.62, -121.94],
  [36.15, -121.7],
  [35.65, -121.23],
  [35.18, -120.85],
  [34.72, -120.64],
  [34.42, -120.48],
  [34.18, -119.95],
  [34.03, -118.74],
  [33.72, -118.15],
  [33.35, -117.72],
  [33.1, -117.38],
  [33.05, -117.1],
  [38.25, -117.1],
];

export const CALIFORNIA_CITY: City = {
  id: "california",
  name: "California",
  center: { lat: 35.66, lng: -120.15 },
  bounds: { minLat: 33.15, maxLat: 38.05, minLng: -123.05, maxLng: -117.55 },
  latScale: 58,
  verticalExaggeration: 2.25,
  // About 2.2 km. This board is a route atlas; city detail lives one level in.
  cellLat: 0.02,
  cellLng: 0.024,
  coastFalloff: 0.025,
  landmasses: [CORRIDOR_LAND],
  parks: [],
  inlandWater: [],
  hills: [
    { name: "Santa Monica Mountains", lat: 34.12, lng: -118.65, elevation: 900, radius: 0.34 },
    { name: "San Emigdio Mountains", lat: 34.88, lng: -119.05, elevation: 2_000, radius: 0.48 },
    { name: "Temblor Range", lat: 35.36, lng: -119.83, elevation: 1_300, radius: 0.56 },
    { name: "Santa Lucia Range", lat: 35.75, lng: -121.25, elevation: 1_580, radius: 0.7 },
    { name: "Diablo Range", lat: 36.63, lng: -121.18, elevation: 1_300, radius: 0.8 },
    { name: "Mount Hamilton", lat: 37.34, lng: -121.64, elevation: 1_280, radius: 0.34 },
    { name: "Santa Cruz Mountains", lat: 37.18, lng: -122.18, elevation: 1_150, radius: 0.52 },
  ],
  districts: [],
  landmarks: [
    { name: "Los Angeles", lat: 34.0522, lng: -118.2437, height: 1_100, footprint: 0.055, shape: "tower", color: 0x9b856b, label: true },
    { name: "San Francisco", lat: 37.7749, lng: -122.4194, height: 1_000, footprint: 0.05, shape: "tower", color: 0x8799a8, label: true },
  ],
  bridges: [],
  roads: [
    { path: CALIFORNIA_US_101, width: 1, kind: "freeway" },
    { path: CALIFORNIA_I_5, width: 1.06, kind: "freeway" },
  ],
  chapters: [
    {
      id: "california-overview",
      label: "California",
      shortLabel: "State",
      number: "01",
      description: "Los Angeles and San Francisco joined as one living route board.",
      focus: { lat: 35.66, lng: -120.15, distance: 370, height: 320, rotation: 0.68 },
    },
    {
      id: "la-sf-us-101",
      label: "US-101",
      shortLabel: "101",
      number: "02",
      description: "Follow the black Model X up the coast and Salinas Valley through Santa Barbara and San Jose.",
      focus: { lat: 35.8, lng: -121.04, distance: 112, height: 72, rotation: 0.8 },
    },
    {
      id: "la-sf-i-5",
      label: "I-5 · I-580 · I-80",
      shortLabel: "I-5",
      number: "03",
      description: "Follow the black Model X through the Central Valley, then the honest Bay approach over I-580 and I-80.",
      focus: { lat: 36.15, lng: -120.15, distance: 105, height: 70, rotation: 0.78 },
    },
    {
      id: "los-angeles",
      label: "Los Angeles",
      shortLabel: "LA",
      number: "04",
      description: "The southern door into Tera's detailed Southern California board.",
      focus: { lat: 34.0522, lng: -118.2437, distance: 38, height: 26, rotation: 0.8 },
    },
    {
      id: "san-francisco",
      label: "San Francisco",
      shortLabel: "SF",
      number: "05",
      description: "The northern door into the detailed Bay Area board and Lumbridge HQ.",
      focus: { lat: 37.7749, lng: -122.4194, distance: 38, height: 26, rotation: 0.8 },
    },
  ],
  palette: {
    skyTop: 0x7da6c9,
    skyHorizon: 0xe9d8bb,
    sea: 0x477891,
    lake: 0x527f91,
    shore: 0xc9b789,
    sand: 0xd9c693,
    flats: 0xb7a16c,
    upland: 0x9a8155,
    park: 0x66764c,
    parkHigh: 0x485d43,
  },
};

export default CALIFORNIA_CITY;
