#include "view_temp_u_g_p.h"

temp_set_window *temp_widgets = NULL;
char* flag_temp_u_g_p;
char* set_add_edit;
char* _glade_path = NULL;
vector* vec_temp = NULL;
config_all* _all_config_temp = NULL;
int table_index = 0;
char* disk = NULL;

vector* temp_vec_actors = NULL;

void temp_check_activ_disk() {
    int menu_id = gtk_combo_box_get_active(GTK_COMBO_BOX(temp_widgets->UserDeviceCombo));
    if (menu_id == -1) {
        gtk_widget_set_sensitive(temp_widgets->entryCatalogProject, 0);
        gtk_widget_set_sensitive(temp_widgets->btnReviewProject, 0);
    }
    else {
        gtk_widget_set_sensitive(temp_widgets->entryCatalogProject, 1);
        gtk_widget_set_sensitive(temp_widgets->btnReviewProject, 1);
    }
}
void destroy_show_warning(GtkWidget *self) {
    gtk_widget_destroy(gtk_widget_get_toplevel(self));
    temp_widgets->WarrningWindow = NULL;
    temp_widgets->CancelWarrningButton = NULL;
    temp_widgets->lblWarningText = NULL;
}

void show_warning_no_fill(char* text_mess) {
    GtkBuilder *builder = gtk_builder_new_from_resource(_glade_path);
    temp_widgets->WarrningWindow = yon_gtk_builder_get_widget(builder,"WarrningWindow");
    temp_widgets->lblWarningText = yon_gtk_builder_get_widget(builder,"lblWarningText");
    temp_widgets->CancelWarrningButton = yon_gtk_builder_get_widget(builder,"CancelWarrningButton");
    gtk_label_set_label(GTK_LABEL(temp_widgets->lblWarningText), text_mess);
    gtk_widget_show(temp_widgets->WarrningWindow);
    g_signal_connect(G_OBJECT(temp_widgets->WarrningWindow), "destroy", G_CALLBACK(destroy_show_warning), temp_widgets->WarrningWindow);
    g_signal_connect(G_OBJECT(temp_widgets->CancelWarrningButton),"clicked",G_CALLBACK(destroy_show_warning),temp_widgets->WarrningWindow);
}

void show_warning_disk() {
    char* text_mess = NULL;
    char* str_disk = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(temp_widgets->UserDeviceCombo));
    for (int i = 0; i < _all_config_temp->v_disk_status.vectorList.total; i++) {
        disk_status* _config = (disk_status*)_all_config_temp->v_disk_status.pfVectorGet(&_all_config_temp->v_disk_status, i);
        if (strstr(str_disk, _config->device)) {
            text_mess = STR_ERROR_PATH_IS_DISK_FALSE;
            text_mess = yon_char_unite(text_mess, " ", _config->device, NULL);
            show_warning_no_fill(text_mess);
        }
    }
}


void set_disk(char* _disk) {
    disk = yon_char_new(_disk);
}

void set_all_config(config_all* _all_config) {
    _all_config_temp = _all_config;
}

temp_set_window* get_widget_temp() {
    return temp_widgets;
}

void set_vector(vector* _vec_temp) {
    vec_temp = _vec_temp;
}

void temp_set_index(int _index) {
    table_index = _index;
}

temp_set_window *temp_setup_window_base(char* __glade_path) {
    _glade_path = __glade_path;
    if (temp_widgets == NULL) {
        temp_widgets = malloc(sizeof(temp_set_window));
        GtkBuilder *builder = gtk_builder_new_from_resource(_glade_path);
        temp_widgets->Window = yon_gtk_builder_get_widget(builder,"QuotasEditWindow");
        temp_widgets->btnSaveTempSave = yon_gtk_builder_get_widget(builder,"quotegroupSaveButton");
        temp_widgets->btnTempCancel = yon_gtk_builder_get_widget(builder,"quotegroupCancelButton");
        temp_setup_window_custom(temp_widgets, builder);
        temp_init_windows();
        temp_localization(temp_widgets);
        temp_event(temp_widgets);
    }
    return temp_widgets;
}

void temp_check_path() {
    char* str_disk = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(temp_widgets->UserDeviceCombo));
    for (int i = 0; i < _all_config_temp->v_disk_status.vectorList.total; i++) {
        disk_status* _config = (disk_status*)_all_config_temp->v_disk_status.pfVectorGet(&_all_config_temp->v_disk_status, i);
        if (strstr(str_disk, _config->device)) {
            const char* str_path = gtk_entry_get_text(GTK_ENTRY(temp_widgets->entryCatalogProject));
            if (strstr(str_path,_config->mount_point)) {
                gtk_widget_set_sensitive(temp_widgets->btnSaveTempSave, 1);
            }
            else {
                gtk_widget_set_sensitive(temp_widgets->btnSaveTempSave, 0);
                show_warning_disk();
            }
        }   
    }
}

