/*
    Hurry Curry! - a game about cooking
    Copyright (C) 2026 Hurry Curry! Contributors

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License only.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/
use super::{Entity, EntityContext};
use crate::random_gauss;
use anyhow::Result;
use hurrycurry_locale::TrError;
use hurrycurry_protocol::{Character, PacketS, PlayerClass, PlayerID, glam::Vec2};
use rand::random;
use std::collections::HashMap;

pub struct Pedestrians {
    pub players: HashMap<PlayerID, usize>,
    pub points: Vec<Vec2>,
    pub spawn_delay: f32,
    pub spawn_delay_stdev: f32,
    pub cooldown: f32,
    pub speed: f32,
}

impl Entity for Pedestrians {
    fn finished(&self) -> bool {
        false
    }
    fn tick(&mut self, c: EntityContext<'_>) -> Result<(), TrError> {
        self.cooldown -= c.dt;
        if self.cooldown <= 0. && self.players.len() < 32 {
            let id = c.game.get_unused_player_id();
            c.packet_in.push_back(PacketS::Join {
                id: Some(id),
                name: "Pedestrian".to_string(),
                character: Character {
                    color: random(),
                    hairstyle: random(),
                    headwear: 0,
                },
                class: PlayerClass::Customer,
                position: self.points.first().copied(),
            });
            self.players.insert(id, 0);
            self.cooldown += (self.spawn_delay + self.spawn_delay_stdev * random_gauss()).max(0.1);
        }

        let mut remove = Vec::new();

        for (id, index) in &mut self.players {
            if let Some(player) = c.game.players.get(id) {
                let diff = self.points[*index] - player.movement.position;
                if diff.length() < 0.8 {
                    *index += 1;
                    if *index >= self.points.len() {
                        remove.push(*id)
                    }
                } else {
                    c.packet_in.push_back(PacketS::Movement {
                        player: *id,
                        dir: diff.normalize_or_zero() * self.speed,
                        boost: false,
                        pos: None,
                    });
                }
            }
        }

        for id in remove {
            if self.players.remove(&id).is_some() {
                c.packet_in.push_back(PacketS::Leave { player: id });
            }
        }

        Ok(())
    }
    fn destructor(&mut self, c: EntityContext<'_>) {
        for (id, _) in self.players.drain() {
            c.packet_in.push_back(PacketS::Leave { player: id })
        }
    }
}
