// Typed input and event layer over the virtual backend.
//
// The backend hands over `[kind, a, b, c]` codes — the same encoding the C
// backends have always produced — and everything user-facing is decoded here.
// Moving this up from `tycho_window` is what lets `2d` / `3d` / `ui` compile for
// every target: only the backend stays native.

#derive(Eq, Debug)
pub(all) enum CursorMode {
  Normal
  Hidden
  Locked
}

#derive(Eq, Debug)
pub(all) enum CursorShape {
  Default
  Pointer
  IBeam
  Crosshair
  HResize
  VResize
}

///|
/// Logical key identity. The platform layer reports X11 keysyms for historical
/// reasons; `from_keysym` / `to_keysym` translate losslessly, and keys the enum
/// does not name are preserved as `Unknown(keysym)`.
#derive(Eq, Debug)
pub(all) enum KeyCode {
  Escape
  Return
  Tab
  Backspace
  Space
  Left
  Up
  Right
  Down
  Home
  End
  PageUp
  PageDown
  Insert
  Delete
  Num0
  Num1
  Num2
  Num3
  Num4
  Num5
  Num6
  Num7
  Num8
  Num9
  A
  B
  C
  D
  E
  F
  G
  H
  I
  J
  K
  L
  M
  N
  O
  P
  Q
  R
  S
  T
  U
  V
  W
  X
  Y
  Z
  F1
  F2
  F3
  F4
  F5
  F6
  F7
  F8
  F9
  F10
  F11
  F12
  LShift
  RShift
  LCtrl
  RCtrl
  LAlt
  RAlt
  Unknown(Int)
}

///|
pub fn KeyCode::from_keysym(sym : Int) -> KeyCode {
  match sym {
    0xff1b => Escape
    0xff0d => Return
    0xff09 => Tab
    0xff08 => Backspace
    0x0020 => Space
    0xff51 => Left
    0xff52 => Up
    0xff53 => Right
    0xff54 => Down
    0xff50 => Home
    0xff57 => End
    0xff55 => PageUp
    0xff56 => PageDown
    0xff63 => Insert
    0xffff => Delete
    0x0030 => Num0
    0x0031 => Num1
    0x0032 => Num2
    0x0033 => Num3
    0x0034 => Num4
    0x0035 => Num5
    0x0036 => Num6
    0x0037 => Num7
    0x0038 => Num8
    0x0039 => Num9
    0x0061 => A
    0x0062 => B
    0x0063 => C
    0x0064 => D
    0x0065 => E
    0x0066 => F
    0x0067 => G
    0x0068 => H
    0x0069 => I
    0x006a => J
    0x006b => K
    0x006c => L
    0x006d => M
    0x006e => N
    0x006f => O
    0x0070 => P
    0x0071 => Q
    0x0072 => R
    0x0073 => S
    0x0074 => T
    0x0075 => U
    0x0076 => V
    0x0077 => W
    0x0078 => X
    0x0079 => Y
    0x007a => Z
    0xffbe => F1
    0xffbf => F2
    0xffc0 => F3
    0xffc1 => F4
    0xffc2 => F5
    0xffc3 => F6
    0xffc4 => F7
    0xffc5 => F8
    0xffc6 => F9
    0xffc7 => F10
    0xffc8 => F11
    0xffc9 => F12
    0xffe1 => LShift
    0xffe2 => RShift
    0xffe3 => LCtrl
    0xffe4 => RCtrl
    0xffe9 => LAlt
    0xffea => RAlt
    other => Unknown(other)
  }
}

