/*
    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::simple::State;
use crate::{BotAlgo, PacketSink, algos::simple::Context, step::StepState};
use hurrycurry_game_core::Game;
use hurrycurry_protocol::PlayerID;

pub struct Waiter {
    step: StepState,
    me: PlayerID,
}

type LogicRes<Out = ()> = Result<Out, ()>;

impl Waiter {
    pub fn new(me: PlayerID) -> Self {
        Self {
            step: StepState::new_idle(me, "idle".to_string()),
            me,
        }
    }
}
impl BotAlgo for Waiter {
    fn tick(&mut self, mut out: PacketSink, game: &Game, dt: f32) {
        if self.step.is_busy() {
            return self.step.tick(&mut out, game, dt);
        }

        Context {
            game,
            me: self.me,
            state: self,
            complexity_abort: 0,
            stack: Vec::new(),
        }
        .update()
        .ok();
    }
}

impl State for Waiter {
    fn queue_step(&mut self, step: StepState) {
        self.step = step;
    }
}

impl Context<'_, Waiter> {
    fn update(&mut self) -> LogicRes {
        let mut dems = self.find_demands();
        dems.sort_by_key(|(_, _, x)| (*x * 1000.) as i32);
        if dems.is_empty() {
            self.wait(0.5, "no orders")?;
        }
        for (item, table, _) in dems {
            if self.game.data.item_name(item) == "unknown-order" {
                self.interact_with(table, 0., "accept order")?;
            }
            if self.is_hand_item(item) {
                if self.is_tile_occupied(table) {
                    self.dispose_hand(true, "return order (table clear)")?;
                }
                self.interact_with(table, 1., "deliver order")?;
            }
            if let Some(pos) = self.find_item_on_map(true, item) {
                if self.is_hand_occupied() {
                    self.dispose_hand(true, "return dirty")?;
                }
                if self.is_tile_occupied(table) {
                    self.interact_with(table, 0., "clear table (order)")?;
                }
                self.interact_with(pos, 0., "pick up order")?;
            }
        }
        if self.is_hand_occupied() {
            self.dispose_hand(true, "return dirty")?;
        }
        if let Some(pos) = self.find_occupied_table() {
            self.interact_with(pos, 0., "clear table (idle)")?;
        }
        Ok(())
    }
}
