use crate::loadar2;
use crate::mode::Mode;
use crate::rendering::scope::ScopeState;


pub fn should_newline(state: &mut ScopeState) -> bool {
    state.command_buf.starts_with("MODE SET") || state.command_buf.starts_with("ELEVATION SET") || state.command_buf.starts_with("FCS SET") || state.command_buf.starts_with("RANGE SET")
}

pub fn exec(state: &mut ScopeState, command: String) {
    let tokens = command.split(' ').collect::<Vec<_>>();
    if command == "CLF OV" {
        state.file_input.click();
    } else if command == "CLF RELOAD" {
        state.command_buf = "SYSTEM PROCESSING".to_string();
        state.command_buf_response_mode = true;
        loadar2("file");
    } else if command == "FCS" {
        state.command_buf = state.prefs.fcs.to_string();
        state.command_buf_response_mode = true;
        return;
    } else if command == "RANGE" {
        state.command_buf = state.prefs.range.to_string();
        state.command_buf_response_mode = true;
        return;
    } else if command.starts_with("FCS SET") {
        if tokens.len() < 3 {
            state.command_buf = "ARGUMENT INVALID".to_string();
            state.command_buf_response_mode = true;
            return;
        }
        let new_val: f32 = match tokens[2].parse() {
            Ok(v) => v,
            Err(_) => {
                state.command_buf = "ARGUMENT INVALID".to_string();
                state.command_buf_response_mode = true;
                return;
            }
        };
        state.prefs.fcs = new_val;
        return;
    } else if command.starts_with("RANGE SET") {
        if tokens.len() < 3 {
            state.command_buf = "ARGUMENT INVALID".to_string();
            state.command_buf_response_mode = true;
            return;
        }
        let new_val: f32 = match tokens[2].parse() {
            Ok(v) => v,
            Err(_) => {
                state.command_buf = "ARGUMENT INVALID".to_string();
                state.command_buf_response_mode = true;
                return;
            }
        };
        state.prefs.range = new_val;
        state.recalculate_scope_display();
        return;
    } else if command.starts_with("MODE SET") {
        if tokens.len() < 3 {
            state.command_buf = "ARGUMENT INVALID".to_string();
            state.command_buf_response_mode = true;
            return;
        } else {
            let mode = tokens[2];
            if let Some(ar2) = &state.ar2 {
                let types = ar2.elevations.get(&state.selected_elevation).unwrap();
                let valid_modes: Vec<_> = types[0].available_data.keys().collect();

                if !valid_modes.contains(&&mode.to_string()) {
                    state.command_buf = "ARGUMENT INVALID".to_string();
                    state.command_buf_response_mode = true;
                    return;
                }

                state.scope_mode = match mode {
                    "REF" => Mode::Reflectivity,
                    "VEL" => Mode::Velocity,
                    "SW" => Mode::SpectrumWidth,
                    "ZDR" => Mode::DifferentialReflectivity,
                    "PHI" => Mode::DifferentialPhase,
                    "RHO" => Mode::CorrelationCoefficient,
                    "CFP" => Mode::ClutterFilterPowerRemoved,
                    _ => unreachable!(),
                };

                state.recalculate_scope_display();
            } else {
                state.command_buf = "CANNOT SET MODE\nRADAR INOPERATIVE".to_string();
                state.command_buf_response_mode = true;
                return;
            }
        }
    } else if command.starts_with("ELEVATION SET") {
        if tokens.len() < 3 {
            state.command_buf = "ARGUMENT INVALID".to_string();
            state.command_buf_response_mode = true;
            return;
        } else {
            let elev = tokens[2].to_string();
            if let Some(ar2) = &state.ar2 {
                let mut valid_elevs = vec![];
                for elev in ar2.elevations.keys() {
                    valid_elevs.push(format!("{:0>3}", elev));
                }
                if !valid_elevs.contains(&elev) {
                    state.command_buf = "ARGUMENT INVALID".to_string();
                    state.command_buf_response_mode = true;
                    return;
                }
                let number: usize = elev.parse().unwrap();

                // reset scope back to reflectivity which is included in every elevation
                state.scope_mode = Mode::Reflectivity;
                state.selected_elevation = number;
                state.recalculate_scope_display();
            } else {
                state.command_buf = "CANNOT SET MODE\nRADAR INOPERATIVE".to_string();
                state.command_buf_response_mode = true;
                return;
            }
        }
    } else if command == "`T" {
        state.show_ui = !state.show_ui;
    } else {
        state.command_buf = "UNRECOGNIZED COMMAND".to_string();
        state.command_buf_response_mode = true;
    }
}
