#include "ubl-settings-manager.h"

gboolean on_settings_doubleckick_switched(GtkWidget *, int state,settings_window *window){
    window->doubleclick = state;
    return 0;
}

gboolean on_settings_size_changed(GtkRange* , GtkScrollType* , gdouble value, settings_window *window){
    long val = (long)value;
    int size = val-val%8;
    if (size<24) size=24;
    if (size>64) size=64;
    window->icon_size = size;
    
    char *size_full = g_strdup_printf("%dx%d",size,size);
    gtk_label_set_text(GTK_LABEL(window->SizeLabel),size_full);
    gtk_image_set_from_pixbuf(GTK_IMAGE(window->SizeIcon),gtk_icon_info_load_icon(gtk_icon_theme_lookup_icon_for_scale(gtk_icon_theme_get_default(), icon_path,size,1,GTK_ICON_LOOKUP_FORCE_SIZE),NULL));
    free(size_full);
    return 0;
}

void on_settings_accept(GtkWidget *, settings_window *window){
    main_window *widgets = g_object_get_data(G_OBJECT(window->Window),"widgets");
    yon_window_config_add_instant_parameter(icon_size_parameter,settings_section,&window->icon_size,YON_TYPE_INT);
    main_config.apps_icon_size = window->icon_size;

    char *cur_theme = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->ThemeCombo));
    if (cur_theme)
        yon_window_config_add_instant_parameter(theme_parameter,settings_section,cur_theme,YON_TYPE_STRING);
    
    if (window->doubleclick>-1){
        yon_window_config_add_instant_parameter(double_click_parameter,settings_section,&window->doubleclick,YON_TYPE_INT);
    }
    yon_interface_update(widgets);
    on_subwindow_close(window->Window);
}

settings_window *yon_settings_window_new(){
    settings_window *window = malloc(sizeof(settings_window));
    memset(window,0,sizeof(settings_window));
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_settings);
    window->Window = yon_gtk_builder_get_widget(builder,"Window");
    window->SizeSlider = yon_gtk_builder_get_widget(builder,"SizeSlider");
    window->SizeLabel = yon_gtk_builder_get_widget(builder,"SizeLabel");
    window->SizeIcon = yon_gtk_builder_get_widget(builder,"SizeIcon");
    window->ThemeCombo = yon_gtk_builder_get_widget(builder,"ThemeCombo");
    window->DoubleClickSwitch = yon_gtk_builder_get_widget(builder,"DoubleClickSwitch");
    window->SectionsConfigurationButton = yon_gtk_builder_get_widget(builder,"SectionsConfigurationButton");
    window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    window->AcceptButton = yon_gtk_builder_get_widget(builder,"AcceptButton");
    window->doubleclick = -1;
    

    g_signal_connect(G_OBJECT(window->DoubleClickSwitch),"state-set",G_CALLBACK(on_settings_doubleckick_switched),window);
    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(on_settings_accept),window);
    g_signal_connect(G_OBJECT(window->SizeSlider),"change-value",G_CALLBACK(on_settings_size_changed),window);

    int size;
    config_str themes = (config_str)g_hash_table_get_keys_as_array(main_config.themes,(guint*)&size);
    qsort(themes,size,sizeof(char*),(__compar_fn_t)yon_char_parsed_compare);
    for (int i=0;i<size;i++){
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(window->ThemeCombo),themes[i],themes[i]);
    }
    char *theme_id = NULL;
    if (yon_window_config_get_parameter(settings_section,theme_parameter,&theme_id,YON_TYPE_STRING)){ 
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(window->ThemeCombo),_(theme_id));
    } else {
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(window->ThemeCombo),MAIN_THEME_LABEL);
    }
    if (!yon_window_config_get_parameter(settings_section,icon_size_parameter,&window->icon_size,YON_TYPE_INT)){
        window->icon_size=56;
    }
    gtk_range_set_value(GTK_RANGE(window->SizeSlider),window->icon_size);
    on_settings_size_changed(NULL,NULL,window->icon_size,window);

    int double_click_activate;
    if (yon_window_config_get_parameter(settings_section,double_click_parameter,&double_click_activate,YON_TYPE_BOOLEAN)){
        window->doubleclick=double_click_activate;
        gtk_switch_set_active(GTK_SWITCH(window->DoubleClickSwitch),double_click_activate);
    } else {
        window->doubleclick=1;
        gtk_switch_set_active(GTK_SWITCH(window->DoubleClickSwitch),1);

    }

    return window;
}

void on_settings_open(GtkWidget *, main_window *widgets){
    settings_window *window = yon_settings_window_new();
    g_signal_connect(G_OBJECT(window->SectionsConfigurationButton),"clicked",G_CALLBACK(yon_section_window_open),window);
    yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),SETTINGS_LABEL,icon_path,"settings_window");
    gtk_widget_show(window->Window);
    g_object_set_data(G_OBJECT(window->Window),"widgets",widgets);
    
}