use chrono::{DateTime, Timelike, Utc};
use wasm_bindgen::JsValue;
use crate::rescaleCanvas;
use crate::scope::ScopeState;
use crate::utils::parse_date;
use crate::vcp::vcp_string;

pub const TEXT_COLOR_RED: &str = "#ef0000";
pub const TEXT_COLOR_GREEN: &str = "#4af626";
pub const TEXT_COLOR_WHITE: &str = "#dedede";

pub fn time(h: usize, m: usize, s: usize, tag_end: &str) -> String {
    format!("{:0>2}:{:0>2}:{:0>2}{}", h, m, s, tag_end)
}
pub fn hms(t: DateTime<Utc>) -> (usize, usize, usize) {
    (t.hour() as usize, t.minute() as usize, t.second() as usize)
}
pub fn zulu(t: DateTime<Utc>) -> String {
    let t2 = hms(t);
    time(t2.0, t2.1, t2.2, "Z")
}

pub fn render(state: &mut ScopeState) -> Result<(), JsValue> {
    // DPI rescaling
    rescaleCanvas(&state.render_state.canvas, &state.render_state.context);

    let ctx = &state.render_state.context;
    let canvas = &state.render_state.canvas;

    ctx.set_font(&format!("{}px monospace", state.prefs.fcs));

    // clear canvas
    ctx.reset_transform()?;
    ctx.clear_rect(0.0, 0.0, canvas.width() as f64, canvas.height() as f64);

    // render out the background
    ctx.set_fill_style(&JsValue::from_str("black"));
    ctx.fill_rect(0.0, 0.0, canvas.width() as f64, canvas.height() as f64);

    let time = Utc::now();

    ctx.set_fill_style(&JsValue::from_str(TEXT_COLOR_GREEN));

    // render out the scope id line
    ctx.fill_text(&format!("NEXRAD {} {}", state.ar2.as_ref().map(|u| u.volume_header_record.icao.as_str()).unwrap_or("INOP"), zulu(time)), 50.0, 50.0)?;

    if state.ar2.is_none() {
        // inop alert
        ctx.set_fill_style(&JsValue::from_str(TEXT_COLOR_RED));
        ctx.fill_text("RADAR INOPERATIVE NO DATA LOADED", 50.0, 50.0 + (state.prefs.fcs as f64))?;
        ctx.set_fill_style(&JsValue::from_str(TEXT_COLOR_GREEN));
    }
    if state.new_data_available {
        // inop alert
        ctx.set_fill_style(&JsValue::from_str(TEXT_COLOR_RED));
        ctx.fill_text("NEW DATA AVAIL RLD RQD", 50.0, 50.0 + (state.prefs.fcs as f64) * 2.0)?;
        ctx.set_fill_style(&JsValue::from_str(TEXT_COLOR_GREEN));
    }

    // render the command buffer
    for (line_no, line) in state.command_buf.split('\n').enumerate() {
        ctx.fill_text(line, 50.0, (canvas.height() / 3) as f64 + (state.prefs.fcs * line_no) as f64)?;
    }

    // render the radar site info
    ctx.set_text_align("right");
    ctx.fill_text("RADAR SITE", (canvas.width() - 50) as f64, 50.0)?;
    if let Some(ar2) = &state.ar2 {
        ctx.fill_text(&format!("{} VCP {} {}", ar2.volume_header_record.icao, ar2.meta_rda_status_data.vcp, vcp_string(ar2.meta_rda_status_data.vcp)), (canvas.width() - 50) as f64, 50.0 + (state.prefs.fcs as f64))?;
        let recorded = parse_date(ar2.volume_header_record.date, ar2.volume_header_record.time);
        let delay = time - recorded;
        ctx.fill_text(&format!("SITE DELAY {}:{}:{}", delay.num_hours(), delay.num_minutes() - (delay.num_hours() * 60), delay.num_seconds() - (delay.num_minutes() - (delay.num_hours() * 60) * 60)), (canvas.width() - 50) as f64, 50.0 + (state.prefs.fcs as f64 * 2.0))?;
    } else {
        ctx.fill_text("SITE INFORMATION UNAVAILABLE", (canvas.width() - 50) as f64, 50.0 + state.prefs.fcs as f64)?;
        ctx.fill_text("DELAY INFORMATION UNAVAILABLE", (canvas.width() - 50) as f64, 50.0 + (state.prefs.fcs * 2) as f64)?;
    }


    Ok(())
}