///|
pub fn KeyCode::to_keysym(self : KeyCode) -> Int {
  match self {
    Escape => 0xff1b
    Return => 0xff0d
    Tab => 0xff09
    Backspace => 0xff08
    Space => 0x0020
    Left => 0xff51
    Up => 0xff52
    Right => 0xff53
    Down => 0xff54
    Home => 0xff50
    End => 0xff57
    PageUp => 0xff55
    PageDown => 0xff56
    Insert => 0xff63
    Delete => 0xffff
    Num0 => 0x0030
    Num1 => 0x0031
    Num2 => 0x0032
    Num3 => 0x0033
    Num4 => 0x0034
    Num5 => 0x0035
    Num6 => 0x0036
    Num7 => 0x0037
    Num8 => 0x0038
    Num9 => 0x0039
    A => 0x0061
    B => 0x0062
    C => 0x0063
    D => 0x0064
    E => 0x0065
    F => 0x0066
    G => 0x0067
    H => 0x0068
    I => 0x0069
    J => 0x006a
    K => 0x006b
    L => 0x006c
    M => 0x006d
    N => 0x006e
    O => 0x006f
    P => 0x0070
    Q => 0x0071
    R => 0x0072
    S => 0x0073
    T => 0x0074
    U => 0x0075
    V => 0x0076
    W => 0x0077
    X => 0x0078
    Y => 0x0079
    Z => 0x007a
    F1 => 0xffbe
    F2 => 0xffbf
    F3 => 0xffc0
    F4 => 0xffc1
    F5 => 0xffc2
    F6 => 0xffc3
    F7 => 0xffc4
    F8 => 0xffc5
    F9 => 0xffc6
    F10 => 0xffc7
    F11 => 0xffc8
    F12 => 0xffc9
    LShift => 0xffe1
    RShift => 0xffe2
    LCtrl => 0xffe3
    RCtrl => 0xffe4
    LAlt => 0xffe9
    RAlt => 0xffea
    Unknown(sym) => sym
  }
}

///|
/// Keyboard modifiers, decoded from the X11-compatible mask the platform layer
/// reports (Shift=1, Lock=2, Ctrl=4, Mod1/Alt=8, Mod4/Super=64).
#derive(Eq, Debug)
pub(all) struct Modifiers {
  shift : Bool
  ctrl : Bool
  alt : Bool
  super_key : Bool
}

///|
pub fn Modifiers::new(
  shift? : Bool = false,
  ctrl? : Bool = false,
  alt? : Bool = false,
  super_key? : Bool = false,
) -> Modifiers {
  { shift, ctrl, alt, super_key }
}

///|
pub fn Modifiers::none() -> Modifiers {
  { shift: false, ctrl: false, alt: false, super_key: false }
}

///|
pub fn Modifiers::from_bits(bits : Int) -> Modifiers {
  {
    shift: (bits & 1) != 0,
    ctrl: (bits & 4) != 0,
    alt: (bits & 8) != 0,
    super_key: (bits & 64) != 0,
  }
}

///|
pub fn Modifiers::bits(self : Modifiers) -> Int {
  (if self.shift { 1 } else { 0 }) |
  (if self.ctrl { 4 } else { 0 }) |
  (if self.alt { 8 } else { 0 }) |
  (if self.super_key { 64 } else { 0 })
}

///|
pub fn Modifiers::is_empty(self : Modifiers) -> Bool {
  !self.shift && !self.ctrl && !self.alt && !self.super_key
}

///|
#derive(Eq, Debug)
pub(all) enum MouseButton {
  Left
  Right
  Middle
  Other(Int)
}

///|
/// Platform button index (X11 numbering: 1 = left, 2 = middle, 3 = right,
/// 8/9 = side buttons).
pub fn MouseButton::from_index(index : Int) -> MouseButton {
  match index {
    1 => Left
    2 => Middle
    3 => Right
    other => Other(other)
  }
}

///|
pub fn MouseButton::index(self : MouseButton) -> Int {
  match self {
    Left => 1
    Middle => 2
    Right => 3
    Other(i) => i
  }
}

///|
#derive(Eq, Debug)
pub(all) enum Event {
  CloseRequested
  Resized(Int, Int)
  KeyDown(KeyCode, Modifiers)
  KeyUp(KeyCode, Modifiers)
  MouseMove(Int, Int)
  MouseDown(MouseButton, Int, Int)
  MouseUp(MouseButton, Int, Int)
  MouseScroll(Int, Int)
  FocusChanged(Bool)
  TextInput(String)
  // IME composition: preedit chars arrive one at a time after a clear, then
  // either commit chars or a cancel. The host feeds these into
  // TextController::set_preedit / commit_preedit / clear_preedit.
  ImePreedit(String)
  ImePreeditClear
  ImeCommit(String)
  ImeCancel
  // Mobile touch and lifecycle events reserved for iOS (UIKit) and Android (NDK).
  /// `(pointer_id, x, y)` in logical surface coordinates.
  TouchDown(Int, Int, Int)
  TouchMove(Int, Int, Int)
  TouchUp(Int, Int, Int)
  /// OS surface became available (e.g. Android `onNativeWindowCreated`, iOS view attach).
  SurfaceCreated
  /// OS surface is being torn down (e.g. Android `onNativeWindowDestroyed`); drop swapchain.
  SurfaceDestroyed
  /// App entered background; pause rendering and release transient GPU work.
  AppSuspended
  /// App returned to foreground.
  AppResumed
}

