use crate::app::core::{Action, Action::*};
use crate::app::model::Model;
use crate::views::{propheight, propwidth, rrect, View};
use crate::MainState;
use esox::domain::index::Indice;
use raylib::consts::GuiIconName::{ICON_BIN, ICON_FILE_OPEN};
use raylib::drawing::RaylibDrawHandle;
use raylib::prelude::*;
use raylib::RaylibThread;
use rfd::FileDialog;
pub(crate) struct SelezioneFileInputView {}
impl View for SelezioneFileInputView {
fn draw(
&mut self,
d: &mut RaylibDrawHandle,
_thread: &RaylibThread,
state: &Model,
main_state: &MainState,
) -> Vec<Action> {
self.draw_background(d, main_state);
let current_index = match state.indice_model.get_selected_index() {
Some(index) => index,
None => {
eprintln!("SelezioneFileInputView: Per qualche assurdo motivo l'indice corrente non รจ validato. Uso NISECI.");
Indice::Niseci
}
};
let button_riferimento_width = propwidth(d, 200);
let button_riferimento_x = d.get_screen_width() / 2 - button_riferimento_width / 2;
let button_riferimento_height = propwidth(d, 50);
let button_fileinput_y_spacing = button_riferimento_height;
let button_riferimento_y =
d.get_screen_height() / 2 - button_fileinput_y_spacing / 2 - button_riferimento_height;
let button_campionamento_width = button_riferimento_width;
let button_campionamento_x = button_riferimento_x;
let button_campionamento_height = button_riferimento_height;
let button_campionamento_y = match current_index {
Indice::Hfbi => button_riferimento_y + button_fileinput_y_spacing,
Indice::Niseci => {
button_riferimento_y + button_riferimento_height + button_fileinput_y_spacing
}
};
let groupbox_width = button_riferimento_width + propwidth(d, 100);
let groupbox_x = button_riferimento_x - propwidth(d, 50);
let groupbox_height =
button_riferimento_height * 2 + button_fileinput_y_spacing + propheight(d, 100);
let groupbox_y = button_riferimento_y - propheight(d, 50);
d.gui_group_box(
rrect(groupbox_x, groupbox_y, groupbox_width, groupbox_height),
"Seleziona file di input",
);
let mut actions = Vec::<Action>::new();
if current_index != Indice::Hfbi {
if let Some(_filepath) = state.fileinput_model.get_riferimento_path() {
let rif_itext = d.gui_icon_text(ICON_BIN, "Annulla Riferimento");
if d.gui_button(
rrect(
button_riferimento_x,
button_riferimento_y,
button_riferimento_width,
button_riferimento_height,
),
rif_itext.as_str(),
) {
actions.push(PickRiferimentoPath(None));
}
} else {
let rif_itext = d.gui_icon_text(ICON_FILE_OPEN, "Riferimento");
if d.gui_button(
rrect(
button_riferimento_x,
button_riferimento_y,
button_riferimento_width,
button_riferimento_height,
),
rif_itext.as_str(),
) {
let file = FileDialog::new()
.add_filter("csv", &["csv"])
.set_directory("/")
.pick_file();
if let Some(filepath) = file {
actions.push(PickRiferimentoPath(Some(filepath)));
} else {
eprintln!("Error: failed getting a file.");
}
}
}
}
if let Some(_filepath) = state.fileinput_model.get_campionamento_path() {
let camp_itext = d.gui_icon_text(ICON_BIN, "Annulla Campionamento");
if d.gui_button(
rrect(
button_campionamento_x,
button_campionamento_y,
button_campionamento_width,
button_campionamento_height,
),
camp_itext.as_str(),
) {
actions.push(PickCampionamentoPath(None));
}
} else {
let camp_itext = d.gui_icon_text(ICON_FILE_OPEN, "Campionamento");
if d.gui_button(
rrect(
button_campionamento_x,
button_campionamento_y,
button_campionamento_width,
button_campionamento_height,
),
camp_itext.as_str(),
) {
let file = FileDialog::new()
.add_filter("csv", &["csv"])
.set_directory("/")
.pick_file();
if let Some(filepath) = file {
actions.push(PickCampionamentoPath(Some(filepath)));
} else {
eprintln!("Error: failed getting a file.");
}
}
}
actions
}
}
impl SelezioneFileInputView {
pub(crate) fn new() -> Self {
Self {}
}
}