// automatically generated by the FlatBuffers compiler, do not modify
// @generated
extern crate alloc;
extern crate flatbuffers;
use alloc::boxed::Box;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::mem;
use core::cmp::Ordering;
use self::flatbuffers::{EndianScalar, Follow};
use super::*;
pub enum GameMessageWrapperOffset {}
#[derive(Copy, Clone, PartialEq)]

pub struct GameMessageWrapper<'a> {
  pub _tab: flatbuffers::Table<'a>,
}

impl<'a> flatbuffers::Follow<'a> for GameMessageWrapper<'a> {
  type Inner = GameMessageWrapper<'a>;
  #[inline]
  unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
    Self { _tab: flatbuffers::Table::new(buf, loc) }
  }
}

impl<'a> GameMessageWrapper<'a> {
  pub const VT_MESSAGE_TYPE: flatbuffers::VOffsetT = 4;
  pub const VT_MESSAGE: flatbuffers::VOffsetT = 6;

  pub const fn get_fully_qualified_name() -> &'static str {
    "RustNamerTest.GameMessageWrapper"
  }

  #[inline]
  pub unsafe fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
    GameMessageWrapper { _tab: table }
  }
  #[allow(unused_mut)]
  pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr, A: flatbuffers::Allocator + 'bldr>(
    _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr, A>,
    args: &'args GameMessageWrapperArgs
  ) -> flatbuffers::WIPOffset<GameMessageWrapper<'bldr>> {
    let mut builder = GameMessageWrapperBuilder::new(_fbb);
    if let Some(x) = args.Message { builder.add_Message(x); }
    builder.add_Message_type(args.Message_type);
    builder.finish()
  }

  pub fn unpack(&self) -> GameMessageWrapperT {
    let Message = match self.Message_type() {
      GameMessage::NONE => GameMessageT::NONE,
      GameMessage::PlayerStatEvent => GameMessageT::PlayerStatEvent(Box::new(
        self.Message_as_player_stat_event()
            .expect("Invalid union table, expected `GameMessage::PlayerStatEvent`.")
            .unpack()
      )),
      GameMessage::PlayerSpectate => GameMessageT::PlayerSpectate(Box::new(
        self.Message_as_player_spectate()
            .expect("Invalid union table, expected `GameMessage::PlayerSpectate`.")
            .unpack()
      )),
      GameMessage::PlayerInputChange => GameMessageT::PlayerInputChange(Box::new(
        self.Message_as_player_input_change()
            .expect("Invalid union table, expected `GameMessage::PlayerInputChange`.")
            .unpack()
      )),
      _ => GameMessageT::NONE,
    };
    GameMessageWrapperT {
      Message,
    }
  }

  #[inline]
  pub fn Message_type(&self) -> GameMessage {
    // Safety:
    // Created from valid Table for this object
    // which contains a valid value in this slot
    unsafe { self._tab.get::<GameMessage>(GameMessageWrapper::VT_MESSAGE_TYPE, Some(GameMessage::NONE)).unwrap()}
  }
  #[inline]
  pub fn Message(&self) -> Option<flatbuffers::Table<'a>> {
    // Safety:
    // Created from valid Table for this object
    // which contains a valid value in this slot
    unsafe { self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Table<'a>>>(GameMessageWrapper::VT_MESSAGE, None)}
  }
  #[inline]
  #[allow(non_snake_case)]
  pub fn Message_as_player_stat_event(&self) -> Option<PlayerStatEvent<'a>> {
    if self.Message_type() == GameMessage::PlayerStatEvent {
      self.Message().map(|t| {
       // Safety:
       // Created from a valid Table for this object
       // Which contains a valid union in this slot
       unsafe { PlayerStatEvent::init_from_table(t) }
     })
    } else {
      None
    }
  }

  #[inline]
  #[allow(non_snake_case)]
  pub fn Message_as_player_spectate(&self) -> Option<PlayerSpectate<'a>> {
    if self.Message_type() == GameMessage::PlayerSpectate {
      self.Message().map(|t| {
       // Safety:
       // Created from a valid Table for this object
       // Which contains a valid union in this slot
       unsafe { PlayerSpectate::init_from_table(t) }
     })
    } else {
      None
    }
  }

  #[inline]
  #[allow(non_snake_case)]
  pub fn Message_as_player_input_change(&self) -> Option<PlayerInputChange<'a>> {
    if self.Message_type() == GameMessage::PlayerInputChange {
      self.Message().map(|t| {
       // Safety:
       // Created from a valid Table for this object
       // Which contains a valid union in this slot
       unsafe { PlayerInputChange::init_from_table(t) }
     })
    } else {
      None
    }
  }

}