temp_set_window *temp_setup_window_custom(temp_set_window* temp_widgets, GtkBuilder *builder) {
    temp_vec_actors = malloc(sizeof(vector));
    vector_init(temp_vec_actors);
    if (strcmp(set_add_edit, "add")==0) {
        table_u_g_get_actors(temp_vec_actors);
    }
    else {
        config_u_g_p* _config = (config_u_g_p*)vec_temp->pfVectorGet(vec_temp, table_index);
        _config->actors.pfVectorCopy(temp_vec_actors, &_config->actors);
        table_u_g_get_actors(temp_vec_actors);
    }
    temp_widgets->lblSetUGP = yon_gtk_builder_get_widget(builder, "lblSetUGP");
    temp_widgets->btnOpenUserGroup = yon_gtk_builder_get_widget(builder, "btnOpenUserGroup");
    temp_widgets->btnReviewProject = yon_gtk_builder_get_widget(builder, "btnReviewProject");
    temp_widgets->btnGenerateProject = yon_gtk_builder_get_widget(builder, "btnGenerateProject");
    temp_widgets->entryCatalogProject = yon_gtk_builder_get_widget(builder, "entryCatalogProject");
    temp_widgets->chbAuto = yon_gtk_builder_get_widget(builder, "chbAuto");
    temp_widgets->entryProjectName = yon_gtk_builder_get_widget(builder, "entryProjectName");
    temp_widgets->boxProjectAddEdit = yon_gtk_builder_get_widget(builder, "boxProjectAddEdit");
    temp_widgets->boxProject = yon_gtk_builder_get_widget(builder, "boxProject");
    temp_widgets->boxProjectAddEditboxProjectId = yon_gtk_builder_get_widget(builder, "boxProjectAddEditboxProjectId");
    temp_widgets->lblHeadQuotasEditWindow = yon_gtk_builder_get_widget(builder, "lblHeadQuotasEditWindow");

    temp_widgets->quotegroupSizeSoftLimitCheck = yon_gtk_builder_get_widget(builder, "quotegroupSizeSoftLimitCheck");
    temp_widgets->quotegroupSizeSoftLimitSpin = yon_gtk_builder_get_widget(builder, "quotegroupSizeSoftLimitSpin");
    temp_widgets->quotegroupSizeSoftLimitCombo = yon_gtk_builder_get_widget(builder, "quotegroupSizeSoftLimitCombo");
    temp_widgets->quotegroupSizeHardLimitCheck = yon_gtk_builder_get_widget(builder, "quotegroupSizeHardLimitCheck");
    temp_widgets->quotegroupSizeHardLimitSpin = yon_gtk_builder_get_widget(builder, "quotegroupSizeHardLimitSpin");
    temp_widgets->quotegroupSizeHardLimitCombo = yon_gtk_builder_get_widget(builder, "quotegroupSizeHardLimitCombo");
    temp_widgets->quotegroupFilesSoftLimitCheck = yon_gtk_builder_get_widget(builder, "quotegroupFilesSoftLimitCheck");
    temp_widgets->quotegroupFilesSoftLimitSpin = yon_gtk_builder_get_widget(builder, "quotegroupFilesSoftLimitSpin");
    temp_widgets->quotegroupFilesHardLimitCheck = yon_gtk_builder_get_widget(builder, "quotegroupFilesHardLimitCheck");
    temp_widgets->quotegroupFilesHardLimitSpin = yon_gtk_builder_get_widget(builder, "quotegroupFilesHardLimitSpin");
    temp_widgets->entryProjectId = yon_gtk_builder_get_widget(builder, "entryProjectId");
    temp_widgets->lblSetNameDevice = yon_gtk_builder_get_widget(builder, "lblSetNameDevice");
    temp_widgets->lblCatalogProjectEdit = yon_gtk_builder_get_widget(builder, "lblCatalogProjectEdit");
    temp_widgets->lblProjectIdValueEdit = yon_gtk_builder_get_widget(builder, "lblProjectIdValueEdit");
    temp_widgets->boxTempDevice = yon_gtk_builder_get_widget(builder, "boxTempDevice");
    temp_widgets->entryUser = yon_gtk_builder_get_widget(builder,"entryUser");
    temp_widgets->UserDeviceCombo = yon_gtk_builder_get_widget(builder,"UserDeviceCombo");
    temp_widgets->lblSetQuotasStatus = yon_gtk_builder_get_widget(builder,"lblSetQuotasStatus");
    temp_widgets->boxBlockGuiAddEdit = yon_gtk_builder_get_widget(builder,"boxBlockGuiAddEdit");
    table_u_g_set_flag(flag_temp_u_g_p,set_add_edit);
    table_u_g_set_glade_path(_glade_path);
    temp_settings();
}

void temp_select_disk() {
    char* str_disk = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(temp_widgets->UserDeviceCombo));
    for (int i = 0; i < _all_config_temp->v_disk_status.vectorList.total; i++) {
        disk_status* _config = (disk_status*)_all_config_temp->v_disk_status.pfVectorGet(&_all_config_temp->v_disk_status, i);
        if (strstr(str_disk, _config->device)) {
            if (_config->status == 2) {
                gtk_label_set_label(GTK_LABEL(temp_widgets->lblSetQuotasStatus), STR_ON);
            }
            else if (_config->status == 1){
                gtk_label_set_label(GTK_LABEL(temp_widgets->lblSetQuotasStatus), STR_OFF);
            }
            else {
                gtk_label_set_label(GTK_LABEL(temp_widgets->lblSetQuotasStatus), STR_NOT_SET);
            }
        }
        
    }
}

