/*
    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 super::{Entity, EntityContext};
use crate::{scoreboard::ScoreboardStore, server::GameServerExt, trm, TrError};
use anyhow::Result;
use hurrycurry_protocol::{
    glam::{IVec2, Vec2},
    PacketC, PlayerID, TileIndex,
};
use serde::{Deserialize, Serialize};
use std::fmt::Write;
#[derive(Debug, Default, Clone)]
pub struct Map {
    pub location: Vec2,
    pub name: String,
}
impl Entity for Map {
    fn tick(&mut self, c: EntityContext) -> Result<()> {
        let mut activate = false;
        c.game
            .players_spatial_index
            .query(self.location, 0.5, |_, _| activate = true);
        if activate {
            *c.load_map = Some(self.name.clone());
        }
        Ok(())
    }
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum GateCondition {
    All(Vec),
    Any(Vec),
    Stars(String, u8),
}
#[derive(Debug, Clone)]
pub struct Gate {
    pub active: bool,
    pub unlocked: bool,
    pub location: IVec2,
    pub blocker_tile: TileIndex,
    pub condition: GateCondition,
}
impl Entity for Gate {
    fn tick(&mut self, c: EntityContext<'_>) -> Result<()> {
        if self.active {
            self.active = false;
            self.unlocked = self.condition.check(c.scoreboard);
            if !self.unlocked {
                c.game
                    .set_tile(self.location, Some(self.blocker_tile), c.packet_out)
            }
        }
        Ok(())
    }
    fn interact(
        &mut self,
        c: EntityContext<'_>,
        pos: Option,
        _player: PlayerID,
    ) -> Result {
        if !self.unlocked && pos == Some(self.location) {
            c.packet_out.push_back(PacketC::ServerMessage {
                message: trm!(
                    "s.campaign.unlock_condition",
                    s = self.condition.show(c.scoreboard) // TODO localize
                ),
                error: false,
            });
            return Ok(true);
        }
        Ok(false)
    }
}
impl 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).map_or(false, |s| {
                s.best.first().map_or(false, |b| b.score.stars >= *thres)
            }),
        }
    }
    pub fn show(&self, scoreboard: &ScoreboardStore) -> String {
        match self {
            GateCondition::All(cs) => {
                let mut o = String::new();
                for c in cs {
                    if !c.check(scoreboard) {
                        writeln!(o, "- {}", c.show(scoreboard)).unwrap();
                    }
                }
                o
            }
            GateCondition::Any(cs) => {
                let mut o = String::new();
                for c in cs {
                    writeln!(o, "- {}", c.show(scoreboard)).unwrap();
                }
                o
            }
            GateCondition::Stars(map, thres) => {
                // TODO what if the language wants map first?
                // trm!(
                //     "s.campaign.condition.stars",
                //     s = thres.to_string(),
                //     s = map.to_string()
                // )
                format!("Reach at least {thres} stars in {map}")
            }
        }
    }
}