#include "view_set_disk.h"

view_device_set_window *obj_view_device_widgets = NULL;
char* view_device_glade_path = NULL;
int view_device_index = 0;
char* view_device_set_add_edit = NULL;
vector* vec_device = NULL;
config_all* view_device_all_config_temp = NULL;

void set_view_device_menu(char* _set_add_edit) {
    view_device_set_add_edit = _set_add_edit;
}
void view_device_set_vector_device(vector* _vec_device) {
    vec_device = _vec_device;
}
view_device_set_window* get_widget_view_device() {
    return obj_view_device_widgets;
}

void view_device_all_config(config_all* _all_config) {
    view_device_all_config_temp = _all_config;
}

void view_device_set_index(int _index) {
    view_device_index = _index;
}

view_device_set_window *view_device_setup_window_base(char* __glade_path) {
    view_device_glade_path = __glade_path;
    if (obj_view_device_widgets == NULL) {
        obj_view_device_widgets = malloc(sizeof(view_device_set_window));
        GtkBuilder *builder = gtk_builder_new_from_resource(__glade_path);
        obj_view_device_widgets->Window = yon_gtk_builder_get_widget(builder,"SetDeviceWindow");
        obj_view_device_widgets->btnSaveTempSave = yon_gtk_builder_get_widget(builder,"SetDeviceSaveButton");
        obj_view_device_widgets->btnTempCancel = yon_gtk_builder_get_widget(builder,"SetDeviceCancelButton");
        gtk_button_set_label(GTK_BUTTON(obj_view_device_widgets->btnSaveTempSave),SAVE_LABEL);
        view_device_setup_window_custom(obj_view_device_widgets, builder);
        view_device_event(obj_view_device_widgets);
    }
    return obj_view_device_widgets;
}
view_device_set_window *view_device_setup_window_custom(view_device_set_window* view_device_view_device_widgets, GtkBuilder *builder) {
    obj_view_device_widgets->spinWeeksFile = yon_gtk_builder_get_widget(builder,"spinWeeksFile");
    obj_view_device_widgets->spinDaysFile = yon_gtk_builder_get_widget(builder,"spinDaysFile");
    obj_view_device_widgets->spinHoursFile = yon_gtk_builder_get_widget(builder,"spinHoursFile");
    obj_view_device_widgets->spinMinutesFile = yon_gtk_builder_get_widget(builder,"spinMinutesFile");

    obj_view_device_widgets->spinWeeksSize = yon_gtk_builder_get_widget(builder,"spinWeeksSize");
    obj_view_device_widgets->spinDaysSize = yon_gtk_builder_get_widget(builder,"spinDaysSize");
    obj_view_device_widgets->spinHoursSize = yon_gtk_builder_get_widget(builder,"spinHoursSize");
    obj_view_device_widgets->spinMinutesSize = yon_gtk_builder_get_widget(builder,"spinMinutesSize");
    obj_view_device_widgets->lblNameDevice = yon_gtk_builder_get_widget(builder,"lblNameDevice");
    obj_view_device_widgets->cmbDeviceSetStatus = yon_gtk_builder_get_widget(builder,"cmbDeviceSetStatus");
    obj_view_device_widgets->DeviceCombo = yon_gtk_builder_get_widget(builder,"DeviceCombo");
    obj_view_device_widgets->lblHeadQuotasEditDeviceWindow = yon_gtk_builder_get_widget(builder,"lblHeadQuotasEditDeviceWindow");
    obj_view_device_widgets->boxBlockGuiAddEditDevice = yon_gtk_builder_get_widget(builder,"boxBlockGuiAddEditDevice");
    obj_view_device_widgets->boxBlockDisabledStatus = yon_gtk_builder_get_widget(builder,"boxBlockDisabledStatus");
    if (strcmp(view_device_set_add_edit, "add") == 0) {
        gtk_label_set_label(GTK_LABEL(obj_view_device_widgets->lblHeadQuotasEditDeviceWindow), STR_TITLE_ADD_QUOTAS);
        gtk_widget_hide(obj_view_device_widgets->lblNameDevice);
        device_fill_disk(obj_view_device_widgets->DeviceCombo,vec_device);
        gtk_combo_box_set_active(GTK_COMBO_BOX(obj_view_device_widgets->cmbDeviceSetStatus),-1);
    }
    else {
        gtk_label_set_label(GTK_LABEL(obj_view_device_widgets->lblHeadQuotasEditDeviceWindow), STR_TITLE_EDIT_QUOTAS);
        gtk_widget_hide(obj_view_device_widgets->DeviceCombo);
    }
    view_device_fill_gui();
    view_device_set_combo_box_device();
}