void temp_settings() {
    char *split_simvol = "    ";
    for (int i = 0; i < _all_config_temp->v_disk_status.vectorList.total; i++) {
        disk_status* _config = (disk_status*)_all_config_temp->v_disk_status.pfVectorGet(&_all_config_temp->v_disk_status, i);
        char* str_disk = yon_char_unite(_config->device,split_simvol,
                                    _config->fs,
                                    split_simvol,
                                    _config->mount_point,
                                    split_simvol,
                                    _config->name, NULL);
        gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(temp_widgets->UserDeviceCombo), yon_char_new(str_disk));
        free(str_disk);
    }
    if (strcmp(flag_temp_u_g_p,"prjquota")==0) {
        gtk_widget_hide(temp_widgets->lblSetUGP);
        gtk_widget_hide(temp_widgets->btnOpenUserGroup);
        gtk_widget_hide(temp_widgets->entryUser);
        if (strcmp(set_add_edit, "add")==0) {
            gtk_widget_hide(temp_widgets->lblCatalogProjectEdit);
            gtk_widget_hide(temp_widgets->lblProjectIdValueEdit);
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(temp_widgets->chbAuto),1);
            gtk_label_set_label(GTK_LABEL(temp_widgets->lblHeadQuotasEditWindow), STR_TITLE_ADD_QUOTAS);
        }
        else {
            gtk_widget_hide(temp_widgets->btnReviewProject);
            gtk_widget_hide(temp_widgets->entryCatalogProject);
            gtk_widget_hide(temp_widgets->chbAuto);
            gtk_widget_hide(temp_widgets->entryProjectId);
            gtk_widget_hide(temp_widgets->btnGenerateProject);
            gtk_widget_hide(temp_widgets->UserDeviceCombo);
            gtk_label_set_label(GTK_LABEL(temp_widgets->lblHeadQuotasEditWindow), STR_TITLE_EDIT_QUOTAS);
        }
    }
    else {
        gtk_widget_hide(temp_widgets->boxProjectAddEditboxProjectId);
        if (strcmp(flag_temp_u_g_p, "usrquota")==0) {
            gtk_label_set_label(GTK_LABEL(temp_widgets->lblSetUGP), yon_char_unite(STR_USER,":",NULL));
        }
        else if (strcmp(flag_temp_u_g_p, "grpquota")==0) {
            gtk_label_set_label(GTK_LABEL(temp_widgets->lblSetUGP), yon_char_unite(STR_GROUP,":",NULL));

        }
        if (strcmp(set_add_edit, "add")==0) {
            gtk_widget_hide(temp_widgets->boxProject);
            gtk_widget_hide(temp_widgets->boxProjectAddEdit);
            gtk_label_set_label(GTK_LABEL(temp_widgets->lblHeadQuotasEditWindow), STR_TITLE_ADD_QUOTAS);
            
        }
        else {
            gtk_widget_hide(temp_widgets->boxProject);
            gtk_widget_hide(temp_widgets->boxProjectAddEdit);
            gtk_widget_hide(temp_widgets->UserDeviceCombo);
            gtk_label_set_label(GTK_LABEL(temp_widgets->lblHeadQuotasEditWindow), STR_TITLE_EDIT_QUOTAS);
        }  
    }
    temp_on_set_combo_box_device();
}

void set_temp_menu(char* _flag_temp_u_g_p, char* _set_add_edit) {
    flag_temp_u_g_p = _flag_temp_u_g_p;
    set_add_edit = _set_add_edit;
}

void temp_show(GtkWidget *self, char* _glade_path) {
    if (temp_widgets != NULL) {
        gtk_widget_show(temp_widgets->Window);
        set_activ_project_id();
        temp_init_windows();
        event_toggled_all();
    }
    else {
        temp_setup_window_base(_glade_path);
        gtk_widget_show(temp_widgets->Window);
        set_activ_project_id();
        event_toggled_all();
    }
}

void temp_localization(temp_set_window* temp_widgets) {

}

void temp_init_windows() {
}

void wrapper_table_u_g_show() {
    table_u_g_get_actors(temp_vec_actors);
    table_u_g_show(NULL, _glade_path);
}

void temp_event(temp_set_window* temp_widgets) {
    g_signal_connect(G_OBJECT(temp_widgets->UserDeviceCombo), "changed", G_CALLBACK(temp_on_set_combo_box_device), NULL);
    g_signal_connect(G_OBJECT(temp_widgets->btnTempCancel),"clicked",G_CALLBACK(temp_destroy),temp_widgets->Window);
    g_signal_connect(G_OBJECT(temp_widgets->Window), "destroy", G_CALLBACK(temp_destroy), temp_widgets->Window);
    g_signal_connect(G_OBJECT(temp_widgets->quotegroupSizeSoftLimitCheck), "toggled", G_CALLBACK(event_toggled_all), NULL);
    g_signal_connect(G_OBJECT(temp_widgets->quotegroupSizeHardLimitCheck), "toggled", G_CALLBACK(event_toggled_all), NULL);
    g_signal_connect(G_OBJECT(temp_widgets->quotegroupFilesSoftLimitCheck), "toggled", G_CALLBACK(event_toggled_all), NULL);
    g_signal_connect(G_OBJECT(temp_widgets->quotegroupFilesHardLimitCheck), "toggled", G_CALLBACK(event_toggled_all), NULL);
    g_signal_connect(G_OBJECT(temp_widgets->UserDeviceCombo), "changed", G_CALLBACK(temp_select_disk), NULL);
    int flag = 3;
    g_signal_connect(G_OBJECT(temp_widgets->quotegroupSizeSoftLimitCombo), "changed", G_CALLBACK(update_max_size_spin), &flag);
    g_signal_connect(G_OBJECT(temp_widgets->quotegroupSizeHardLimitCombo), "changed", G_CALLBACK(update_max_size_spin), &flag);
    g_signal_connect(G_OBJECT(temp_widgets->quotegroupSizeSoftLimitSpin), "changed", G_CALLBACK(update_max_size_spin), NULL);
    g_signal_connect(G_OBJECT(temp_widgets->quotegroupSizeHardLimitSpin), "changed", G_CALLBACK(update_max_size_spin), NULL);
    if (strstr(flag_temp_u_g_p,"prjquota")) {
        g_signal_connect(G_OBJECT(temp_widgets->btnReviewProject),"clicked",G_CALLBACK(wrapper_show_file_manager),NULL);
        g_signal_connect(G_OBJECT(temp_widgets->btnGenerateProject),"clicked",G_CALLBACK(wrapper_generate_id),NULL);
        g_signal_connect(G_OBJECT(temp_widgets->chbAuto),"toggled",G_CALLBACK(set_activ_project_id),NULL);
        g_signal_connect(G_OBJECT(temp_widgets->entryCatalogProject), "changed", G_CALLBACK(temp_check_path), NULL);
        g_signal_connect(G_OBJECT(temp_widgets->entryProjectId), "changed", G_CALLBACK(check_generate_id), NULL);
    }
    else {
        g_signal_connect(G_OBJECT(temp_widgets->btnOpenUserGroup),"clicked",G_CALLBACK(wrapper_table_u_g_show),NULL);
    }
}

