// Steam Integration Example
// This example shows how to use Steam achievements and stats

var steam = use("steam");

// Achievement names (these should match your Steam app configuration)
var ACHIEVEMENTS = {
  FIRST_WIN: "ACH_FIRST_WIN",
  PLAY_10_GAMES: "ACH_PLAY_10_GAMES",
  HIGH_SCORE: "ACH_HIGH_SCORE_1000"
};

// Stat names
var STATS = {
  GAMES_PLAYED: "stat_games_played",
  TOTAL_SCORE: "stat_total_score",
  PLAY_TIME: "stat_play_time"
};

var steam_available = false;
var stats_loaded = false;

// Initialize Steam
function init_steam() {
  if (!steam) {
    log.console("Steam module not available");
    return false;
  }
  
  log.console("Initializing Steam...");
  steam_available = steam.steam_init();
  
  if (steam_available) {
    log.console("Steam initialized successfully");
    
    // Request current stats/achievements
    if (steam.stats.stats_request()) {
      log.console("Stats requested");
      stats_loaded = true;
    }
  } else {
    log.console("Failed to initialize Steam");
  }
  
  return steam_available;
}

// Update Steam (call this regularly, e.g., once per frame)
function update_steam() {
  if (steam_available) {
    steam.steam_run_callbacks();
  }
}

// Unlock an achievement
function unlock_achievement(achievement_name) {
  if (!steam_available || !stats_loaded) return false;
  
  // Check if already unlocked
  var unlocked = steam.achievement.achievement_get(achievement_name);
  if (unlocked) {
    log.console("Achievement already unlocked:", achievement_name);
    return true;
  }
  
  // Unlock it
  if (steam.achievement.achievement_set(achievement_name)) {
    log.console("Achievement unlocked:", achievement_name);
    
    // Store stats to make it permanent
    steam.stats.stats_store();
    return true;
  }
  
  return false;
}

// Update a stat
function update_stat(stat_name, value, is_float) {
  if (!steam_available || !stats_loaded) return false;
  
  var success = null;
  if (is_float) {
    success = steam.stats.stats_set_float(stat_name, value);
  } else {
    success = steam.stats.stats_set_int(stat_name, value);
  }
  
  if (success) {
    log.console("Stat updated:", stat_name, "=", value);
    steam.stats.stats_store();
  }
  
  return success;
}

// Get a stat value
function get_stat(stat_name, is_float) {
  if (!steam_available || !stats_loaded) return 0;
  
  if (is_float) {
    return steam.stats.stats_get_float(stat_name) || 0;
  } else {
    return steam.stats.stats_get_int(stat_name) || 0;
  }
}

// Example game logic
var games_played = 0;
var total_score = 0;
var current_score = 0;

function start_game() {
  games_played = get_stat(STATS.GAMES_PLAYED, false);
  total_score = get_stat(STATS.TOTAL_SCORE, false);
  current_score = 0;
  
  log.console("Starting game #" + (games_played + 1));
}

function end_game(score) {
  current_score = score;
  games_played++;
  total_score += score;
  
  // Update stats
  update_stat(STATS.GAMES_PLAYED, games_played, false);
  update_stat(STATS.TOTAL_SCORE, total_score, false);
  
  // Check for achievements
  if (games_played == 1) {
    unlock_achievement(ACHIEVEMENTS.FIRST_WIN);
  }
  
  if (games_played >= 10) {
    unlock_achievement(ACHIEVEMENTS.PLAY_10_GAMES);
  }
  
  if (score >= 1000) {
    unlock_achievement(ACHIEVEMENTS.HIGH_SCORE);
  }
}

// Cloud save example
function save_to_cloud(save_data) {
  if (!steam_available) return false;
  
  var json_data = json.encode(save_data);
  return steam.cloud.cloud_write("savegame.json", json_data);
}

function load_from_cloud() {
  if (!steam_available) return null;
  
  var data = steam.cloud.cloud_read("savegame.json");
  var json_str = null;
  if (data) {
    json_str = text(data)
    return json.decode(json_str);
  }
  
  return null;
}

// Cleanup
function cleanup_steam() {
  if (steam_available) {
    steam.steam_shutdown();
    log.console("Steam shut down");
  }
}

// Initialize on start
init_steam();