#include "view_check_user_table.h"

char* glade_path = NULL;
char* flag_u_g = NULL;
char* table_u_g_set_add_edit;
table_u_g* table_widgets;
vector* _actors;

void table_u_g_get_actors(vector* actors) {
    _actors = actors;
}

table_u_g* get_widget_table_u_g() {
    return table_widgets;
}

void table_u_g_set_fill_user_group() {
    int size = 0;
    g_object_ref(table_widgets->liststoreUsersGroups);
    gtk_list_store_clear(table_widgets->liststoreUsersGroups);
    gboolean active1 = !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(table_widgets->chkActivUsersGroup));
    if (strcmp(flag_u_g, "grpquota")==0) {
        gtk_button_set_label(GTK_BUTTON(table_widgets->chkActivUsersGroup), STR_CHK_LABEL_WRITE_ALL_GROUPS);
        gtk_label_set_label(GTK_LABEL(table_widgets->lblHeadQuotaUsersGroups), STR_GROUPS);
        char** array_groups = philos_list_group(&size,active1);
        table_u_g_fill_tree_view(array_groups, size);
        GtkTreeViewColumn* column = gtk_tree_view_get_column(GTK_TREE_VIEW(table_widgets->treeViewCheckUsersGroups), 1);
        gtk_tree_view_column_set_title(column, STR_GROUPS);
    }
    else if (strcmp(flag_u_g, "usrquota") == 0) {
        gtk_button_set_label(GTK_BUTTON(table_widgets->chkActivUsersGroup), STR_CHK_LABEL_WRITE_ALL_USERS);
        gtk_label_set_label(GTK_LABEL(table_widgets->lblHeadQuotaUsersGroups), STR_USERS);
        char** array_user = philos_list_user(&size, active1);
        table_u_g_fill_tree_view(array_user, size);
    }
    set_actors(_actors);
}

table_u_g *setup_table_u_g(){
    /* Widgets getting | Получение виджетов */
    table_widgets = malloc(sizeof(table_u_g));
    device_disk_parsed();
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path);
    table_widgets->Window = yon_gtk_builder_get_widget(builder,"tableUserGroupWindow");
    table_widgets->chkActivUsersGroup = yon_gtk_builder_get_widget(builder,"chkActivUsersGroup");
    table_widgets->btnCancel = yon_gtk_builder_get_widget(builder,"quotegroupCancelButton1");
    table_widgets->btnSave = yon_gtk_builder_get_widget(builder,"quotegroupSaveButton1");
    table_widgets->lblHeadQuotaUsersGroups = yon_gtk_builder_get_widget(builder,"lblHeadQuotaUsersGroups");
    table_widgets->treeViewCheckUsersGroups = yon_gtk_builder_get_widget(builder,"treeViewCheckUsersGroups");
    table_widgets->checkColUserGroup = GTK_CELL_RENDERER_TOGGLE(gtk_builder_get_object(builder,"checkColUserGroup"));
    table_widgets->liststoreUsersGroups = GTK_LIST_STORE(gtk_builder_get_object(builder, "liststoreUsersGroups"));
    table_u_g_set_fill_user_group();
    table_u_g_event();
    g_signal_connect(G_OBJECT(table_widgets->chkActivUsersGroup), "toggled", G_CALLBACK(table_u_g_set_fill_user_group), NULL);
}


void table_u_g_set_glade_path(char* _glade_path) {
    glade_path = _glade_path;
}

void table_u_g_set_flag(char* _flag_u_g, char* _table_u_g_set_add_edit) {
    flag_u_g = _flag_u_g;
    table_u_g_set_add_edit = _table_u_g_set_add_edit;
}


void table_u_g_fill_tree_view(char** array, int size) {
    GtkTreeModel *model = GTK_TREE_MODEL(table_widgets->liststoreUsersGroups);
    g_object_ref(table_widgets->liststoreUsersGroups);
    gtk_list_store_clear(table_widgets->liststoreUsersGroups);
    for (int i=0; i<size; i++){
        GtkTreeIter iter;
        gtk_list_store_append(table_widgets->liststoreUsersGroups,&iter);
        gtk_list_store_set(table_widgets->liststoreUsersGroups,&iter,
                                                            0,0,
                                                            1,array[i],
                                                            -1);
    } 
    gtk_tree_view_set_model(GTK_TREE_VIEW(table_widgets->treeViewCheckUsersGroups), model);
}

