use bytes::Bytes;
use http::{request, response, Request, Response};
use std::path::PathBuf;
use std::sync::Arc;

use axum::body::{to_bytes, Body};

use axum::Router;

use tauri::path::BaseDirectory;
use tauri::Manager;
use tokio::sync::{Mutex, MutexGuard};
use tower::{Service, ServiceExt};

async fn process_tauri_request(
    tauri_request: Request<Vec<u8>>,
    mut router: MutexGuard<'_, Router>,
) -> Response<Vec<u8>> {
    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
        .expect("Failed to get ready service from router")
        .call(axum_request)
        .await
        .expect("Could not get response from router");

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

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

    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("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;
                let response = process_tauri_request(request, router).await;
                responder.respond(response);
            });
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