impl flatbuffers::Verifiable for GameMessageWrapper<'_> {
  #[inline]
  fn run_verifier(
    v: &mut flatbuffers::Verifier, pos: usize
  ) -> Result<(), flatbuffers::InvalidFlatbuffer> {
    use self::flatbuffers::Verifiable;
    v.visit_table(pos)?
     .visit_union::<GameMessage, _>("Message_type", Self::VT_MESSAGE_TYPE, "Message", Self::VT_MESSAGE, false, |key, v, pos| {
        match key {
          GameMessage::PlayerStatEvent => v.verify_union_variant::<flatbuffers::ForwardsUOffset<PlayerStatEvent>>("GameMessage::PlayerStatEvent", pos),
          GameMessage::PlayerSpectate => v.verify_union_variant::<flatbuffers::ForwardsUOffset<PlayerSpectate>>("GameMessage::PlayerSpectate", pos),
          GameMessage::PlayerInputChange => v.verify_union_variant::<flatbuffers::ForwardsUOffset<PlayerInputChange>>("GameMessage::PlayerInputChange", pos),
          _ => Ok(()),
        }
     })?
     .finish();
    Ok(())
  }
}
pub struct GameMessageWrapperArgs {
    pub Message_type: GameMessage,
    pub Message: Option<flatbuffers::WIPOffset<flatbuffers::UnionWIPOffset>>,
}
impl<'a> Default for GameMessageWrapperArgs {
  #[inline]
  fn default() -> Self {
    GameMessageWrapperArgs {
      Message_type: GameMessage::NONE,
      Message: None,
    }
  }
}

pub struct GameMessageWrapperBuilder<'a: 'b, 'b, A: flatbuffers::Allocator + 'a> {
  fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a, A>,
  start_: flatbuffers::WIPOffset<flatbuffers::TableUnfinishedWIPOffset>,
}
impl<'a: 'b, 'b, A: flatbuffers::Allocator + 'a> GameMessageWrapperBuilder<'a, 'b, A> {
  #[inline]
  pub fn add_Message_type(&mut self, Message_type: GameMessage) {
    self.fbb_.push_slot::<GameMessage>(GameMessageWrapper::VT_MESSAGE_TYPE, Message_type, GameMessage::NONE);
  }
  #[inline]
  pub fn add_Message(&mut self, Message: flatbuffers::WIPOffset<flatbuffers::UnionWIPOffset>) {
    self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(GameMessageWrapper::VT_MESSAGE, Message);
  }
  #[inline]
  pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a, A>) -> GameMessageWrapperBuilder<'a, 'b, A> {
    let start = _fbb.start_table();
    GameMessageWrapperBuilder {
      fbb_: _fbb,
      start_: start,
    }
  }
  #[inline]
  pub fn finish(self) -> flatbuffers::WIPOffset<GameMessageWrapper<'a>> {
    let o = self.fbb_.end_table(self.start_);
    flatbuffers::WIPOffset::new(o.value())
  }
}

impl core::fmt::Debug for GameMessageWrapper<'_> {
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    let mut ds = f.debug_struct("GameMessageWrapper");
      ds.field("Message_type", &self.Message_type());
      match self.Message_type() {
        GameMessage::PlayerStatEvent => {
          if let Some(x) = self.Message_as_player_stat_event() {
            ds.field("Message", &x)
          } else {
            ds.field("Message", &"InvalidFlatbuffer: Union discriminant does not match value.")
          }
        },
        GameMessage::PlayerSpectate => {
          if let Some(x) = self.Message_as_player_spectate() {
            ds.field("Message", &x)
          } else {
            ds.field("Message", &"InvalidFlatbuffer: Union discriminant does not match value.")
          }
        },
        GameMessage::PlayerInputChange => {
          if let Some(x) = self.Message_as_player_input_change() {
            ds.field("Message", &x)
          } else {
            ds.field("Message", &"InvalidFlatbuffer: Union discriminant does not match value.")
          }
        },
        _ => {
          let x: Option<()> = None;
          ds.field("Message", &x)
        },
      };
      ds.finish()
  }
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct GameMessageWrapperT {
  pub Message: GameMessageT,
}
impl Default for GameMessageWrapperT {
  fn default() -> Self {
    Self {
      Message: GameMessageT::NONE,
    }
  }
}
impl GameMessageWrapperT {
  pub fn pack<'b, A: flatbuffers::Allocator + 'b>(
    &self,
    _fbb: &mut flatbuffers::FlatBufferBuilder<'b, A>
  ) -> flatbuffers::WIPOffset<GameMessageWrapper<'b>> {
    let Message_type = self.Message.game_message_type();
    let Message = self.Message.pack(_fbb);
    GameMessageWrapper::create(_fbb, &GameMessageWrapperArgs{
      Message_type,
      Message,
    })
  }
}
