/*
    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::scoreboard::ScoreboardStore;
use anyhow::Result;
use hurrycurry_data::entities::GateCondition;
use hurrycurry_locale::{TrError, trm};
use hurrycurry_protocol::{
    GameConfig, ItemLocation, Message, PacketC, PlayerID, TileIndex,
    glam::{IVec2, Vec2},
};

#[derive(Debug, Default, Clone)]
pub struct Map {
    pub pos: Vec2,
    pub name: String,
}

impl Entity for Map {
    fn tick(&mut self, c: EntityContext) -> Result<(), TrError> {
        let mut activate = false;
        c.game
            .players_spatial_index
            .query(self.pos, 0.5, |_, _| activate = true);

        if activate {
            *c.load_map = Some(GameConfig {
                map: self.name.clone(),
                ..GameConfig::default()
            });
        }

        Ok(())
    }
}

#[derive(Debug, Clone)]
pub struct Gate {
    pub active: bool,
    pub unlocked: bool,
    pub pos: IVec2,
    pub blocker_tile: TileIndex,
    pub condition: GateCondition,
}
impl Entity for Gate {
    fn tick(&mut self, c: EntityContext<'_>) -> Result<(), TrError> {
        if self.active {
            self.active = false;
            self.unlocked = self.condition.check(c.scoreboard);
            if !self.unlocked {
                c.game.set_tile(self.pos, vec![self.blocker_tile]);
            }
        }
        Ok(())
    }
    fn interact(
        &mut self,
        c: EntityContext<'_>,
        pos: Option<ItemLocation>,
        _player: PlayerID,
    ) -> Result<bool, TrError> {
        if !self.unlocked && pos == Some(ItemLocation::Tile(self.pos)) {
            c.packet_out.push_back(PacketC::ServerMessage {
                message: trm!(
                    "s.campaign.unlock_condition",
                    m = self.condition.show(c.scoreboard) // TODO localize
                ),
                error: false,
            });
            return Ok(true);
        }
        Ok(false)
    }
}

trait GateConditionExt {
    fn check(&self, scoreboard: &ScoreboardStore) -> bool;
    fn show(&self, scoreboard: &ScoreboardStore) -> Message;
}
impl GateConditionExt for GateCondition {
    fn check(&self, scoreboard: &ScoreboardStore) -> bool {
        match self {
            GateCondition::All(cs) => cs.iter().all(|c| c.check(scoreboard)),
            GateCondition::Any(cs) => cs.iter().any(|c| c.check(scoreboard)),
            GateCondition::Stars(map, thres) => scoreboard
                .get(map)
                .is_some_and(|s| s.best.first().is_some_and(|b| b.score.stars >= *thres)),
        }
    }
    fn show(&self, scoreboard: &ScoreboardStore) -> Message {
        match self {
            GateCondition::All(cs) => cs
                .iter()
                .map(|c| c.show(scoreboard))
                .reduce(|a, b| trm!("s.campaign.list_helper", m = a, m = b))
                .unwrap_or(Message::Text(String::new())),
            GateCondition::Any(cs) => cs
                .iter()
                .map(|c| c.show(scoreboard))
                .reduce(|a, b| trm!("s.campaign.list_helper", m = a, m = b))
                .unwrap_or(Message::Text(String::new())),
            GateCondition::Stars(map, thres) => {
                // TODO show current score
                let _ = scoreboard;
                trm!(
                    "s.campaign.condition.stars",
                    s = thres.to_string(),
                    s = map.to_string()
                )
            }
        }
    }
}
