use std::time::Duration;

use crate::cloud::gcp::vpc::subnet::models::SubnetFingerprint;

/// Get info about subnetwork
/// Project ID, token, need to be provided
pub async fn get_subnetwork_info(
    token: String,
    project: String,
    subnet_region: String,
    subnet_name: String
) -> Result<(), std::io::Error> {

    let client = awc::Client::default();
    let request = client.get(format!("https://compute.googleapis.com/compute/v1/projects/{project}/regions/{subnet_region}/subnetworks/{subnet_name}"))
        .bearer_auth(&token)
        .insert_header(("Content-Type", "application/json"))
        .timeout(Duration::from_secs(30))
        .send()
        .await
        .expect("Request GET subnet name could not been sent");

    let mut req = request;
    let req_status = req.status().as_u16();
    let respone = req.body().await.unwrap_or_default();

    match req_status {
        200 => {
            println!("Request has been successfull: Status: {:?}, {:?}", req_status, respone);
        },
        400 => {
            println!("Bad Request. Check URL parameters or body: {:?}", respone);
        },
        403 => {
            println!("You don't have access to perform such request: {:?}", respone);
        }
        404 => {
            println!("Requested resource does not exists: {:?}", respone);
        },
        409 => {
            println!("Requested resource already exists! {:?}", respone)
        }
        _ => {
            println!("Request status mismatch. Check response: {:?}", respone);
        }
    }

    Ok(())

}

/// Get fingerprint of subnetwork
/// Project ID, token, need to be provided
pub async fn get_subnetwork_fingerprint(
    token: String,
    project: String,
    subnet_region: String,
    subnet_name: String
) -> Result<String, std::io::Error> {


    let client = awc::Client::default();
    let request = client.get(format!("https://compute.googleapis.com/compute/v1/projects/{project}/regions/{subnet_region}/subnetworks/{subnet_name}"))
        .bearer_auth(&token)
        .insert_header(("Content-Type", "application/json"))
        .timeout(Duration::from_secs(30))
        .send()
        .await
        .expect("Request GET subnet fingerprint could not been sent")
        .json::<SubnetFingerprint>()
        .await
        .unwrap_or_default();

    Ok(request.fingerprint)
    
}