///|
pub fn event_from_code(kind : Int, a : Int, b : Int, c : Int) -> Event {
  match kind {
    1 => Event::CloseRequested
    2 => Event::Resized(a, b)
    3 => Event::KeyDown(KeyCode::from_keysym(a), Modifiers::from_bits(b))
    4 => Event::KeyUp(KeyCode::from_keysym(a), Modifiers::from_bits(b))
    5 => Event::MouseMove(a, b)
    6 => Event::MouseDown(MouseButton::from_index(a), b, c)
    7 => Event::MouseUp(MouseButton::from_index(a), b, c)
    8 => Event::MouseScroll(a, b)
    9 => Event::FocusChanged(a != 0)
    10 => {
      let ch = Int::unsafe_to_char(a)
      Event::TextInput(ch.to_string())
    }
    11 => {
      let ch = Int::unsafe_to_char(a)
      Event::ImePreedit(ch.to_string())
    }
    12 => Event::ImePreeditClear
    13 => {
      let ch = Int::unsafe_to_char(a)
      Event::ImeCommit(ch.to_string())
    }
    14 => Event::ImeCancel
    15 => Event::TouchDown(a, b, c)
    16 => Event::TouchMove(a, b, c)
    17 => Event::TouchUp(a, b, c)
    18 => Event::SurfaceCreated
    19 => Event::SurfaceDestroyed
    20 => Event::AppSuspended
    21 => Event::AppResumed
    _ => Event::CloseRequested
  }
}

///|
pub fn Window::poll_events(self : Window) -> Array[Event] {
  @be.backend_win_poll()
  let out : Array[Event] = []
  let buf : FixedArray[Int32] = FixedArray::make(4, (0 : Int32))
  while @be.backend_win_next_event(self.id, buf) != (0 : Int32) {
    out.push(
      event_from_code(
        buf[0].to_int(),
        buf[1].to_int(),
        buf[2].to_int(),
        buf[3].to_int(),
      ),
    )
  }
  out
}

///|
pub fn Window::mouse_pos(self : Window) -> (Double, Double) {
  (@be.backend_win_mouse_x(self.id), @be.backend_win_mouse_y(self.id))
}

///|
pub fn Window::is_mouse_down(self : Window, button : MouseButton) -> Bool {
  @be.backend_win_mouse_button(self.id, button.index())
}

///|
pub fn Window::set_cursor_mode(self : Window, mode : CursorMode) -> Unit {
  let code = match mode {
    CursorMode::Normal => 0
    CursorMode::Hidden => 1
    CursorMode::Locked => 2
  }
  @be.backend_win_set_cursor_mode(self.id, code)
}

///|
pub fn Window::set_cursor_shape(self : Window, shape : CursorShape) -> Unit {
  let code = match shape {
    CursorShape::Default => 0
    CursorShape::Pointer => 1
    CursorShape::IBeam => 2
    CursorShape::Crosshair => 3
    CursorShape::HResize => 4
    CursorShape::VResize => 5
  }
  @be.backend_win_set_cursor_shape(self.id, code)
}

///|
pub fn Window::set_clipboard_text(self : Window, text : String) -> Unit {
  @be.backend_win_set_clipboard(self.id, text)
}

///|
pub fn Window::clipboard_text(self : Window) -> String {
  @be.backend_win_clipboard(self.id)
}

///|
/// Wait for the next batch of platform events (compositor-driven).
pub fn wait_events(timeout_ms : Int) -> Unit {
  @be.backend_win_wait_events(timeout_ms)
}

///|
/// Sleep until the next display vsync (Android AChoreographer clock; no-op
/// elsewhere). Used to pace the render loop to the refresh boundary.
pub fn wait_frame() -> Unit {
  @be.backend_win_wait_frame()
}

///|
/// Display refresh rate in Hz.
pub fn refresh_rate() -> Double {
  @be.backend_win_refresh_rate()
}