void view_device_show(GtkWidget *self, char* __glade_path) {
    if (obj_view_device_widgets != NULL) {
        gtk_widget_show(obj_view_device_widgets->Window);
    }
    else {
        view_device_setup_window_base(__glade_path);
        gtk_widget_show(obj_view_device_widgets->Window);
    }
}

void view_device_save() {
    view_device_get_gui_data();
    view_device_on_destroy_subwindow(obj_view_device_widgets->Window);
}



void view_device_event(view_device_set_window* view_device_view_device_widgets) {
    g_signal_connect(G_OBJECT(obj_view_device_widgets->DeviceCombo), "changed", G_CALLBACK(view_device_set_combo_box_device), NULL);
    g_signal_connect(G_OBJECT(obj_view_device_widgets->Window), "destroy", G_CALLBACK(view_device_destroy), obj_view_device_widgets->Window);
    g_signal_connect(G_OBJECT(obj_view_device_widgets->btnTempCancel),"clicked",G_CALLBACK(view_device_on_destroy_subwindow), NULL);
    g_signal_connect(G_OBJECT(obj_view_device_widgets->spinWeeksSize), "value-changed", G_CALLBACK(view_device_update_gui_spin_time), NULL);
    g_signal_connect(G_OBJECT(obj_view_device_widgets->spinDaysSize), "value-changed", G_CALLBACK(view_device_update_gui_spin_time), NULL);
    g_signal_connect(G_OBJECT(obj_view_device_widgets->spinHoursSize), "value-changed", G_CALLBACK(view_device_update_gui_spin_time), NULL);
    g_signal_connect(G_OBJECT(obj_view_device_widgets->spinMinutesSize), "value-changed", G_CALLBACK(view_device_update_gui_spin_time), NULL);
    g_signal_connect(G_OBJECT(obj_view_device_widgets->spinWeeksFile), "value-changed", G_CALLBACK(view_device_update_gui_spin_time), NULL);
    g_signal_connect(G_OBJECT(obj_view_device_widgets->spinDaysFile), "value-changed", G_CALLBACK(view_device_update_gui_spin_time), NULL);
    g_signal_connect(G_OBJECT(obj_view_device_widgets->spinHoursFile), "value-changed", G_CALLBACK(view_device_update_gui_spin_time), NULL);
    g_signal_connect(G_OBJECT(obj_view_device_widgets->spinMinutesFile), "value-changed", G_CALLBACK(view_device_update_gui_spin_time), NULL);
}

void view_device_set_combo_box_device() {
    if (strcmp(view_device_set_add_edit, "add")==0) {
        int disk_id = gtk_combo_box_get_active(GTK_COMBO_BOX(obj_view_device_widgets->DeviceCombo));
        if (disk_id == -1) {
            gtk_widget_set_sensitive(obj_view_device_widgets->boxBlockGuiAddEditDevice, 0);
            gtk_widget_set_sensitive(obj_view_device_widgets->btnSaveTempSave, 0);
        }
        else {
            gtk_widget_set_sensitive(obj_view_device_widgets->boxBlockGuiAddEditDevice, 1);
            gtk_widget_set_sensitive(obj_view_device_widgets->btnSaveTempSave, 1);
            int status_id = gtk_combo_box_get_active(GTK_COMBO_BOX(obj_view_device_widgets->cmbDeviceSetStatus));
            if (status_id == -1) {
                gtk_combo_box_set_active(GTK_COMBO_BOX(obj_view_device_widgets->cmbDeviceSetStatus), 0);
            }
        }
    }
}

void view_device_on_destroy_subwindow(GtkWidget *self) {
    if (self) {
        gtk_widget_destroy(gtk_widget_get_toplevel(self));
        obj_view_device_widgets = NULL;
        
    }
}

void view_device_destroy(GtkWidget *self) {
    if (self) {
        obj_view_device_widgets = NULL;
    }
}

void view_device_update_gui_spin_time() {
    
}

void view_device_fill_gui() {
    size_t second_file = 0;
    size_t second_size = 0;
    if (strcmp(view_device_set_add_edit, "edit") == 0) {
        if (view_device_index != -1) {
            disk_status* _config = (disk_status*)vec_device->pfVectorGet(vec_device, view_device_index);
            second_size = _config->deferring_size;
            second_file = _config->deferring_file;
            gtk_combo_box_set_active(GTK_COMBO_BOX(obj_view_device_widgets->cmbDeviceSetStatus), _config->status);
            gtk_label_set_label(GTK_LABEL(obj_view_device_widgets->lblNameDevice), _config->device);
        }
    }
    view_device_set_time_spin(second_file, second_size);
}
void view_device_get_spin_time(size_t* second_file, size_t* second_size) {
    int spin_week_size = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(obj_view_device_widgets->spinWeeksSize));
    int spin_days_size = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(obj_view_device_widgets->spinDaysSize));
    int spin_hours_size = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(obj_view_device_widgets->spinHoursSize));
    int spin_minutes_size = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(obj_view_device_widgets->spinMinutesSize));
    *second_size = spin_week_size*7*24*3600+spin_days_size*24*3600+spin_hours_size*3600+spin_minutes_size*60;
    int spin_week_file = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(obj_view_device_widgets->spinWeeksFile));
    int spin_days_file = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(obj_view_device_widgets->spinDaysFile));
    int spin_hours_file = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(obj_view_device_widgets->spinHoursFile));
    int spin_minutes_file = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(obj_view_device_widgets->spinMinutesFile)); 
    *second_file = spin_week_file*7*24*3600+spin_days_file*24*3600+spin_hours_file*3600+spin_minutes_file*60;
    view_device_set_time_spin(*second_file, *second_size);
}

