use std::rc::{Rc, Weak};

use data::math_def::Vector3;
use proto::{AbilityControlBlock, AbilitySyncStateInfo, TeamEnterSceneInfo};

use crate::{entity::Entity, player::Player};

#[derive(Default)]
pub struct AvatarTeamEntity {
    pos: Vector3,
    rot: Vector3,
    player: Weak<Player>,
    id: u32,
}

impl AvatarTeamEntity {
    pub fn set_player(&mut self, player_ptr: &Rc<Player>) {
        self.player = Rc::downgrade(player_ptr);
    }

    pub fn to_client(&self, team_enter_info: &mut TeamEnterSceneInfo) {
        team_enter_info.ability_control_block = Some(AbilityControlBlock::default());
        team_enter_info.team_ability_info = Some(AbilitySyncStateInfo::default());
        team_enter_info.team_entity_id = self.id;
    }
}

impl Entity for AvatarTeamEntity {
    fn get_entity_type(&self) -> proto::ProtEntityType {
        proto::ProtEntityType::ProtEntityTeam
    }

    fn get_group_id(&self) -> u32 {
        0
    }

    fn entity_id(&self) -> u32 {
        self.id
    }

    fn set_entity_id(&mut self, id: u32) {
        self.id = id;
    }

    fn position(&self) -> &data::math_def::Vector3 {
        &self.pos
    }

    fn rotation(&self) -> &data::math_def::Vector3 {
        &self.rot
    }

    fn set_position(&mut self, pos: data::math_def::Vector3) {
        self.pos = pos;
    }

    fn set_rotation(&mut self, rot: data::math_def::Vector3) {
        self.rot = rot;
    }

    fn get_owner_player(&self) -> Option<Rc<Player>> {
        self.player.upgrade()
    }
}