void temp_on_set_combo_box_device() {
    if (strcmp(set_add_edit, "add")==0) {
        int menu_id = gtk_combo_box_get_active(GTK_COMBO_BOX(temp_widgets->UserDeviceCombo));
        if (menu_id == -1) {
            gtk_widget_set_sensitive(temp_widgets->boxBlockGuiAddEdit, 0);
            gtk_widget_set_sensitive(temp_widgets->btnSaveTempSave, 0);
        }
        else {
            gtk_widget_set_sensitive(temp_widgets->boxBlockGuiAddEdit, 1);
            gtk_widget_set_sensitive(temp_widgets->btnSaveTempSave, 1);
        }
    }
}

void temp_on_destroy_subwindow(GtkWidget *self) {
    if (self) {
        if (wrapper_check_save()) {
            if (strcmp(set_add_edit, "add")==0) {
                get_add_gui_data_base(_all_config_temp);
            }
            else {
                get_edit_gui_data_base(_all_config_temp, table_index);
            }
            gtk_widget_destroy(gtk_widget_get_toplevel(self));
            temp_widgets = NULL;
        }
        else {

        }
        
    }
}

void update_max_size_spin(GtkWidget *self, int* flag) {
    if (flag) {
        gtk_spin_button_set_digits(GTK_SPIN_BUTTON(temp_widgets->quotegroupSizeSoftLimitSpin), 0);
        gtk_spin_button_set_digits(GTK_SPIN_BUTTON(temp_widgets->quotegroupSizeHardLimitSpin), 0);
    }
    char* str_disk = NULL;
    if (strcmp(set_add_edit, "add")==0) {
        str_disk = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(temp_widgets->UserDeviceCombo));
    }
    else {
        str_disk = (char*)gtk_label_get_label(GTK_LABEL(temp_widgets->lblSetNameDevice));
    }
    if (!str_disk) {
        return;
    }
    for (int i = 0; i < get_device_vec()->vectorList.total; i++) {
        device_config* _config = (device_config*)get_device_vec()->pfVectorGet(get_device_vec(), i);
        if (strstr(str_disk, _config->name_disk)) {
            if (self==temp_widgets->quotegroupSizeSoftLimitCombo)
                gtk_spin_button_set_value(GTK_SPIN_BUTTON(temp_widgets->quotegroupSizeSoftLimitSpin),0.0);
            else if (self==temp_widgets->quotegroupSizeHardLimitCombo)
                gtk_spin_button_set_value(GTK_SPIN_BUTTON(temp_widgets->quotegroupSizeHardLimitSpin),0.0);
            philos_set_spin_adjustment(temp_widgets->quotegroupSizeSoftLimitCheck,
                              temp_widgets->quotegroupSizeSoftLimitSpin,
                              temp_widgets->quotegroupSizeSoftLimitCombo, _config->size_disk);
            philos_set_spin_adjustment(temp_widgets->quotegroupSizeHardLimitCheck,
                                temp_widgets->quotegroupSizeHardLimitSpin,
                                temp_widgets->quotegroupSizeHardLimitCombo, _config->size_disk);
        }   
    }
    
}

void event_toggled_all() {
    philos_set_active_widgets(temp_widgets->quotegroupSizeSoftLimitCheck,
                              temp_widgets->quotegroupSizeSoftLimitSpin,
                              temp_widgets->quotegroupSizeSoftLimitCombo);
    philos_set_active_widgets(temp_widgets->quotegroupSizeHardLimitCheck,
                                temp_widgets->quotegroupSizeHardLimitSpin,
                                temp_widgets->quotegroupSizeHardLimitCombo
    );
    philos_set_active_widgets(temp_widgets->quotegroupFilesSoftLimitCheck,
                                temp_widgets->quotegroupFilesSoftLimitSpin,
                                NULL
                                );
    philos_set_active_widgets(temp_widgets->quotegroupFilesHardLimitCheck,
                                temp_widgets->quotegroupFilesHardLimitSpin,
                                NULL);
    update_max_size_spin(NULL, NULL);
}

