pub(crate) mod sources;

mod grib2;
mod pixmap;
mod tile;

use std::collections::{BTreeMap};
use std::fmt::Debug;
use tokio::sync::RwLock;
use std::time::SystemTime;
use actix_web::{App, HttpServer};
use actix_web::web::Data;
use crate::grib2::{LookupTable2D};
use crate::sources::noaa::mrms_cref;

#[global_allocator]
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;

#[repr(usize)]
#[derive(Ord, PartialOrd, Eq, PartialEq, Debug, Clone, Copy)]
pub enum LutKey {
    NoaaMrmsMergedCompositeReflectivityQc = 1
}

pub struct AppState {
    lut_cache: RwLock<BTreeMap<LutKey, LookupTable2D>>,
    lut_cache_timestamps: RwLock<BTreeMap<LutKey, SystemTime>>
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let f = reqwest::get("https://mrms.ncep.noaa.gov/data/2D/ReflectivityAtLowestAltitude/MRMS_ReflectivityAtLowestAltitude.latest.grib2.gz").await.unwrap();



    let data = Data::new(AppState {
        lut_cache: RwLock::new(BTreeMap::new()),
        lut_cache_timestamps: RwLock::new(BTreeMap::new())
    });
    HttpServer::new(move || {
        App::new()
            .service(mrms_cref)
            .app_data(data.clone())
    })
        .bind(("::", 8080))?
        .run()
        .await

}

