use glam::Vec3;
use xtra::{Actor, Address, Handler};

use crate::{
  packet::{
    Packet, PacketData,
    PacketData_variants::{Connect, Player},
  }, player::broadcast_packet, protocol::String
};

pub struct Faker {
  pub address: Address<Faker>,
}

impl Actor for Faker {
  type Stop = ();

  async fn stopped(self) -> Self::Stop {}
}

impl Handler<Packet> for Faker {
  type Return = ();

  async fn handle(&mut self, message: Packet, _: &mut xtra::Context<Self>) -> Self::Return {
    // trace!("got packet {message:?}");
    match message.data {
      PacketData::Connect(connect) => {
        broadcast_packet(Packet {
          user_id: 1,
          udp: message.udp,
          data: PacketData::Connect(Connect {
            client_name: String::try_from("Bot").unwrap(),
            ..connect
          }),
        })
        .await;
      }
      PacketData::Player(player) => {
        let pos = Vec3::from_array(player.position);
        broadcast_packet(Packet {
          user_id: 1,
          udp: message.udp,
          data: PacketData::Player(Player {
            position: (pos + Vec3::new(0., -150., 0.)).into(),
            ..player
          }),
        })
        .await;
      }

      data => {
        broadcast_packet(Packet {
          user_id: 1,
          udp: message.udp,
          data,
        })
        .await;
      }
    }
  }
}