void block_btn_save() {
    if (wrapper_check_save()) {
        gtk_widget_set_sensitive(temp_widgets->btnSaveTempSave, 1);
    }
    else {
        gtk_widget_set_sensitive(temp_widgets->btnSaveTempSave, 0);
    }
}

void wrapper_show_file_manager() {
    GtkBuilder *builder = gtk_builder_new_from_resource(_glade_path);
    show_file_manger(temp_widgets, builder);
    gtk_widget_show(temp_widgets->wndChooseFileWallpaper);
    gtk_window_set_title(GTK_WINDOW(temp_widgets->wndChooseFileWallpaper), STR_TITLE_FILE_MANGER);
    char* str_disk = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(temp_widgets->UserDeviceCombo));
    for (int i = 0; i < _all_config_temp->v_disk_status.vectorList.total; i++) {
        disk_status* _config = (disk_status*)_all_config_temp->v_disk_status.pfVectorGet(&_all_config_temp->v_disk_status, i);
        if (strstr(str_disk, _config->device)) {
            gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(temp_widgets->wndChooseFileWallpaper),_config->mount_point);
        }   
    }
}

void temp_destroy(GtkWidget *self) {
    if (self) {
        gtk_widget_destroy(gtk_widget_get_toplevel(self));
        if (temp_vec_actors) {
            model_free_vector_string(temp_vec_actors);
            vectorFree(temp_vec_actors);
            temp_vec_actors = NULL;
        }
        temp_widgets = NULL;
    }
    
}



void show_file_manger(temp_set_window* temp_widgets, GtkBuilder *builder) {
    temp_widgets->wndChooseFileWallpaper = yon_gtk_builder_get_widget(builder,"wndChooseFileWallpaper");
    temp_widgets->btnFilemangerOk = yon_gtk_builder_get_widget(builder,"btnFilemangerOk");
    temp_widgets->btnFilemangerExit = yon_gtk_builder_get_widget(builder,"btnFilemangerExit");
    
    g_signal_connect(G_OBJECT(temp_widgets->btnFilemangerOk),"clicked",G_CALLBACK(btn_ok_file_manager), NULL);
    g_signal_connect(G_OBJECT(temp_widgets->btnFilemangerExit),"clicked",G_CALLBACK(destroy_file_manager), NULL);
}

void destroy_file_manager(GtkWidget *self) {
    gtk_widget_destroy(gtk_widget_get_toplevel(self));
    temp_widgets->wndChooseFileWallpaper = NULL;
    temp_widgets->btnFilemangerOk = NULL;
    temp_widgets->btnFilemangerExit = NULL;
}

void btn_ok_file_manager(GtkWidget *self) {
    char* path_project = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(temp_widgets->wndChooseFileWallpaper));
    gtk_entry_set_text(GTK_ENTRY(temp_widgets->entryCatalogProject), path_project);
    destroy_file_manager(self);
    
}


void set_edit_gui_data_base(config_all* all_config, int index) {
    config_u_g_p* _config = (config_u_g_p*)vec_temp->pfVectorGet(vec_temp, index);
    
    if (_config && index >= 0) {
        set_widget(temp_widgets->quotegroupSizeSoftLimitCheck,
                    temp_widgets->quotegroupSizeSoftLimitSpin,
                    temp_widgets->quotegroupSizeSoftLimitCombo,
                    _config->soft_restriction_size,
                    _config->soft_restriction_size_pow
        );
        set_widget(temp_widgets->quotegroupSizeHardLimitCheck,
                    temp_widgets->quotegroupSizeHardLimitSpin,
                    temp_widgets->quotegroupSizeHardLimitCombo,
                    _config->severe_limitation_size,
                    _config->severe_limitation_size_pow
        );
        set_widget(temp_widgets->quotegroupFilesSoftLimitCheck,
                    temp_widgets->quotegroupFilesSoftLimitSpin,
                    NULL,
                    _config->soft_restriction_file,
                    0
        );
        set_widget(temp_widgets->quotegroupFilesHardLimitCheck,
                    temp_widgets->quotegroupFilesHardLimitSpin,
                    NULL,
                    _config->severe_limitation_file,
                    0
        );
    for (int i = 0; i < _all_config_temp->v_disk_status.vectorList.total; i++) {
        disk_status* _config_disk = (disk_status*)_all_config_temp->v_disk_status.pfVectorGet(&_all_config_temp->v_disk_status, i);
        if (strstr(_config->type_arr, "prjquota")) {
            if (strstr(_config_disk->device, model_find_path_to_disk(&all_config->v_disk_status, _config->UNIX_file)))  {
                if (_config_disk->status == 1) {
                    gtk_label_set_label(GTK_LABEL(temp_widgets->lblSetQuotasStatus), STR_OFF);
                }
                else if (_config_disk->status == 2) {
                    gtk_label_set_label(GTK_LABEL(temp_widgets->lblSetQuotasStatus), STR_ON);
                }
            }
        }
        else {
            if (strstr(_config_disk->device, _config->UNIX_file))  {
                if (_config_disk->status == 1) {
                    gtk_label_set_label(GTK_LABEL(temp_widgets->lblSetQuotasStatus), STR_OFF);
                }
                else if (_config_disk->status == 2) {
                    gtk_label_set_label(GTK_LABEL(temp_widgets->lblSetQuotasStatus), STR_ON);
                }
            }
        }
        
    }
    if (strstr(flag_temp_u_g_p,"prjquota")) {
        if (strstr(set_add_edit, "add")) { 
        }
        else {
            char* id = fill_tree_view_id(_config->id);
            char* actors = format_actors(&_config->actors);
            gtk_label_set_label(GTK_LABEL(temp_widgets->lblProjectIdValueEdit),  id);
            gtk_label_set_label(GTK_LABEL(temp_widgets->lblCatalogProjectEdit), _config->UNIX_file);
            if (strcmp(actors,"-")!=0) {
                gtk_entry_set_text(GTK_ENTRY(temp_widgets->entryProjectName), actors);
            }
            gtk_label_set_label(GTK_LABEL(temp_widgets->lblSetNameDevice), model_find_path_to_disk(&all_config->v_disk_status, _config->UNIX_file));
        }
    }
    else {
        if (strcmp(set_add_edit, "add")==0) {
        } 
        else {
            gtk_label_set_label(GTK_LABEL(temp_widgets->lblSetNameDevice), _config->UNIX_file);
            gtk_entry_set_text(GTK_ENTRY(temp_widgets->entryUser), format_actors(&_config->actors));
            
        }
    }
        
    }   
}

