use anyhow::{Context, Result};
use lesavka_common::lesavka::{
    CapturePowerCommand, Empty, SetCapturePowerRequest, relay_client::RelayClient,
};
use tonic::{Request, transport::Channel};

use super::state::CapturePowerStatus;

pub fn fetch_capture_power(server_addr: &str) -> Result<CapturePowerStatus> {
    with_runtime(async move {
        let mut client = connect(server_addr).await?;
        let reply = client
            .get_capture_power(Request::new(Empty {}))
            .await
            .context("querying capture power state")?
            .into_inner();
        Ok(map_state(reply))
    })
}

pub fn set_capture_power_mode(
    server_addr: &str,
    command: CapturePowerCommand,
) -> Result<CapturePowerStatus> {
    with_runtime(async move {
        let mut client = connect(server_addr).await?;
        let reply = client
            .set_capture_power(Request::new(SetCapturePowerRequest {
                enabled: matches!(command, CapturePowerCommand::ForceOn),
                command: command as i32,
            }))
            .await
            .context("setting capture power state")?
            .into_inner();
        Ok(map_state(reply))
    })
}

pub fn reset_usb_gadget(server_addr: &str) -> Result<()> {
    with_runtime(async move {
        let mut client = connect(server_addr).await?;
        let reply = client
            .reset_usb(Request::new(Empty {}))
            .await
            .context("requesting USB gadget reset")?
            .into_inner();
        if reply.ok {
            Ok(())
        } else {
            anyhow::bail!("relay reported USB reset failure");
        }
    })
}

fn with_runtime<F, T>(future: F) -> Result<T>
where
    F: std::future::Future<Output = Result<T>>,
{
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .context("building launcher power runtime")?
        .block_on(future)
}

async fn connect(server_addr: &str) -> Result<RelayClient<Channel>> {
    let channel = Channel::from_shared(server_addr.to_string())
        .context("invalid launcher server address")?
        .tcp_nodelay(true)
        .connect()
        .await
        .context("connecting launcher to relay host")?;
    Ok(RelayClient::new(channel))
}

fn map_state(reply: lesavka_common::lesavka::CapturePowerState) -> CapturePowerStatus {
    CapturePowerStatus {
        available: reply.available,
        enabled: reply.enabled,
        unit: reply.unit,
        detail: reply.detail,
        active_leases: reply.active_leases,
        mode: reply.mode,
        detected_devices: reply.detected_devices,
    }
}
