#include "libublsettingsui-gtk3.h"

void on_confuguration_combo_box_changed(GtkComboBox *self, char *id);
void __yon_on_boolean_parameter_toggled(GtkToggleButton *self ,char *id);

struct yon_configuration_combo_parameter{
    GtkWidget *Box;
    GtkWidget *Label;
    GtkWidget *ComboBox;
    char *id;
};
// yon_configuration_entry_parameter *yon_configuration_entry_parameter_new(char *id, char *label_text){
//     yon_configuration_entry_parameter *parameter = malloc(sizeof(yon_configuration_entry_parameter));
//     parameter->Box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5);
//     parameter->Label = gtk_label_new(label_text);
//     parameter->Entry = gtk_entry_new();
//     parameter->id = yon_char_new(id);
//     gtk_box_pack_start(GTK_BOX(parameter->Box),parameter->Label,1,1,0);
//     gtk_box_pack_start(GTK_BOX(parameter->Box),parameter->Entry,1,1,0);
//     gtk_widget_show_all(parameter->Box);
//     return parameter;
// }

// yon_configuration_parameters *__yon_settings_parameters = NULL;

// GtkWidget *yon_configuration_combo_box_parameter_new(char *id){
//     struct yon_configuration_combo_parameter *parameter = malloc(sizeof(struct yon_configuration_combo_parameter));
//     parameter->Box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5);
//     parameter->Label = gtk_label_new(NULL);
//     parameter->ComboBox = gtk_combo_box_text_new();
//     parameter->id = yon_char_new(id);
//     g_object_set_data(G_OBJECT(parameter->Box),"data_struct",parameter);
//     gtk_box_pack_start(GTK_BOX(parameter->Box),parameter->Label,0,0,0);
//     gtk_box_pack_start(GTK_BOX(parameter->Box),parameter->ComboBox,0,0,0);
// //     for (int i=0;parameters[i]&&parameters[i+1];i+=2){
// //         gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(parameter->ComboBox),parameters[i],parameters[i+1]);
// //     }
// //     char *value = NULL;
// //     yon_window_config_get_parameter("settings",id,&value,YON_TYPE_STRING);
// //     if (!yon_char_is_empty(value)){
// //         gtk_combo_box_set_active_id(GTK_COMBO_BOX(parameter->ComboBox),value);
// //     } else {
// //         gtk_combo_box_set_active(GTK_COMBO_BOX(parameter->ComboBox),0);
// //     }
// //     g_signal_connect(G_OBJECT(parameter->ComboBox),"changed",G_CALLBACK(on_confuguration_combo_box_changed),id);
// //     yon_configuration_parameters *target = (yon_configuration_parameters*)yon_dictionary_get((dictionary**)&__yon_settings_parameters,id);
// //     if (target){
// //         if (target->func){
// //             g_signal_connect(G_OBJECT(parameter->ComboBox),"changed",G_CALLBACK(target->func),target->value);
// //         }
// //     }
// //     gtk_widget_show_all(parameter->Box);
//     return parameter->Box;
// }


// GtkWidget *__yon_configuration_boolean_parameter_new(char *id, char *label_text){
//     yon_configuration_boolean_parameter *parameter = malloc(sizeof(yon_configuration_boolean_parameter));
//     parameter->Box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5);
//     parameter->CheckButton = gtk_check_button_new_with_label(label_text);
//     parameter->id = yon_char_new(id);

//     g_signal_connect(G_OBJECT(parameter->CheckButton),"toggled",G_CALLBACK(__yon_on_boolean_parameter_toggled),parameter->id);
//     gtk_box_pack_start(GTK_BOX(parameter->Box),parameter->CheckButton,0,0,0);
//     return parameter;
// }

// //parameter section

// void yon_configuration_window_add_boolean_parameter(enum CONFIGURATION_PARAMETER_TYPE type, char *id, char *label){
//         yon_configuration_parameter_add_or_create_if_exists_with_data(__yon_settings_parameters,id,label);
//         yon_configuration_parameters *parameter = (yon_configuration_parameters*)yon_dictionary_get_last((dictionary*)__yon_settings_parameters);
//          if (parameter){
//             parameter->type = CONFIGURATION_PARAMETER_BOOL;
//         }
// }

char *yon_settings_configuration_get(char *id){
    char *return_val = NULL;
    yon_window_config_get_parameter(yon_configuration_window_section,id,&return_val,YON_TYPE_STRING);
    return return_val;
}

//signnals section

// void __yon_on_boolean_parameter_toggled(GtkToggleButton *self ,char *id){
//     yon_configuration_parameters *current = (yon_configuration_parameters*)yon_dictionary_get((dictionary**)&__yon_settings_parameters,id);
//     if (current){
//         current->data= gtk_toggle_button_get_active(self)?(void*)1:(void*)0;
//     }
// }


