import * as $int from "../gleam_stdlib/gleam/int.mjs";
import * as $list from "../gleam_stdlib/gleam/list.mjs";
import * as $paint from "../paint/paint.mjs";
import * as $canvas from "../paint/paint/canvas.mjs";
import { toList, prepend as listPrepend } from "./gleam.mjs";
import * as $hud from "./hud.mjs";
import * as $target from "./target.mjs";
import * as $types from "./types.mjs";
import { GameOver, MainMenu, Playing } from "./types.mjs";

function draw_main_menu() {
  let lucy_image = $canvas.image_from_src("../res/lucy.svg");
  let diamond_image = $canvas.image_from_src("../res/diamond.svg");
  let rocket_image = $canvas.image_from_src("../res/rocket.svg");
  return $paint.combine(
    toList([
      (() => {
        let _pipe = $paint.text("Stellar prune", 72);
        let _pipe$1 = $paint.translate_xy(_pipe, 300.0, 100.0);
        let _pipe$2 = $paint.fill(_pipe$1, $paint.colour_hex("#ffffff"));
        return $paint.image_scaling_pixelated(_pipe$2);
      })(),
      (() => {
        let _pipe = $paint.text("Click/swipe the targets to score points!", 28);
        let _pipe$1 = $paint.translate_xy(_pipe, 320.0, 200.0);
        let _pipe$2 = $paint.fill(_pipe$1, $paint.colour_hex("#aaaaaa"));
        return $paint.image_scaling_pixelated(_pipe$2);
      })(),
      (() => {
        let _pipe = $paint.text(
          "Don't let them escape or you'll lose health!",
          28,
        );
        let _pipe$1 = $paint.translate_xy(_pipe, 320.0, 240.0);
        let _pipe$2 = $paint.fill(_pipe$1, $paint.colour_hex("#aaaaaa"));
        return $paint.image_scaling_pixelated(_pipe$2);
      })(),
      (() => {
        let _pipe = $paint.image(lucy_image, 80, 80);
        let _pipe$1 = $paint.image_scaling_pixelated(_pipe);
        return $paint.translate_xy(_pipe$1, 320.0, 320.0);
      })(),
      (() => {
        let _pipe = $paint.text("Lucy - Click for 1 point.", 24);
        let _pipe$1 = $paint.translate_xy(_pipe, 420.0, 370.0);
        let _pipe$2 = $paint.fill(_pipe$1, $paint.colour_hex("#ffffff"));
        return $paint.image_scaling_pixelated(_pipe$2);
      })(),
      (() => {
        let _pipe = $paint.image(diamond_image, 80, 80);
        let _pipe$1 = $paint.image_scaling_pixelated(_pipe);
        return $paint.translate_xy(_pipe$1, 320.0, 420.0);
      })(),
      (() => {
        let _pipe = $paint.text("Diamond - Click for 3 points!", 24);
        let _pipe$1 = $paint.translate_xy(_pipe, 420.0, 460.0);
        let _pipe$2 = $paint.fill(_pipe$1, $paint.colour_hex("#ffffff"));
        return $paint.image_scaling_pixelated(_pipe$2);
      })(),
      (() => {
        let _pipe = $paint.image(rocket_image, 80, 80);
        let _pipe$1 = $paint.image_scaling_pixelated(_pipe);
        return $paint.translate_xy(_pipe$1, 320.0, 520.0);
      })(),
      (() => {
        let _pipe = $paint.text("Rocket - AVOID! Loses 1 health", 24);
        let _pipe$1 = $paint.translate_xy(_pipe, 420.0, 560.0);
        let _pipe$2 = $paint.fill(_pipe$1, $paint.colour_hex("#ff6666"));
        return $paint.image_scaling_pixelated(_pipe$2);
      })(),
      (() => {
        let _pipe = $paint.text("Click to play!", 48);
        let _pipe$1 = $paint.translate_xy(_pipe, 420.0, 680.0);
        let _pipe$2 = $paint.fill(_pipe$1, $paint.colour_hex("#ffaff3"));
        return $paint.image_scaling_pixelated(_pipe$2);
      })(),
    ]),
  );
}

function draw_game_over(final_score) {
  return $paint.combine(
    toList([
      (() => {
        let _pipe = $paint.text("Well done!", 128);
        let _pipe$1 = $paint.translate_xy(_pipe, 300.0, 175.0);
        let _pipe$2 = $paint.fill(_pipe$1, $paint.colour_hex("#ffaff3"));
        return $paint.image_scaling_pixelated(_pipe$2);
      })(),
      (() => {
        let _pipe = $paint.text("Final Score: ", 48);
        let _pipe$1 = $paint.translate_xy(_pipe, 440.0, 400.0);
        let _pipe$2 = $paint.fill(_pipe$1, $paint.colour_hex("#ffffff"));
        return $paint.image_scaling_pixelated(_pipe$2);
      })(),
      (() => {
        let _pipe = $paint.text($int.to_string(final_score), 48);
        let _pipe$1 = $paint.translate_xy(_pipe, 710.0, 400.0);
        let _pipe$2 = $paint.fill(_pipe$1, $paint.colour_hex("#ffaff3"));
        return $paint.image_scaling_pixelated(_pipe$2);
      })(),
      (() => {
        let _pipe = $paint.text("Click to play again!", 32);
        let _pipe$1 = $paint.translate_xy(_pipe, 455.0, 600.0);
        let _pipe$2 = $paint.fill(_pipe$1, $paint.colour_hex("#ffaff3"));
        return $paint.image_scaling_pixelated(_pipe$2);
      })(),
    ]),
  );
}

function draw_playing(playing_state) {
  let target_pictures = $list.map(
    playing_state.targets,
    (target) => {
      let position;
      let rigid_body;
      let paintable;
      position = target.position;
      rigid_body = target.rigid_body;
      paintable = target.paintable;
      let _pipe = $target.paintable_to_picture(paintable);
      let _pipe$1 = $paint.rotate(_pipe, $paint.angle_deg(rigid_body.rotation));
      return $paint.translate_xy(_pipe$1, position.x, position.y);
    },
  );
  let hud_without_crosshair = $paint.combine(
    toList([
      $hud.health_to_picture(playing_state.hud.health),
      $hud.score_to_picture(playing_state.hud.score),
    ]),
  );
  let crosshair = $hud.cross_hair_to_picture(playing_state.hud.cross_hair);
  return $paint.combine(
    listPrepend(
      hud_without_crosshair,
      $list.append(target_pictures, toList([crosshair])),
    ),
  );
}

export function render(state) {
  let _block;
  if (state instanceof MainMenu) {
    _block = draw_main_menu();
  } else if (state instanceof Playing) {
    let playing_state = state.state;
    _block = draw_playing(playing_state);
  } else {
    let final_score = state.final_score;
    _block = draw_game_over(final_score);
  }
  let picture = _block;
  return $canvas.display((_) => { return picture; }, "#canvas");
}