void set_widget(GtkWidget* check, GtkWidget* spin, GtkWidget* combo, int value_spin, int pow_mem_size) {
    if (value_spin>0) {
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check),1);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value_spin);
        if (combo) {
            gtk_combo_box_set_active(GTK_COMBO_BOX(combo), pow_mem_size);
        }
    }
    else {
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check),0);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), 0);
        if (combo) {
            gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
        }
    }
    
}
void get_edit_gui_data_base(config_all* all_config, int index) {
    config_u_g_p* _config = (config_u_g_p*)vec_temp->pfVectorGet(vec_temp, index);
    if (_config && index >= 0) {
        char* str_user = (char*)gtk_entry_get_text(GTK_ENTRY(temp_widgets->entryUser));
        if (strlen(str_user)>2) {
            vectorFree(&_config->actors);
            vector_init(&_config->actors);
            int size_str = 0;
            char** name_disk = yon_char_parse(str_user, &size_str, ",");
            vector t;
            vector_init(&t);
            if (name_disk) {
                for (int i=0; i< size_str; i++) {
                    t.pfVectorAdd(&t, yon_char_new(name_disk[i]));
                }
                _config->actors.pfVectorCopy(&_config->actors, &t);
            }
            else {
                t.pfVectorAdd(&t, yon_char_new(str_user));
                _config->actors.pfVectorCopy(&_config->actors, &t);
            }
        }
        get_temp_gui_data_base(_config);
        vec_temp->pfVectorSet(vec_temp, index, _config);
    }
    
}
void get_add_gui_data_base(config_all* all_config) {
    config_u_g_p* _config = malloc(sizeof(config_u_g_p));
    model_null_str_writing(_config);
    get_temp_gui_data_base(_config);
    char* str_user = (char*)gtk_entry_get_text(GTK_ENTRY(temp_widgets->entryUser));
    if (strlen(str_user)>2) {
        int size_str = 0;
        char** name_disk = yon_char_parse(str_user, &size_str, ",");
        vector t;
        vector_init(&t);
        if (name_disk) {
            for (int i=0; i< size_str; i++) {
                t.pfVectorAdd(&t, yon_char_new(name_disk[i]));
            }
            _config->actors.pfVectorCopy(&_config->actors, &t);
        }
        else {
            t.pfVectorAdd(&t, yon_char_new(str_user));
            _config->actors.pfVectorCopy(&_config->actors, &t);
        }
    }
    model_format_del_str(_config);
    vec_temp->pfVectorAdd(vec_temp, _config);
    
    
}

void save(GtkWidget *self) {
    temp_vec_actors = get_actors_table(temp_vec_actors);
    gtk_entry_set_text(GTK_ENTRY(temp_widgets->entryUser), format_actors(temp_vec_actors));
    table_u_g_on_hide_subwindow(self);
}

int check_save() {                          
    gboolean active1 = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(temp_widgets->quotegroupSizeSoftLimitCheck));
    gboolean active2 = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(temp_widgets->quotegroupSizeHardLimitCheck));
    gboolean active3 = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(temp_widgets->quotegroupFilesSoftLimitCheck));
    gboolean active4 = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(temp_widgets->quotegroupFilesHardLimitCheck));

    char* value1 = (char*)gtk_entry_get_text(GTK_ENTRY(temp_widgets->quotegroupSizeSoftLimitSpin));
    char* value2 = (char*)gtk_entry_get_text(GTK_ENTRY(temp_widgets->quotegroupSizeHardLimitSpin));
    char* value3 = (char*)gtk_entry_get_text(GTK_ENTRY(temp_widgets->quotegroupFilesSoftLimitSpin));
    char* value4 = (char*)gtk_entry_get_text(GTK_ENTRY(temp_widgets->quotegroupFilesHardLimitSpin));
    int menu_id = 0;
    if (strcmp(gtk_entry_get_text(GTK_ENTRY(temp_widgets->entryUser)),"")==0&&!strstr(flag_temp_u_g_p,"prjquota")){
            show_warning_no_fill(STR_ERROR_LIST_USER_NULL);
            return 0;
    }
    else if (!active1 && !active2 && !active3 && !active4) {
        show_warning_no_fill(STR_ERROR_ENTRY_VALUE_VOLUME_OR_FILES);
        return 0;
    }
    else if (menu_id == -1 || ((!strcmp(value1, "0") || !strcmp(value1, "")) && active1) 
            || ((!strcmp(value2, "0") || !strcmp(value2, "")) && active2)
            || ((!strcmp(value3, "0") || !strcmp(value3, "")) && active3) 
            || ((!strcmp(value4, "0") || !strcmp(value4, "")) && active4)) {
                show_warning_no_fill(STR_ERROR_ENTRY_VALUE_VOLUME_OR_FILES);
        return 0;
    }
    else if (strcmp(set_add_edit, "add")==0) {
        menu_id = gtk_combo_box_get_active(GTK_COMBO_BOX(temp_widgets->UserDeviceCombo));
        if (menu_id == -1) {
            show_warning_no_fill(STR_ERROR_NO_SELECTED_DISK);
        }
    }
    return 1;
}