// void yon_configuration_window_add_boolean_parameter(char *id, void (*parameter_update_func)(GtkWidget*)){
//     struct yon_configuration_window_custom_parameter *custom_parameter_data = malloc(sizeof(struct yon_configuration_window_custom_parameter));
//     memset(custom_parameter_data,0,sizeof(struct yon_configuration_window_custom_parameter));
//     custom_parameter_data->custom_parameter_create_func = yon_configuration_boolean_parameter_new;
//     yon_configuration_window_add_custom_parameter(id,custom_parameter_data);
// }

// void yon_on_configuration_window_combo_changed(GtkWidget *root){
//     struct yon_configuration_combo_parameter *parameter = g_object_get_data(G_OBJECT(root),"data_struct");
//     const char *param_id = gtk_combo_box_get_active_id(GTK_COMBO_BOX(parameter->ComboBox));
//     yon_window_config_add_instant_parameter(parameter->id,"CONFIGURATION_WINDOW_PARAMETERS",(char*)param_id,YON_TYPE_STRING);
// }

// void yon_configuration_window_add_combo_box_parameter(char *id, void (*parameter_update_func)(GtkWidget*)){
//     struct yon_configuration_window_custom_parameter *custom_parameter_data = malloc(sizeof(struct yon_configuration_window_custom_parameter));
//     memset(custom_parameter_data,0,sizeof(struct yon_configuration_window_custom_parameter));
//     custom_parameter_data->custom_parameter_create_func = yon_configuration_combo_box_parameter_new;
//     yon_configuration_window_add_custom_parameter(id,custom_parameter_data);
// }

// init section

void __yon_on_ubl_settings_window_open();
gboolean yon_on_settings_window_accept(GtkWidget*,GdkEvent *,ubl_settings_window *window);
char *__yon_configuration_window_custom_name = NULL;

void yon_ubl_settings_window_init(GtkMenu *menu){
    if (!yon_window_config_check_init()) return;
    GtkWidget *menu_item = gtk_menu_item_new();
    GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,0);
    textdomain(template_ui_LocaleName);
    GtkWidget *label = NULL;
    if (!yon_char_is_empty(__yon_configuration_window_custom_name)){
        label = gtk_label_new(__yon_configuration_window_custom_name);
    } else {
        label = gtk_label_new(CONFIG_WINDOW_MENU_LABEL);
    }
    textdomain(template_app_information.app_locale);
    gtk_style_context_add_class(gtk_widget_get_style_context(menu_item),"menuitemtop");
    GtkWidget *image = gtk_image_new_from_icon_name("com.ublinux.libublsettingsui-gtk3.settings-symbolic",GTK_ICON_SIZE_BUTTON);
    gtk_menu_shell_prepend(GTK_MENU_SHELL(menu),menu_item);
    gtk_container_add(GTK_CONTAINER(menu_item),box);
    gtk_box_pack_start(GTK_BOX(box),image,0,0,5);
    gtk_box_pack_start(GTK_BOX(box),label,0,0,5);
    gtk_widget_show_all(menu_item);
    g_signal_connect(G_OBJECT(menu_item),"activate",G_CALLBACK(__yon_on_ubl_settings_window_open),NULL);
}

ubl_settings_window *yon_ubl_settings_window_new(){
    ubl_settings_window *window = malloc(sizeof(ubl_settings_window));
    GtkBuilder *builder = gtk_builder_new_from_resource("/com/ublinux/ui/libublsettingsui-gtk3-config-window.glade");
    window->window = yon_gtk_builder_get_widget(builder,"Window");
    window->WorkZoneBox = yon_gtk_builder_get_widget(builder,"WorkZoneBox");
    // window->AcceptButton = yon_gtk_builder_get_widget(builder,"AcceptButton");
    // window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    g_signal_connect(G_OBJECT(window->window),"delete-event",G_CALLBACK(yon_on_settings_window_accept),window);
    return window;
}

struct yon_configuration_custom_parameter {
    GtkWidget *custom_parameter_root;
    char *custom_parameter_id;
    struct yon_configuration_window_custom_parameter *custom_callbacks;
};

dictionary *__yon_configuration_custom_parameters = NULL;

int __yon_configuration_window_width = -1;
int __yon_configuration_window_height = -1;

