#include "ubl-settings-manager.h"

typedef struct {
    GtkWidget *Window;
    GtkWidget *StatusBox;
    GtkWidget *DefaultSwitch;
    GtkWidget *SectionsBox;
    GtkWidget *NameEntry;
    GtkWidget *CategoriesEntry;
    GtkWidget *ClearButton;
    GtkWidget *AddButton;
    GtkWidget *CancelButton;
    GtkWidget *SaveButton;
    // section_struct *sections;
} settings_section_window;

typedef struct {
    GtkWidget *SectionBox;
    GtkWidget *NameEntry;
    GtkWidget *CategoriesEntry;
    GtkWidget *RemoveButton;
    // GtkWidget *AcceptButton;
} section_struct;

void yon_section_window_add_default(settings_section_window *window);
void on_section_remove(GtkWidget *,section_struct *section);
void on_section_add(GtkWidget *,settings_section_window *window);
void on_section_clear(GtkWidget *,settings_section_window *window);
void on_sections_save(GtkWidget *,settings_section_window *window);
section_struct *yon_section_new();
settings_section_window *yon_section_window_new();


void on_section_remove(GtkWidget *,section_struct *section){
    gtk_widget_destroy(section->SectionBox);
}

section_struct *yon_section_new(){
    section_struct *section = malloc(sizeof(section_struct));
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_section_element);
    section->SectionBox = yon_gtk_builder_get_widget(builder,"SectionBox");
    section->NameEntry = yon_gtk_builder_get_widget(builder,"NameEntry");
    section->CategoriesEntry = yon_gtk_builder_get_widget(builder,"CategoriesEntry");
    section->RemoveButton = yon_gtk_builder_get_widget(builder,"RemoveButton");
    // section->AcceptButton = yon_gtk_builder_get_widget(builder,"AcceptButton");

    g_object_set_data(G_OBJECT(section->SectionBox),"section",section);

    g_signal_connect(G_OBJECT(section->RemoveButton),"clicked",G_CALLBACK(on_section_remove),section);
    return section;
}

void on_section_add(GtkWidget *,settings_section_window *window){
    const char *cat_name = gtk_entry_get_text(GTK_ENTRY(window->NameEntry));
    char *cat_categories = yon_char_new(gtk_entry_get_text(GTK_ENTRY(window->CategoriesEntry)));
    if (cat_categories[strlen(cat_categories)-1]!=';') {
        char *temp = yon_char_append_c(cat_categories,';');
        free(cat_categories);
        cat_categories = temp;
    }
    if (yon_char_is_empty(cat_name)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),_EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->NameEntry);
        return;
    }
    section_struct *section = yon_section_new();
    gtk_box_pack_start(GTK_BOX(window->SectionsBox),section->SectionBox,0,0,0);

    gtk_entry_set_text(GTK_ENTRY(section->NameEntry),cat_name);
    gtk_entry_set_text(GTK_ENTRY(section->CategoriesEntry),cat_categories);

    gtk_entry_set_text(GTK_ENTRY(window->NameEntry),"");
    gtk_entry_set_text(GTK_ENTRY(window->CategoriesEntry),"");
    free(cat_categories);
}

void on_section_clear(GtkWidget *,settings_section_window *window){
    gtk_entry_set_text(GTK_ENTRY(window->NameEntry),"");
    gtk_entry_set_text(GTK_ENTRY(window->CategoriesEntry),"");
}

void on_sections_save(GtkWidget *,settings_section_window *window){
    main_window *widgets = g_object_get_data(G_OBJECT(window->Window),"widgets");
    gsize size;
    config_str parameters = yon_window_config_get_section(sections_section,&size);
    for (gsize i=0;i<size;i++){
        yon_window_config_erase_instant_parameter(parameters[i],sections_section);
    }
    if (!gtk_switch_get_active(GTK_SWITCH(window->DefaultSwitch))){
        GList *list = gtk_container_get_children(GTK_CONTAINER(window->SectionsBox));
        GList *iter;
        for (iter=list;iter;iter=iter->next){
            section_struct *section = g_object_get_data(G_OBJECT(iter->data),"section");
            char *name = (char*)gtk_entry_get_text(GTK_ENTRY(section->NameEntry));
            char *categories = (char*)gtk_entry_get_text(GTK_ENTRY(section->CategoriesEntry));
            yon_window_config_add_instant_parameter(name,sections_section,categories,YON_TYPE_STRING);
        }
    }
    on_subwindow_close(window->Window);
    yon_interface_update(widgets);  

}

