/*
    Hurry Curry! - a game about cooking
    Copyright (C) 2025 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 anyhow::Result;
use hurrycurry_bot::{BotAlgo, DynBotAlgo};
use hurrycurry_protocol::{Character, Hand, PacketS, PlayerClass, PlayerID};
use log::debug;
use std::any::Any;

pub type DynBotDriver = BotDriver<DynBotAlgo>;

pub struct BotDriver<T> {
    algo: T,
    join_data: Option<(String, Character, PlayerClass)>,
    id: PlayerID,
    interacting: bool,
    left: bool,
}

impl<T: BotAlgo> BotDriver<T> {
    pub fn new(name: String, character: Character, class: PlayerClass, algo: T) -> Self {
        Self {
            algo,
            id: PlayerID(0),
            interacting: false,
            join_data: Some((name, character, class)),
            left: false,
        }
    }
}
impl<T: BotAlgo + Any> Entity for BotDriver<T> {
    fn finished(&self) -> bool {
        self.left
    }
    fn tick(&mut self, c: EntityContext<'_>) -> Result<()> {
        if let Some((name, character, class)) = self.join_data.take() {
            self.id = c.game.get_unused_player_id(); // TODO clashes when multiple bots join in the same tick
            debug!("join {}", self.id);
            c.packet_in.push_back(PacketS::Join {
                name,
                character,
                id: Some(self.id),
                class,
                position: None,
            })
        }

        let input = self.algo.tick(self.id, c.game, c.dt);
        if input.leave {
            debug!("leave {}", self.id);
            c.packet_in.push_back(PacketS::Leave { player: self.id });
            self.left = true;
            return Ok(());
        }
        if input.interact.is_some() != self.interacting {
            self.interacting = input.interact.is_some();
            c.packet_in.push_back(PacketS::Interact {
                player: self.id,
                pos: input.interact,
                hand: Hand(0),
            })
        }
        c.packet_in.push_back(PacketS::Movement {
            player: self.id,
            dir: input.direction,
            boost: input.boost,
            pos: None,
        });
        c.packet_in.extend(input.extra);
        Ok(())
    }
    fn destructor(&mut self, c: EntityContext<'_>) {
        if self.join_data.is_none() && !self.left {
            c.packet_in.push_back(PacketS::Leave { player: self.id })
        }
    }
}