void __yon_on_ubl_settings_window_open(GtkWidget *self){
    if (!__yon_configuration_custom_parameters) return;
    ubl_settings_window *window = yon_ubl_settings_window_new();
    char *icon_name = yon_char_unite("com.ublinux.",template_app_information.app_tech_name,NULL);
    char *title = NULL;
    if (!yon_char_is_empty(__yon_configuration_window_custom_name)){
        title = __yon_configuration_window_custom_name;
    } else {
        title = SETTINGS_TITLE_LABEL;
    }
    yon_gtk_window_setup(GTK_WINDOW(window->window),GTK_WINDOW(gtk_widget_get_toplevel(self)),title,icon_name,"SETTINGS_WINDOW");
        gtk_widget_set_size_request(window->window,__yon_configuration_window_width,__yon_configuration_window_height);
    dictionary *current = NULL;
    for_dictionaries(current,__yon_configuration_custom_parameters){
        struct yon_configuration_custom_parameter *parameter = ((struct yon_configuration_custom_parameter*)current->data);
        parameter->custom_parameter_root = parameter->custom_callbacks->custom_parameter_create_func(parameter->custom_parameter_id);
        gtk_box_pack_start(GTK_BOX(window->WorkZoneBox),parameter->custom_parameter_root,0,0,0);
        parameter->custom_callbacks->custom_parameter_update_func(parameter->custom_parameter_root);
    }
    gtk_widget_show(window->window);
}

gboolean yon_on_settings_window_accept(GtkWidget*,GdkEvent *,ubl_settings_window *window){
    dictionary *current;
    for_dictionaries(current,__yon_configuration_custom_parameters){
        struct yon_configuration_custom_parameter *parameter = ((struct yon_configuration_custom_parameter*)current->data);
        parameter->custom_callbacks->custom_parameter_save_value_func(parameter->custom_parameter_root);
    }
    return 0;
}

void yon_configuration_window_add_custom_parameter(const char *id, struct yon_configuration_window_custom_parameter *custom_parameter_data){
    struct yon_configuration_custom_parameter *parameter = malloc(sizeof(struct yon_configuration_custom_parameter));
    parameter->custom_callbacks = custom_parameter_data;
    parameter->custom_parameter_id = yon_char_new(id);
    yon_dictionary_add_or_create_if_exists_with_data(__yon_configuration_custom_parameters,(char*)id,parameter);
}

void yon_configuration_window_set_custom_name(const char *name){
    __yon_configuration_window_custom_name = yon_char_new(name);
}

void yon_configuration_window_set_size(int width, int height){
    __yon_configuration_window_width = width;
    __yon_configuration_window_height = height;
}

#define date_format_parameter "DateFormat"

void on_parameter_destroy(GtkWidget *, struct yon_date_chooser_option *parameter);
void on_parameter_destroy(GtkWidget *, struct yon_date_chooser_option *parameter){
    free(parameter);
}

GtkWidget *yon_date_chooser_new( char *){
    struct yon_date_chooser_option *parameter = malloc(sizeof(struct yon_date_chooser_option));

    parameter->RootBox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5);
    parameter->DateLabel = gtk_label_new(DATE_FORMAT_SETTINGS_LABEL);
    parameter->DateCombo = gtk_combo_box_text_new();
	gtk_box_pack_start(GTK_BOX(parameter->RootBox),parameter->DateLabel,0,0,0);
	gtk_box_pack_start(GTK_BOX(parameter->RootBox),parameter->DateCombo,1,1,0);
    g_object_set_data(G_OBJECT(parameter->RootBox),"data_struct",parameter);
	
    g_signal_connect(G_OBJECT(parameter->RootBox),"destroy",G_CALLBACK(on_parameter_destroy),parameter);
	gtk_widget_show_all(parameter->RootBox);
    return parameter->RootBox;
}

void yon_date_chooser_update(GtkWidget *root){
    struct yon_date_chooser_option *parameter = g_object_get_data(G_OBJECT(root),"data_struct");
	gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(parameter->DateCombo));
	gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(parameter->DateCombo),"%d.%m.%Y",DMY_FORMAT_LABEL);
	gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(parameter->DateCombo),"%d-%m-%Y",DMY_MINUS_FORMAT_LABEL);
	gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(parameter->DateCombo),"%d/%m/%Y",DMY_SLASH_FORMAT_LABEL);
	gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(parameter->DateCombo),"%m-%d-%Y",MDY_FORMAT_LABEL);

    char *target;
    if (!yon_window_config_get_parameter(yon_configuration_window_section,date_format_parameter,&target,YON_TYPE_STRING)){
        target = NULL;
    }
    if (target){
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(parameter->DateCombo),target);
    } else {
		gtk_combo_box_set_active(GTK_COMBO_BOX(parameter->DateCombo),0);
	}
    
}

void yon_date_chooser_save(GtkWidget *root){
    struct yon_date_chooser_option *parameter = g_object_get_data(G_OBJECT(root),"data_struct");
    char *path = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(parameter->DateCombo));
    if (!yon_char_is_empty(path)){
        yon_window_config_add_instant_parameter(date_format_parameter,yon_configuration_window_section,path,YON_TYPE_STRING);
    } else {
        yon_window_config_erase_instant_parameter(date_format_parameter,yon_configuration_window_section);
    }
}