//! LQP Client state management.
//!
//! Defines the game flow phases and client state tracking.

/// Game flow phase states.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum GameflowPhase {
    /// Client is in main menu or lobby.
    #[default]
    None,
    /// In lobby.
    Lobby,
    /// In queue.
    Queue,
    /// Match found, ready check.
    ReadyCheck,
    /// In champion select.
    ChampSelect,
    /// Game is starting.
    GameStart,
    /// In game.
    InProgress,
    /// Game ended, waiting for stats.
    WaitingForStats,
    /// End of game stats screen.
    EndOfGame,
    /// Unknown phase.
    Unknown,
}

impl From<&str> for GameflowPhase {
    fn from(s: &str) -> Self {
        match s {
            "None" => GameflowPhase::None,
            "Lobby" => GameflowPhase::Lobby,
            "Queue" => GameflowPhase::Queue,
            "ReadyCheck" => GameflowPhase::ReadyCheck,
            "ChampSelect" => GameflowPhase::ChampSelect,
            "GameStart" => GameflowPhase::GameStart,
            "InProgress" => GameflowPhase::InProgress,
            "WaitingForStats" => GameflowPhase::WaitingForStats,
            "EndOfGame" => GameflowPhase::EndOfGame,
            _ => GameflowPhase::Unknown,
        }
    }
}

/// LQP Client state.
#[derive(Debug, Clone)]
pub struct ClientState {
    /// Current gameflow phase.
    pub phase: GameflowPhase,
    /// Current game ID if in game.
    pub game_id: Option<u64>,
    /// Current champion name.
    pub champion: Option<String>,
    /// Current player's puuid.
    pub local_puuid: Option<String>,
}

impl Default for ClientState {
    fn default() -> Self {
        Self {
            phase: GameflowPhase::None,
            game_id: None,
            champion: None,
            local_puuid: None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_gameflow_phase_from_str() {
        assert_eq!(GameflowPhase::from("InProgress"), GameflowPhase::InProgress);
        assert_eq!(
            GameflowPhase::from("ChampSelect"),
            GameflowPhase::ChampSelect
        );
        assert_eq!(GameflowPhase::from("UnknownPhase"), GameflowPhase::Unknown);
    }

    #[test]
    fn test_client_state_default() {
        let state = ClientState::default();
        assert_eq!(state.phase, GameflowPhase::None);
        assert!(state.game_id.is_none());
        assert!(state.champion.is_none());
        assert!(state.local_puuid.is_none());
    }
}
