#include "ubl-settings-events.h"

gboolean yon_sync_columns_idle(gpointer user_data){
    ColumnSyncData *data = (ColumnSyncData *)user_data;

    for (guint col_index = 0; col_index < data->n_columns; col_index++) {
        gint max_width = 0;
        GPtrArray *columns = g_ptr_array_new();

        for (guint i = 0; i < data->treeviews->len; i++) {
            GtkTreeView *view = g_ptr_array_index(data->treeviews, i);
            GtkTreeViewColumn *col = gtk_tree_view_get_column(view, col_index);
            if (col) {
                gint width = gtk_tree_view_column_get_width(col);
                if (width > max_width)
                    max_width = width;
                g_ptr_array_add(columns, col);
            }
        }

        for (guint i = 0; i < columns->len; i++) {
            GtkTreeViewColumn *col = g_ptr_array_index(columns, i);
            gtk_tree_view_column_set_sizing(col, GTK_TREE_VIEW_COLUMN_FIXED);
            gtk_tree_view_column_set_fixed_width(col, max_width);
        }

        g_ptr_array_free(columns, TRUE);
    }

    g_ptr_array_free(data->treeviews, TRUE);
    g_free(data);

    return G_SOURCE_REMOVE;
}

void yon_sync_treeview_columns(GPtrArray *treeviews, guint n_columns){
    ColumnSyncData *data = g_new0(ColumnSyncData, 1);
    data->treeviews = g_ptr_array_new();

    for (guint i = 0; i < treeviews->len; i++) {
        g_ptr_array_add(data->treeviews, g_ptr_array_index(treeviews, i));
    }

    data->n_columns = n_columns;

    g_idle_add(yon_sync_columns_idle, data);
}

yon_menu_table *yon_menu_table_get_selected(GtkListBoxRow *row){
    return (yon_menu_table*)g_object_get_data(G_OBJECT(row), "menu_table");

}

void yon_sequence_save_rules(yon_menu_table *table, void*){
    char *key = yon_char_unite(table->key,":",table->enabled?"enable":"disable",NULL);
    char *value = yon_char_unite(!yon_char_is_empty(table->rule)?table->rule:"",!yon_char_is_empty(table->description)?"#":"",!yon_char_is_empty(table->description)?table->description:"",NULL);
    yon_config_register(AUDITD(key),AUDITD_cmd(key),g_shell_quote(value));
    yon_config_compare_ignore_set(AUDITD(key),1);
}

void yon_menu_tables_save(main_window *){
    g_sequence_foreach(main_config.trees_order,(GFunc)yon_sequence_save_rules,NULL);
}

void on_enabled_toggled(GtkWidget *, yon_menu_table *table){
    int status = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(table->EnabledCheck));
    table->enabled=status;
}

void yon_menu_table_update(yon_menu_table *table){
    if (yon_char_is_empty(table->key)||(!yon_char_is_empty(table->key)&&strstr(table->key,"comment_")&&yon_char_is_empty(table->rule))){
        if (yon_char_is_empty(table->key)){
            char *comment_id = yon_char_from_int(main_config.comments_size++);
            table->key = yon_char_append("comment_",comment_id);
            free(comment_id);
        }
        gtk_widget_hide(table->IdLabel);
        gtk_widget_hide(table->RuleLabel);
        gtk_widget_hide(table->Separator1);
        gtk_widget_hide(table->Separator2);
        gtk_widget_hide(table->Separator3);
        gtk_widget_hide(table->EnabledCheck);
        char *description_comment = yon_char_unite("<b>",g_markup_escape_text(_(table->description),strlen(_(table->description))),"</b>",NULL);
        gtk_label_set_markup(GTK_LABEL(table->DescriptionLabel),yon_char_wrap_to_length_str(description_comment,50));
        (free(description_comment));
    } else {
        if (table->enabled==1){
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(table->EnabledCheck),1);
        } else {
            int size;
            config_str parsed = yon_char_parse(table->key,&size,":");
            if (size>1) {
                if (!strcmp(parsed[1],"enable")){
                    table->enabled=1;
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(table->EnabledCheck),1);
                }
            }
            table->key=parsed[0];
        }
        gtk_label_set_text(GTK_LABEL(table->IdLabel),yon_char_wrap_to_length_str(table->key,25));
        gtk_label_set_text(GTK_LABEL(table->RuleLabel),yon_char_wrap_to_length_str(table->rule,25));
        gtk_label_set_markup(GTK_LABEL(table->DescriptionLabel),!yon_char_is_empty(table->description)?g_markup_escape_text(_(table->description),strlen(_(table->description))):"");
    }
}

yon_menu_table *yon_menu_table_new(){
    yon_menu_table *table = new(yon_menu_table);
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_table);
    table->MainBox = yon_gtk_builder_get_widget(builder,"MainBox");
    table->MainListBox = yon_gtk_builder_get_widget(builder,"MainListBox");
    table->EnabledCheck = yon_gtk_builder_get_widget(builder,"EnabledCheck");
    table->IdLabel = yon_gtk_builder_get_widget(builder,"IdLabel");
    table->RuleLabel = yon_gtk_builder_get_widget(builder,"RuleLabel");
    table->DescriptionLabel = yon_gtk_builder_get_widget(builder,"DescriptionLabel");
    table->Separator1 = yon_gtk_builder_get_widget(builder,"Separator1");
    table->Separator2 = yon_gtk_builder_get_widget(builder,"Separator2");
    table->Separator3 = yon_gtk_builder_get_widget(builder,"Separator3");

    g_signal_connect(G_OBJECT(table->EnabledCheck),"toggled",G_CALLBACK(on_enabled_toggled),table);

    table->enabled=0;
    table->key=NULL;
    table->rule=NULL;
    table->description=NULL;

    gtk_widget_show(table->MainBox);
    return table;
}

void yon_menu_table_destroy(yon_menu_table *table){
    if (table){
        g_hash_table_remove(main_config.trees,table->key);
        g_sequence_remove(table->order_position);
        if (!yon_char_is_empty(table->key))
            free(table->key);
        if (!yon_char_is_empty(table->rule))
            free(table->rule);
        if (!yon_char_is_empty(table->description))
            free(table->description);
    }
}