int check_save_project() {
    gboolean active1 = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(temp_widgets->chbAuto));
    char* project_name = (char*)gtk_entry_get_text(GTK_ENTRY(temp_widgets->entryProjectName));
    if (strcmp(set_add_edit, "add")==0) {
        char* file_dir = (char*)gtk_entry_get_text(GTK_ENTRY(temp_widgets->entryCatalogProject));
        char* project_id = (char*)gtk_entry_get_text(GTK_ENTRY(temp_widgets->entryProjectId));
        struct stat buf;
        int error_file = stat(file_dir, &buf);
        if (error_file < 0 || !S_ISDIR(buf.st_mode) || !strlen(file_dir) || (!strlen(project_id) && !active1) || !strlen(project_name)) {
            if (!strlen(project_id) && !active1) {
                show_warning_no_fill(STR_ERROR_ERROR_PROJECT_ID);
            }
            else if (!strlen(file_dir)) {
                show_warning_no_fill(STR_ERROR_PATH_IS_DISK_FALSE);
            }
            else if (!strlen(project_name)) {
                show_warning_no_fill(STR_ERROR_PROJECT_NAME_VALUE_NULL);
            }
            else if(error_file < 0) {
                // Если отработало условие то
                // Нет такого файла или каталога
                show_warning_no_fill(STR_ERROR_MESS_NOT_DIR);
            }
            else if (!S_ISDIR(buf.st_mode)) {
                // Если отработало условие то
                // Это файл, а не каталога
                show_warning_no_fill(STR_ERROR_MESS_NOT_DIR);
            }
            return 0;
        }
        return 1;
    }
    else {
        if (strlen(project_name)<=1) {
            show_warning_no_fill(STR_ERROR_PROJECT_NAME_ENTER_VALUE);
            return 0;
        }
        return 1;
    }
}

int wrapper_check_save() {
    if (strstr(flag_temp_u_g_p,"prjquota")) {
        temp_check_activ_disk();
        int flag_save_project = check_save_project();
        int flag_save = check_save();
        if (flag_save_project && flag_save) {
            return 1;
        }
        return 0;
    }
    else {
        const char* str_user = gtk_entry_get_text(GTK_ENTRY(temp_widgets->entryUser));
        if ((strlen(str_user)>2)) {
            if (check_save()) {
                return 1;
            }
            return 0;
        }
        else {
            if (check_save()) {
                return 1;
            }
            return 0;
        }
    }
}

void table_u_g_event() {
    g_signal_connect(G_OBJECT(get_widget_table_u_g()->checkColUserGroup),"toggled",G_CALLBACK(chk_toggled_user_group),NULL);
    g_signal_connect(G_OBJECT(get_widget_table_u_g()->btnCancel), "clicked",G_CALLBACK(table_u_g_on_hide_subwindow),NULL);
    g_signal_connect(G_OBJECT(get_widget_table_u_g()->btnSave), "clicked",G_CALLBACK(save),NULL);
    g_signal_connect(G_OBJECT(get_widget_table_u_g()->Window), "destroy", G_CALLBACK(table_u_g_destroy), NULL);
}

