module main

import vendor.raylib rl

let minimum_window_height: i32 = 160
let window_text_padding: i32 = 40
let reference_window_height: f64 = 600.0

let overlay_title_font_size: i32 = 48
let overlay_instruction_font_size: i32 = 24
let overlay_title_y_offset: i32 = 60
let overlay_instruction_y_offset: i32 = 10
let overlay_second_instruction_y_offset: i32 = 42

let defeat_title = c"GAME OVER"
let pause_title = c"PAUSED"
let begin_title = c"FLAPPY BURD"
let begin_instruction = c"Press SPACE or click to begin"

let scale_for_window_height(value, window_height: i32): i32 {
  let scaled = (value.(f64) * window_height.(f64) / reference_window_height).(i32)

  if scaled < 1 {
    return 1
  }

  return scaled
}

let scale_with_window_height(value: i32): i32 =
  scale_for_window_height(value, rl.get_screen_height())

let overlay_text_width(window_height: i32): i32 {
  let title_font_size =
    scale_for_window_height(overlay_title_font_size, window_height)
  let instruction_font_size =
    scale_for_window_height(overlay_instruction_font_size, window_height)
  let mut widest = rl.measure_text(defeat_title, title_font_size)
  let widths: [3]i32 = [
    rl.measure_text(pause_title, title_font_size),
    rl.measure_text(begin_title, title_font_size),
    rl.measure_text(begin_instruction, instruction_font_size),
  ]

  for width in widths {
    if width > widest {
      widest = width
    }
  }

  return widest
}

let validate_window_size() {
  let screen_width = rl.get_screen_width()
  let screen_height = rl.get_screen_height()
  let mut required_height = screen_height

  if required_height < minimum_window_height {
    required_height = minimum_window_height
  }

  // Keep the bird on the left half of the screen at every aspect ratio.
  let mut required_width = (2.0 * bird_x * playfield_pixel_height_for(required_height).(f64)).(i32)
  let text_padding = scale_for_window_height(window_text_padding, required_height)
  let widest_text = overlay_text_width(required_height)

  if required_width < widest_text + text_padding {
    required_width = widest_text + text_padding
  }

  rl.set_window_min_size(required_width, minimum_window_height)

  if screen_width < required_width || screen_height < required_height {
    let mut corrected_width = screen_width

    if corrected_width < required_width {
      corrected_width = required_width
    }

    rl.set_window_size(corrected_width, required_height)
  }
}

let main() {
  rl.set_config_flags(.{ .WindowResizable, .WindowTopmost, .VsyncHint })

  rl.init_window(800, 600, "Flappy Burd")
  defer rl.close_window()

  rl.set_exit_key(.Null)
  validate_window_size()

  rl.init_audio_device()
  defer rl.close_audio_device()

  let textures = GameAssets.load()
  defer textures.unload()

  rl.set_target_fps(256)

  let mut state = GameState.{
    bird_y = 0.5,
    bird_velocity_y = 0.0,
    pipes = [.{ x = 0.0, gap_y = 0.5, passed = false }; pipe_buffer_size],
    delta_time = 0.0,
    background_scroll = 0.0,
    base_scroll = 0.0,
    started = false,
    paused = false,
    lost = false,
    time_since_loss = 0.0,
    show_collision_boxes = false,
    double_pipe_speed = false,
    points = 0,
  }

  reset_game(state.&mut)

  for !rl.window_should_close() {
    validate_window_size()
    update(state.&mut, textures.&)

    rl.begin_drawing()
    defer rl.end_drawing()

    render(state.&, textures.&)
  }
}