/*
    Hurry Curry! - a game about cooking
    Copyright 2024 metamuffin
    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 .
*/
use crate::{
    pathfinding::{find_path_to_neighbour, Path},
    BotAlgo, BotInput,
};
use hurrycurry_client_lib::Game;
use hurrycurry_protocol::{glam::IVec2, ItemIndex, Message, PlayerID};
use log::info;
#[derive(Default)]
pub struct Test {
    path: Option,
}
impl BotAlgo for Test {
    fn tick(&mut self, me: PlayerID, game: &Game, dt: f32) -> BotInput {
        let Some(player) = game.players.get(&me) else {
            return BotInput::default();
        };
        let pos = player.movement.position;
        if let Some(path) = &mut self.path {
            let direction = path.next_direction(pos, dt);
            return BotInput {
                direction,
                boost: false,
                interact: None,
                ..Default::default()
            };
        } else if let Some((item, near)) = find_demand(game) {
            info!("demand {item:?} near {near}");
            if let Some(path) = find_path_to_neighbour(&game.walkable, pos.as_ivec2(), near) {
                self.path = Some(path);
            }
        }
        BotInput::default()
    }
}
fn find_demand(game: &Game) -> Option<(ItemIndex, IVec2)> {
    game.players
        .iter()
        .find_map(|(_, pl)| match &pl.communicate_persist {
            Some((Message::Item(item), _)) => {
                let pos = pl.movement.position.as_ivec2();
                let t = pos;
                Some((*item, t))
            }
            _ => None,
        })
}