void view_device_get_gui_data() {
    char* disk = NULL;
    disk_status* _config = NULL;
    int status_id = gtk_combo_box_get_active(GTK_COMBO_BOX(obj_view_device_widgets->cmbDeviceSetStatus));
    if (status_id != -1) {
        size_t second_file = 0;
        size_t second_size = 0;
        view_device_get_spin_time(&second_file, &second_size);
        if (view_device_index != -1) {
            _config = (disk_status*)vec_device->pfVectorGet(vec_device, view_device_index);
            
        }
        else {
            _config = malloc(sizeof(disk_status));
            disk = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(obj_view_device_widgets->DeviceCombo));
            if (get_device_vec()->vectorList.total > 0) {
                for (int index = 0; index < get_device_vec()->vectorList.total; index++)
                {   
                    device_config* obj_device_config = (device_config*)get_device_vec()->pfVectorGet(get_device_vec(), index);
                    if (strstr(disk, obj_device_config->name_disk)) {
                        _config->device = yon_char_new(obj_device_config->name_disk);
                        _config->fs = yon_char_new(obj_device_config->file_system);
                        _config->mount_point = yon_char_new(obj_device_config->mounted);
                        _config->name = yon_char_new(obj_device_config->type_dick);
                    }
                    
                }
            }
        }
        _config->deferring_size = second_size;
        _config->deferring_file = second_file;
        if (strcmp(view_device_set_add_edit, "add") == 0) {
            if (status_id == 0) {
                _config->status = 1;
            }
            else {
                _config->status = status_id;
            }
        }
        else {
            if (status_id == 0) {
                yon_window_config_erase_custom_parameter(_config->device, "deferring_size");
                yon_window_config_erase_custom_parameter(_config->device, "deferring_file");
                yon_window_config_erase_custom_parameter(_config->device, _config->device);
                model_search_disk_remove(&view_device_all_config_temp->v_user, &view_device_all_config_temp->v_remove, _config->device);
                model_search_disk_remove(&view_device_all_config_temp->v_group, &view_device_all_config_temp->v_remove, _config->device);
                model_search_disk_remove_project(&view_device_all_config_temp->v_project,&view_device_all_config_temp->v_disk_status, &view_device_all_config_temp->v_remove, _config->device);
                _config->status = 1;
            }
            else {
                _config->status = status_id;
            }
        }
        if (view_device_index == -1) {
            vec_device->pfVectorAdd(vec_device, _config);
        }
    }
}
void view_device_set_time_spin(size_t seconds_file, size_t seconds_size) {
    me_time t_file = time_convert(seconds_file);
    me_time t_size = time_convert(seconds_size);
    if (seconds_file!=-1) {
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(obj_view_device_widgets->spinWeeksFile), t_file.weeks);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(obj_view_device_widgets->spinDaysFile), t_file.days);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(obj_view_device_widgets->spinHoursFile), t_file.hours);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(obj_view_device_widgets->spinMinutesFile), t_file.minutes);
    }
    else {
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(obj_view_device_widgets->spinWeeksFile), 0);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(obj_view_device_widgets->spinDaysFile), 0);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(obj_view_device_widgets->spinHoursFile), 0);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(obj_view_device_widgets->spinMinutesFile), 0);
    }
    if (seconds_size!=-1) {
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(obj_view_device_widgets->spinWeeksSize), t_size.weeks);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(obj_view_device_widgets->spinDaysSize), t_size.days);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(obj_view_device_widgets->spinHoursSize), t_size.hours);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(obj_view_device_widgets->spinMinutesSize), t_size.minutes);
    }
    else {
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(obj_view_device_widgets->spinWeeksSize), 0);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(obj_view_device_widgets->spinDaysSize), 0);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(obj_view_device_widgets->spinHoursSize), 0);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(obj_view_device_widgets->spinMinutesSize), 0);
    }
}
