use std::sync::Arc;
use clap::{Args, Subcommand};
use holodeck_core::models::SimulatorState;
use holodeck_core::{LiveSimctlClient, SimctlClient};
use holodeck_services::SimulatorService;
use crate::resolve::resolve_in_state;
#[derive(Args, Debug)]
pub struct LocationArgs {
#[command(subcommand)]
pub command: LocationSubcommand,
}
#[derive(Subcommand, Debug)]
pub enum LocationSubcommand {
Set(SetArgs),
Clear(ClearArgs),
}
#[derive(Args, Debug)]
pub struct SetArgs {
pub query: String,
pub latitude: f64,
pub longitude: f64,
}
#[derive(Args, Debug)]
pub struct ClearArgs {
pub query: String,
}
impl LocationArgs {
pub async fn run(&self) -> anyhow::Result<()> {
match &self.command {
LocationSubcommand::Set(args) => args.run().await,
LocationSubcommand::Clear(args) => args.run().await,
}
}
}
impl SetArgs {
pub async fn run(&self) -> anyhow::Result<()> {
let client: Arc<dyn SimctlClient> = Arc::new(LiveSimctlClient::new());
let service = SimulatorService::new(client.clone());
let sim = resolve_in_state(&service, &self.query, SimulatorState::Booted, "the simulator must be booted").await?;
client.set_location(sim.id, self.latitude, self.longitude).await?;
println!("Set {} location to {},{}.", sim.name, self.latitude, self.longitude);
Ok(())
}
}
impl ClearArgs {
pub async fn run(&self) -> anyhow::Result<()> {
let client: Arc<dyn SimctlClient> = Arc::new(LiveSimctlClient::new());
let service = SimulatorService::new(client.clone());
let sim = resolve_in_state(&service, &self.query, SimulatorState::Booted, "the simulator must be booted").await?;
client.clear_location(sim.id).await?;
println!("Cleared location on {}.", sim.name);
Ok(())
}
}