use bevy::prelude::*;
use bevy::window::PrimaryWindow;
use bevy_remote::BrpError;
use bevy_remote::BrpResult;
use bevy_remote::error_codes::INTERNAL_ERROR;
use bevy_remote::error_codes::INVALID_PARAMS;
use serde_json::Value;
use serde_json::json;
pub fn handler(In(params): In<Option<Value>>, world: &mut World) -> BrpResult {
let title = params
.as_ref()
.and_then(|p| p.get("title"))
.and_then(|t| t.as_str())
.ok_or_else(|| BrpError {
code: INVALID_PARAMS,
message: "Missing or invalid 'title' parameter".to_string(),
data: None,
})?;
let mut query = world.query_filtered::<&mut Window, With<PrimaryWindow>>();
let mut window = query.single_mut(world).map_err(|_| BrpError {
code: INTERNAL_ERROR,
message: "No primary window found".to_string(),
data: None,
})?;
let old_title = window.title.clone();
window.title = title.to_string();
Ok(json!({
"status": "success",
"old_title": old_title,
"new_title": title,
"message": format!("Window title changed from '{old_title}' to '{title}'")
}))
}