use crate::url::Url;
use http::Uri;

pub fn url_authority(in_url: Url) -> String {
    in_url.authority().into()
}

pub fn make_url(in_url: Url, scheme: String, path: String) -> Url {
    let cur_url = in_url.clone();
    let uri = Uri::builder()
        .authority(cur_url.authority())
        .path_and_query(path)
        .scheme(scheme.as_str())
        .build()
        .unwrap();
    Url::parse(uri.to_string().as_str()).unwrap()
}

pub fn titan_url(in_url: Url, path: &String) -> Url {
    let cur_url = in_url.clone();
    let uri = Uri::builder()
        .authority(cur_url.authority())
        .path_and_query(path)
        .scheme("titan")
        .build()
        .unwrap();
    Url::parse(uri.to_string().as_str()).unwrap()
}

pub fn url_inv_scheme(in_url: Url) -> Url {
    let cur_url = in_url.clone();

    let dst_scheme = match cur_url.scheme() {
        "gemini" => "titan",
        "titan" => "gemini",
        &_ => "gemini",
    };

    let uri = Uri::builder()
        .authority(cur_url.authority())
        .path_and_query(cur_url.path())
        .scheme(dst_scheme)
        .build()
        .unwrap();
    println!("{} => {}", in_url, uri);

    Url::parse(uri.to_string().as_str()).unwrap()
}
