#include "view_filters.h"
char* flag_filters_u_g_p = NULL;
filters_window *filters_widgets = NULL;
GtkTreeView* _tree_view = NULL;
const int len_user_group = 8;
const int len_project = 10;
gboolean flag_filters_user[8];
gboolean flag_filters_group[8];
gboolean flag_filters_project[10];
filters_window *filters_setup_window(char* glade_path){
    if (filters_widgets == NULL) {
        filters_widgets = malloc(sizeof(filters_window));
        GtkBuilder *builder = gtk_builder_new_from_resource(glade_path);
        filters_widgets->Window = yon_gtk_builder_get_widget(builder,"wndFilters");
        filters_widgets->chbFilterName = yon_gtk_builder_get_widget(builder,"chbFilterName");
        filters_widgets->chbFilterSoftLimitFile = yon_gtk_builder_get_widget(builder,"chbFilterSoftLimitFile");
        filters_widgets->chbFilterFiles = yon_gtk_builder_get_widget(builder,"chbFilterFiles");
        filters_widgets->chbFilterHardLimitSizeDelay = yon_gtk_builder_get_widget(builder,"chbFilterHardLimitSizeDelay");
        filters_widgets->chbFilterHardLimitSize = yon_gtk_builder_get_widget(builder,"chbFilterHardLimitSize");
        filters_widgets->chbFilterSoftLimitSize = yon_gtk_builder_get_widget(builder,"chbFilterSoftLimitSize");
        filters_widgets->chbFilterQuotas = yon_gtk_builder_get_widget(builder,"chbFilterQuotas");
        filters_widgets->chbFilterSize = yon_gtk_builder_get_widget(builder,"chbFilterSize");
        filters_widgets->btnFiltersCancel = yon_gtk_builder_get_widget(builder,"btnFiltersCancel");
        filters_widgets->btnFiltersSave = yon_gtk_builder_get_widget(builder,"btnFiltersSave");
        filters_widgets->chbFilterHardLimitFile = yon_gtk_builder_get_widget(builder,"chbFilterHardLimitFile");
        filters_widgets->chbFilterHardLimitFileDelay = yon_gtk_builder_get_widget(builder,"chbFilterHardLimitFileDelay");
        filters_widgets->chbProjectName = yon_gtk_builder_get_widget(builder,"chbProjectName");
        filters_widgets->chbFilterDevice = yon_gtk_builder_get_widget(builder,"chbFilterDevice");
        filters_widgets->chkFilterID = yon_gtk_builder_get_widget(builder,"chkFilterID");
        filters_widgets->chbFilterCatalog = yon_gtk_builder_get_widget(builder,"chbFilterCatalog");
        if (strcmp(flag_filters_u_g_p, "prjquota")==0) {
            gtk_widget_hide(filters_widgets->chbFilterName);
        }
        else {
            gtk_widget_hide(filters_widgets->chbProjectName);
            gtk_widget_hide(filters_widgets->chkFilterID);
            gtk_widget_hide(filters_widgets->chbFilterCatalog);
        }
        filters_event(filters_widgets);
    }
    filters_set_flag();
    return filters_widgets;
}

gboolean* filters_get_flag_user() {
    flag_filters_user[0] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterName));
    flag_filters_user[1] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterDevice));
    flag_filters_user[2] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterSoftLimitSize));
    flag_filters_user[3] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterHardLimitSize));
    flag_filters_user[4] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterSoftLimitFile));
    flag_filters_user[5] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterHardLimitFile));
    flag_filters_user[6] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterHardLimitSizeDelay));
    flag_filters_user[7] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterHardLimitFileDelay));
    return flag_filters_user;
}


gboolean* filters_get_flag_group() {
    flag_filters_group[0] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterName));
    flag_filters_group[1] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterDevice));
    flag_filters_group[2] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterSoftLimitSize));
    flag_filters_group[3] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterHardLimitSize));
    flag_filters_group[4] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterSoftLimitFile));
    flag_filters_group[5] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterHardLimitFile));
    flag_filters_group[6] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterHardLimitSizeDelay));
    flag_filters_group[7] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterHardLimitFileDelay));
    return flag_filters_group;
}