config_u_g_p* get_temp_gui_data_base(config_u_g_p* _config) {
    update_max_size_spin(NULL, NULL);
    check_generate_id();
    set_widget_config(temp_widgets->quotegroupSizeSoftLimitCheck,
                temp_widgets->quotegroupSizeSoftLimitSpin,
                temp_widgets->quotegroupSizeSoftLimitCombo,
                &_config->soft_restriction_size,
                &_config->soft_restriction_size_pow
    );
    set_widget_config(temp_widgets->quotegroupSizeHardLimitCheck,
                temp_widgets->quotegroupSizeHardLimitSpin,
                temp_widgets->quotegroupSizeHardLimitCombo,
                &_config->severe_limitation_size,
                &_config->severe_limitation_size_pow
    );
    set_widget_config(temp_widgets->quotegroupFilesSoftLimitCheck,
                temp_widgets->quotegroupFilesSoftLimitSpin,
                NULL,
                &_config->soft_restriction_file,
                NULL
    );
    set_widget_config(temp_widgets->quotegroupFilesHardLimitCheck,
                temp_widgets->quotegroupFilesHardLimitSpin,
                NULL,
                &_config->severe_limitation_file,
                NULL
    );
    char* str_disk = NULL;
    if (strcmp(set_add_edit, "add")==0) {
        str_disk = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(temp_widgets->UserDeviceCombo));
    }
    else {
        str_disk = (char*)gtk_label_get_label(GTK_LABEL(temp_widgets->lblSetNameDevice));
    }
    for (int i = 0; i < _all_config_temp->v_disk_status.vectorList.total; i++) {
        disk_status* _config_disk = (disk_status*)_all_config_temp->v_disk_status.pfVectorGet(&_all_config_temp->v_disk_status, i);
        if (strstr(str_disk, _config_disk->device))  {
            _config->deferring_size = _config_disk->deferring_size;
            _config->deferring_file = _config_disk->deferring_file;
        }
    }
    if (strstr(flag_temp_u_g_p,"prjquota")) {
        char* actors = (char*)gtk_entry_get_text(GTK_ENTRY(temp_widgets->entryProjectName));
        if (strlen(actors)>1) {
            if (_config->actors.vectorList.total > 0) {
                _config->actors.pfVectorSet(&_config->actors,0, yon_char_new(actors));
            }
            else {
                _config->actors.pfVectorAdd(&_config->actors, yon_char_new(actors));
            }
            
        }
        gboolean active = active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(temp_widgets->chbAuto));
        if (active) {
            _config->id = -1;
        }
        else {
            char* id = (char*)gtk_entry_get_text(GTK_ENTRY(temp_widgets->entryProjectId));
            if (strlen(id)) {
                _config->id = atoi(id);
            }
            else {
                _config->id = -1;
            }
            
        }
        if (strcmp(set_add_edit, "add")==0) {
            char* file_dir = (char*)gtk_entry_get_text(GTK_ENTRY(temp_widgets->entryCatalogProject));
            _config->UNIX_file = yon_char_new(file_dir);
            _config->type_arr = yon_char_new(flag_temp_u_g_p);
        }
    }
    else {
        char* activ_disk = NULL;
        char* str_user = (char*)gtk_entry_get_text(GTK_ENTRY(temp_widgets->entryUser));
        table_u_g_get_actors(&_config->actors);
        if (strcmp(set_add_edit, "add")==0) {
            activ_disk = yon_char_new(gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(temp_widgets->UserDeviceCombo)));
        }
        else {
            activ_disk = yon_char_new((char*)gtk_label_get_label(GTK_LABEL(temp_widgets->lblSetNameDevice)));

        }
        activ_disk = yon_char_divide_search(activ_disk, " ", -1);
        _config->UNIX_file = activ_disk;
        _config->type_arr = yon_char_new(flag_temp_u_g_p);
    }
    return _config;
}



void set_widget_config(GtkWidget* check, GtkWidget* spin, GtkWidget* combo, size_t* value, int* mem_size) {
    gboolean active = active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check));
    if (active) {
        (*value) = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spin));
        if (combo) {
            if ((*value) != 0) {
                (*mem_size) = gtk_combo_box_get_active(GTK_COMBO_BOX(combo));
            }
            else {
                (*mem_size) = -3;
            }
        }
    } 
    else {
        (*value) = 0;
        if (combo) {
            (*mem_size) = -3;
        }
    }
}
void check_generate_id() {
    if (strstr(flag_temp_u_g_p,"prjquota")) {
        g_signal_handlers_block_by_func(G_OBJECT(temp_widgets->entryProjectId), G_CALLBACK(check_generate_id), NULL);
        int active = gtk_widget_get_sensitive(temp_widgets->entryProjectId);
        if (active) {
            char* text = (char*)gtk_entry_get_text(GTK_ENTRY(temp_widgets->entryProjectId));
            if (strlen(text)) {
                int id = atoi(text);
                if (check_generate(vec_temp, id)) {
                    id = generate_id(vec_temp, id);
                    gtk_entry_set_text(GTK_ENTRY(temp_widgets->entryProjectId), yon_char_from_int(id));
                }
                
            }
        }
        g_signal_handlers_unblock_by_func(G_OBJECT(temp_widgets->entryProjectId), G_CALLBACK(check_generate_id), NULL);
    }
}
void wrapper_generate_id() {
    int id = generate_id(vec_temp, -1);
    gtk_entry_set_text(GTK_ENTRY(temp_widgets->entryProjectId), yon_char_from_int(id));
}

int generate_id(vector* _vec_temp, int max_id) {
    if (_vec_temp->vectorList.total> 0) {
        for (int index = 0; index < _vec_temp->vectorList.total; index++) {
            config_u_g_p* _config = (config_u_g_p*)_vec_temp->pfVectorGet(_vec_temp, index);
            if (_config->id > max_id) {
                max_id = _config->id;
            }
        }
    }
    max_id++;
    return max_id;
}
int check_generate(vector* _vec_temp, int max_id) {
    if (_vec_temp->vectorList.total> 0) {
        for (int index = 0; index < _vec_temp->vectorList.total; index++) {
            config_u_g_p* _config = (config_u_g_p*)_vec_temp->pfVectorGet(_vec_temp, index);
            if (_config->id == max_id) {
                return 1;
            }
        }
    }
    return 0;
}
void set_activ_project_id() {
    gboolean active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(temp_widgets->chbAuto));
    if (active) {
        gtk_entry_set_text(GTK_ENTRY(temp_widgets->entryProjectId), "");
    }
    gtk_widget_set_sensitive(temp_widgets->entryProjectId, !active);
    gtk_widget_set_sensitive(temp_widgets->btnGenerateProject, !active);
}


