import gleam/dynamic/decode
import gleam/json

pub type PlayerState {
  PlayerState(user_id: Int, game_id: String)
}

pub type PlayerClientMessages {
  BuzzIn(time: Float)
}

pub fn player_client_messages_decoder() -> decode.Decoder(PlayerClientMessages) {
  use time <- decode.field("time", decode.float)
  decode.success(BuzzIn(time:))
}

pub type PlayerServerMessages {
  Ack
  UpdatePointTotal(score: Int)
  ResetBuzzer
  ExitGame
}

pub fn encode_player_server_messages(
  player_server_messages: PlayerServerMessages,
) -> json.Json {
  case player_server_messages {
    Ack -> json.object([#("type", json.string("Ack"))])
    UpdatePointTotal(..) ->
      json.object([
        #("type", json.string("UpdatePointTotal")),
        #("score", json.int(player_server_messages.score)),
      ])
    ResetBuzzer -> json.object([#("type", json.string("ResetBuzzer"))])
    ExitGame -> json.object([#("type", json.string("ExitGame"))])
  }
}