gboolean* filters_get_flag_project() {
    flag_filters_project[0] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterDevice));
    flag_filters_project[1] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterCatalog));
    flag_filters_project[2] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chkFilterID));
    flag_filters_project[3] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbProjectName));
    flag_filters_project[4] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterSoftLimitSize));
    flag_filters_project[5] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterHardLimitSize));
    flag_filters_project[6] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterSoftLimitFile));
    flag_filters_project[7] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterHardLimitFile));
    flag_filters_project[8] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterHardLimitSizeDelay));
    flag_filters_project[9] = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterHardLimitFileDelay));
    return flag_filters_project;
}

void filters_set_name(char* _flag_filters_u_g_p) {
    flag_filters_u_g_p = _flag_filters_u_g_p;

}

void filters_get_tree_view(GtkWidget *tree_view) {
    _tree_view = GTK_TREE_VIEW(tree_view);
}

void filters_tree_view_update() {
    int n_end = len_user_group;
    gboolean* array_flags = NULL;
    if (strcmp(flag_filters_u_g_p, "prjquota")==0) {
        n_end = len_project;
        array_flags = filters_get_flag_project();
    }
    else {
        n_end = len_user_group;
        if (strcmp(flag_filters_u_g_p, "usrquota")==0) {
            array_flags = filters_get_flag_user();  
        }
        else {
            array_flags = filters_get_flag_group();
        }
    }
    for (int i = 0; i < n_end; i++) {
        GtkTreeViewColumn* column = gtk_tree_view_get_column(_tree_view, i);
        gtk_tree_view_column_set_visible(column, array_flags[i]);
    }
}
void filters_save(GtkWidget *self) {
    filters_tree_view_update();
    filters_on_hide_subwindow(self);

}

void filters_event(filters_window *widgets) {
    g_signal_connect(G_OBJECT(widgets->btnFiltersSave), "clicked",G_CALLBACK(filters_save),widgets->Window);
    g_signal_connect(G_OBJECT(widgets->btnFiltersCancel), "clicked",G_CALLBACK(filters_on_hide_subwindow),widgets->Window);
    g_signal_connect(G_OBJECT(widgets->Window), "destroy", G_CALLBACK(filters_destroy), NULL);
}

void filters_destroy(GtkWidget *self) {
    filters_widgets = NULL;
}

void filters_localization(filters_window *widgets) {

}

void filters_on_hide_subwindow(GtkWidget *self) {
    gtk_widget_destroy(gtk_widget_get_toplevel(self));
    filters_widgets = NULL;
}

filters_window *get_widget_filters() {
    return filters_widgets;
}

void filters_show(GtkWidget *self, char* glade_path) {
    if (filters_widgets != NULL) {
        gtk_widget_show(filters_widgets->Window);
    }
    else {
        filters_setup_window(glade_path);
        gtk_widget_show(filters_widgets->Window);
    }
}



void filters_init_flag() {
    for (int i=0; i<len_user_group;i++) {
        flag_filters_user[i] = 1;
        flag_filters_group[i] = 1;
        flag_filters_project[i] = 1;
    }
    flag_filters_project[8] = 1;
    flag_filters_project[9] = 1;
}

void filters_set_flag() {
    gboolean* array_flags = NULL;
    if (strcmp(flag_filters_u_g_p, "prjquota")==0) {
        array_flags = flag_filters_project;
    }
    else {
        if (strcmp(flag_filters_u_g_p, "usrquota")==0) {
            array_flags = flag_filters_user;  
        }
        else {
            array_flags = flag_filters_group;
        }
    }
    if (strcmp(flag_filters_u_g_p,"prjquota")) {
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterName),array_flags[0] );
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterDevice), array_flags[1]);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterSoftLimitSize), array_flags[2]);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterHardLimitSize), array_flags[3]);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterSoftLimitFile), array_flags[4]);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterHardLimitFile), array_flags[5]);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterHardLimitSizeDelay), array_flags[6]);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterHardLimitFileDelay), array_flags[7]);
    }
    else if (strcmp(flag_filters_u_g_p,"prjquota")==0) {
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterDevice),array_flags[0] );
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterCatalog), array_flags[1]);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(filters_widgets->chkFilterID), array_flags[2]);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(filters_widgets->chbProjectName), array_flags[3]);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterSoftLimitSize), array_flags[4]);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterHardLimitSize), array_flags[5]);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterSoftLimitFile), array_flags[6]);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterHardLimitFile), array_flags[7]);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterHardLimitSizeDelay), array_flags[8]);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(filters_widgets->chbFilterHardLimitFileDelay), array_flags[9]);
    }
}