void table_u_g_show(GtkWidget *self, char* glade_path) {
    if (table_widgets != NULL) {
        gtk_widget_show(table_widgets->Window);
    }
    else {
        setup_table_u_g(glade_path);
        gtk_widget_show(table_widgets->Window);
    }
}
void chk_toggled_user_group (
  GtkCellRendererToggle* self,
  gchar* path,
  gpointer user_data
) {
    GtkTreeIter iter;
    GtkTreeModel *model = GTK_TREE_MODEL(table_widgets->liststoreUsersGroups);
    gtk_tree_model_get_iter_from_string(model,&iter,path);
    gboolean flag;
    gtk_tree_model_get(model,&iter,0, &flag,-1);
    gtk_list_store_set(table_widgets->liststoreUsersGroups,&iter, 0,!flag,-1);
    gtk_tree_view_set_model(GTK_TREE_VIEW(table_widgets->treeViewCheckUsersGroups), model);
}



vector* get_actors_table(vector* actors) {
    model_free_vector_string(actors);
    vectorFree(actors);
    actors = malloc(sizeof(vector));
    vector_init(actors);
    GtkTreeIter iter;
    GtkTreeModel *model = GTK_TREE_MODEL(table_widgets->liststoreUsersGroups);
    int valid = 1;
    for(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(table_widgets->liststoreUsersGroups),&iter);valid==1;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(table_widgets->liststoreUsersGroups),&iter)){
        int is_active=0;
        char* user_group;
        gtk_tree_model_get(model,&iter,0,&is_active,-1);
        gtk_tree_model_get(model,&iter,1,&user_group,-1);
        if (is_active){
            actors->pfVectorAdd(actors, yon_char_new(user_group));
        }
    }
    return actors;
}

void table_u_g_destroy(GtkWidget *self) {
    table_widgets = NULL;
}

void table_u_g_localization(table_u_g *widgets) {

}

void table_u_g_on_hide_subwindow(GtkWidget *self) {
    gtk_widget_destroy(gtk_widget_get_toplevel(self));
    table_widgets = NULL;
}

table_u_g *table_u_g_get_widget_filters() {
    return table_widgets;
}

void set_actors(vector* actors) {
    GtkTreeIter iter;
    GtkTreeModel *model = GTK_TREE_MODEL(table_widgets->liststoreUsersGroups);
    int valid = 1;
    for(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(table_widgets->liststoreUsersGroups),&iter);valid==1;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(table_widgets->liststoreUsersGroups),&iter)){
        char* user_group;
        gtk_tree_model_get(model,&iter,1,&user_group,-1);
        if (actors->vectorList.total> 0) {
            for (int i = 0; i < actors->vectorList.total; i++) {
                char* str_actors = (char*)actors->pfVectorGet(actors, i);
                if (strcmp(str_actors, user_group)==0){
                    gtk_list_store_set(table_widgets->liststoreUsersGroups,&iter, 0,1,-1);
                }    
            }      
        }
    }
    gtk_tree_view_set_model(GTK_TREE_VIEW(table_widgets->treeViewCheckUsersGroups), model);
}

void table_btn_block_save() {
    GtkTreeIter iter;
    GtkTreeModel *model = GTK_TREE_MODEL(table_widgets->liststoreUsersGroups);
    int valid = 1;
    int flag_activ = 0;
    for(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(table_widgets->liststoreUsersGroups),&iter);valid==1;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(table_widgets->liststoreUsersGroups),&iter)){
        int is_active=0;
        gtk_tree_model_get(model,&iter,0,&is_active,-1);
        char* user_group;
        if (is_active){
            flag_activ = 1;
        }
    }
    gtk_widget_set_sensitive(table_widgets->btnSave,flag_activ);
}