//! Steam-User Example - Rich Presence
//!
//! Demonstrates setting and updating rich presence for a game.
//!
//! # Usage
//! ```bash
//! REFRESH_TOKEN="your_refresh_token" cargo run --example rich_presence
//! ```

use std::{collections::HashMap, time::Duration};

use steam_client::{EPersonaState, LogOnDetails, SteamClient};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize logging
    tracing_subscriber::fmt().with_max_level(tracing::Level::INFO).init();

    let refresh_token = std::env::var("REFRESH_TOKEN").expect("Please set REFRESH_TOKEN environment variable");

    let mut client = SteamClient::new(Default::default());

    println!("🎮 Rich Presence Example\n");

    // Log on
    let response = client.log_on(LogOnDetails { refresh_token: Some(refresh_token), ..Default::default() }).await?;

    println!("✅ Logged in as {}", response.steam_id.steam3());

    // Set online
    client.set_persona(EPersonaState::Online, None).await?;

    // Start playing a game (Team Fortress 2)
    let app_id = 440u32;
    client.games_played(vec![app_id]).await?;
    println!("🎮 Now playing Team Fortress 2 (AppID {})", app_id);

    // Set initial rich presence
    let mut presence = HashMap::new();
    presence.insert("status".to_string(), "Main Menu".to_string());
    presence.insert("steam_display".to_string(), "#TF_RichPresence_Display".to_string());

    client.upload_rich_presence(app_id, &presence).await?;
    println!("📝 Set rich presence: Main Menu");

    // Wait a bit
    tokio::time::sleep(Duration::from_secs(3)).await;

    // Update rich presence - now in a match
    let mut presence = HashMap::new();
    presence.insert("status".to_string(), "Playing Capture the Flag".to_string());
    presence.insert("map".to_string(), "ctf_2fort".to_string());
    presence.insert("steam_display".to_string(), "#TF_RichPresence_Display".to_string());
    presence.insert("steam_player_group".to_string(), "12345".to_string());
    presence.insert("steam_player_group_size".to_string(), "12".to_string());

    client.upload_rich_presence(app_id, &presence).await?;
    println!("📝 Updated rich presence: Playing on 2Fort");

    // Wait a bit more
    tokio::time::sleep(Duration::from_secs(3)).await;

    // Update again - with score
    let mut presence = HashMap::new();
    presence.insert("status".to_string(), "In Match - Score: 2-1".to_string());
    presence.insert("map".to_string(), "ctf_2fort".to_string());
    presence.insert("class".to_string(), "Scout".to_string());
    presence.insert("steam_display".to_string(), "#TF_RichPresence_Display".to_string());

    client.upload_rich_presence(app_id, &presence).await?;

    println!("📝 Updated rich presence: In match as Scout");

    // Keep running for a bit to let friends see the presence
    println!("\n⏳ Keeping rich presence active for 10 seconds...");
    println!("   (Your friends should see this on your profile)\n");

    tokio::time::sleep(Duration::from_secs(10)).await;

    // Clear rich presence
    client.clear_rich_presence(app_id).await?;
    println!("🧹 Cleared rich presence");

    // Stop playing
    client.games_played(vec![]).await?;
    println!("🛑 Stopped playing");

    // Log off
    client.log_off().await?;
    println!("\n👋 Done!");

    Ok(())
}
