use axum::body::{to_bytes, Body};
use axum::Router;
use bytes::Bytes;
use http::{request, response, Request, Response};
use std::path::PathBuf;
use std::sync::Arc;
use tauri::path::BaseDirectory;
use tauri::Manager;
use thiserror::Error;
use tokio::sync::{Mutex, MutexGuard};
use tower::{Service, ServiceExt};

#[derive(Error, Debug)]
pub enum DesktopError {
    #[error("Axum error:\n{0}")]
    Axum(#[from] axum::Error),
    #[error("Infallible error")]
    Infallible(#[from] std::convert::Infallible),
}

/// Process requests sent to Tauri (with the `axum://` protocol) and handle them with Axum
/// When an error occurs, this function is expected to panic, which should result in a 500 error
/// being sent to the client, so we let the client handle the error recovering
async fn process_tauri_request(
    tauri_request: Request<Vec<u8>>,
    mut router: MutexGuard<'_, Router>,
) -> Result<Response<Vec<u8>>, DesktopError> {
    let (parts, body): (request::Parts, Vec<u8>) = tauri_request.into_parts();
    let axum_request: Request<Body> = Request::from_parts(parts, body.into());

    let axum_response: Response<Body> = router
        .as_service()
        .ready()
        .await
        .map_err(DesktopError::Infallible)?
        .call(axum_request)
        .await
        .map_err(DesktopError::Infallible)?;

    let (parts, body): (response::Parts, Body) = axum_response.into_parts();
    let body: Bytes = to_bytes(body, usize::MAX).await?;

    let tauri_response: Response<Vec<u8>> = Response::from_parts(parts, body.into());
    Ok(tauri_response)
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .setup(|app| {
            let assets_path: PathBuf = app
                .path()
                .resolve("assets", BaseDirectory::Resource)
                .expect("Assets path should be resolvable");

            // Adds Axum router to application state
            // This makes it so we can retrieve it from any app instance (see bellow)
            let router = Arc::new(Mutex::new(app::get_router(&assets_path)));

            app.manage(router);

            Ok(())
        })
        .register_asynchronous_uri_scheme_protocol("axum", move |app, request, responder| {
            // Retrieve the router from the application state and clone it for the async block
            let router = Arc::clone(&app.state::<Arc<Mutex<axum::Router>>>());

            // Spawn a new async task to process the request
            tauri::async_runtime::spawn(async move {
                let router = router.lock().await;
                match process_tauri_request(request, router).await {
                    Ok(response) => responder.respond(response),
                    Err(err) => {
                        let body = format!("Failed to process an axum:// request:\n{}", err);
                        responder.respond(
                            http::Response::builder()
                                .status(http::StatusCode::BAD_REQUEST)
                                .header(http::header::CONTENT_TYPE, "text/plain")
                                .body::<Vec<u8>>(body.into())
                                .expect("BAD_REQUEST response should be valid"),
                        )
                    }
                }
            });
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}