void yon_section_window_add_default(settings_section_window *window){
    
    section_struct *cur_section = yon_section_new();
    gtk_box_pack_start(GTK_BOX(window->SectionsBox),cur_section->SectionBox,0,0,0);
    gtk_entry_set_text(GTK_ENTRY(cur_section->NameEntry),_("Personal"));
    gtk_entry_set_text(GTK_ENTRY(cur_section->CategoriesEntry),"X-UBL-SettingsManager;X-UBL-PersonalSettings;");

    cur_section = yon_section_new();
    gtk_box_pack_start(GTK_BOX(window->SectionsBox),cur_section->SectionBox,0,0,0);
    gtk_entry_set_text(GTK_ENTRY(cur_section->NameEntry),_("Hardware"));
    gtk_entry_set_text(GTK_ENTRY(cur_section->CategoriesEntry),"X-UBL-SettingsManager;X-UBL-HardwareSettings;");

    cur_section = yon_section_new();
    gtk_box_pack_start(GTK_BOX(window->SectionsBox),cur_section->SectionBox,0,0,0);
    gtk_entry_set_text(GTK_ENTRY(cur_section->NameEntry),_("System"));
    gtk_entry_set_text(GTK_ENTRY(cur_section->CategoriesEntry),"X-UBL-SettingsManager;X-UBL-SystemSettings;");

    cur_section = yon_section_new();
    gtk_box_pack_start(GTK_BOX(window->SectionsBox),cur_section->SectionBox,0,0,0);
    gtk_entry_set_text(GTK_ENTRY(cur_section->NameEntry),_("Misc"));
    gtk_entry_set_text(GTK_ENTRY(cur_section->CategoriesEntry),"X-UBL-SettingsManager;");
}

settings_section_window *yon_section_window_new(){
    settings_section_window *window = malloc(sizeof(settings_section_window));

    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_settings_section);
    window->Window = yon_gtk_builder_get_widget(builder,"Window");
    window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    window->DefaultSwitch = yon_gtk_builder_get_widget(builder,"DefaultSwitch");
    window->SectionsBox = yon_gtk_builder_get_widget(builder,"SectionsBox");
    window->NameEntry = yon_gtk_builder_get_widget(builder,"NameEntry");
    window->CategoriesEntry = yon_gtk_builder_get_widget(builder,"CategoriesEntry");
    window->ClearButton = yon_gtk_builder_get_widget(builder,"ClearButton");
    window->AddButton = yon_gtk_builder_get_widget(builder,"AddButton");
    window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    window->SaveButton = yon_gtk_builder_get_widget(builder,"SaveButton");

    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    g_signal_connect(G_OBJECT(window->SaveButton),"clicked",G_CALLBACK(on_sections_save),window);
    g_signal_connect(G_OBJECT(window->AddButton),"clicked",G_CALLBACK(on_section_add),window);
    g_signal_connect(G_OBJECT(window->ClearButton),"clicked",G_CALLBACK(on_section_clear),window);
    g_signal_connect(G_OBJECT(window->DefaultSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch_inversed),window->SectionsBox);
    g_signal_connect(G_OBJECT(window->DefaultSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch_inversed),window->NameEntry);
    g_signal_connect(G_OBJECT(window->DefaultSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch_inversed),window->CategoriesEntry);
    g_signal_connect(G_OBJECT(window->DefaultSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch_inversed),window->AddButton);
    g_signal_connect(G_OBJECT(window->DefaultSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch_inversed),window->ClearButton);

    gsize parameters_size;
    config_str parameters = NULL;
    parameters = yon_window_config_get_section(sections_section,&parameters_size);
    if (parameters_size){
        for (gsize i=0;i<parameters_size;i++){
            char *param;
            yon_window_config_get_parameter(sections_section,parameters[i],&param,YON_TYPE_STRING);
            section_struct *cur_section = yon_section_new();
            gtk_box_pack_start(GTK_BOX(window->SectionsBox),cur_section->SectionBox,0,0,0);
            gtk_entry_set_text(GTK_ENTRY(cur_section->NameEntry),parameters[i]);
            gtk_entry_set_text(GTK_ENTRY(cur_section->CategoriesEntry),param);

        }
    } else {
        gtk_switch_set_active(GTK_SWITCH(window->DefaultSwitch),1);
        yon_section_window_add_default(window);
    }
    return window;
}

void yon_section_window_open(GtkWidget *, settings_window *settings){
    main_window *widgets = g_object_get_data(G_OBJECT(settings->Window),"widgets");
    settings_section_window *window = yon_section_window_new();
    g_object_set_data(G_OBJECT(window->Window),"widgets",widgets);
    yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(settings->Window),TITLE_LABEL,icon_path,"sections_window");
}