#include "libublsettings-gtk3.h"


#ifdef __GTK_H__

// GtkEntry section
// struct entry_pattern_data {
//     GtkEntry *entry;
//     char *pattern; // %d %s
// };

// char *yon_gtk_entry_pattern_format(GtkWidget *target, struct entry_pattern_data *data){
//     if (target){
//         const char *entry_data = gtk_entry_get_text(target);
//         int next_track=0;
//         for (int i=0; i<strlen(data->pattern);i++){
//             char target_letter=-1;
//             target_letter = ;
//             switch (data->pattern[i]){
//                 case '%':next_track=1;
//                     break;
//                 case 'd':if (next_track){
                        
//                     }
//                     break;
//                 case 's':
//                     break;
//                 default:

//             }
//         }

//     };
// }

// void yon_gtk_entry_set_pattern(GtkEntry *entry, char *pattern){
//     struct entry_pattern_data *data = malloc(sizeof(struct entry_pattern_data));
//     data->entry=entry;
//     data->pattern=pattern;
//     char *current_text = gtk_entry_get_text(entry);

// }


config_str yon_resource_open_file(const char *path, int *size){
    config_str parsed = NULL;
    char *modules = NULL;
    gsize sz=0;
    GFile *file = g_file_new_for_uri(path);
    g_file_load_contents(file,NULL,&modules,&sz,NULL,NULL);
    g_object_unref(G_OBJECT(file));
    parsed = yon_char_parse(modules,size,"\n");
    return parsed;
}


struct yon_combo_default_struct {
    char *command;
    GtkComboBoxText *target;
    void *result_callback; // char*(*)(char*)
    char *default_value;
    char *command_value;
};

void _yon_combo_box_set_default_value(struct yon_combo_default_struct *target){
    gtk_combo_box_text_prepend(target->target," ",target->default_value);
    if (!yon_char_is_empty(target->default_value)) free(target->command);
    if (!yon_char_is_empty(target->default_value)) free(target->command_value);
}

void *__yon_combo_set_default(struct yon_combo_default_struct *target){
    int size;
    config_str parameter = yon_config_load(yon_config_parameter_prepare_command(target->command,"default",NULL,NULL),&size);
    if (size&&!yon_char_is_empty(parameter[0])&&strcmp(parameter[0],"(null)\n")){
        yon_char_remove_last_symbol(parameter[0],'\n');
        target->command_value = yon_char_new(parameter[0]);
        target->default_value = ((char*(*)(char*))(target->result_callback))(parameter[0]);
    } else {
        target->default_value = ((char*(*)(char*))(target->result_callback))(NULL);
    }
        g_idle_add_once((GSourceOnceFunc)_yon_combo_box_set_default_value,target);
    
}

void yon_combo_box_set_default(GtkComboBoxText *target, char *command, char*(result_callback)(char*)){
    struct yon_combo_default_struct *target_struct = malloc(sizeof(struct yon_combo_default_struct));
    target_struct->result_callback = result_callback;
    target_struct->target = target;
    target_struct->command = yon_char_new(command);
    target_struct->command_value=NULL;
    target_struct->default_value = NULL;
    g_thread_new("combo_get_default",(GThreadFunc)__yon_combo_set_default,target_struct);
}

void on_widget_changed(GObject* target,  GdkEvent*,  gpointer user_data){
    char *config_parameter = g_object_get_data(target,"config_parameter");
    char *get_command = g_object_get_data(target,"get_command");
    char *object_property = g_object_get_data(target,"object_property");
    GValue value = G_VALUE_INIT;
    g_object_get_property(target,object_property,&value);
    char *value_str = g_strdup_value_contents(&value);
    if (!yon_char_is_empty(value_str)||strcmp(value_str,"[invalid GValue]")){
        yon_config_register(config_parameter,get_command,value_str);
    }
    if (!yon_char_is_empty(value_str)) free(value_str);
}

void yon_gtk_widget_register_parameter(GtkWidget *target, char *config_parameter, char *get_command, char *object_property){
    g_return_if_fail(G_IS_OBJECT(target));
    char *signal_name = yon_char_unite("notify::",object_property,NULL);
    g_object_set_data(G_OBJECT(target),"config_parameter",yon_char_new(config_parameter));
    g_object_set_data(G_OBJECT(target),"get_command",yon_char_new(get_command));
    g_object_set_data(G_OBJECT(target),"object_property",yon_char_new(object_property));
    g_signal_connect(G_OBJECT(target),signal_name,G_CALLBACK(on_widget_changed),NULL);
}

dictionary *__register_widgets = NULL;

void yon_gtk_widget_register_parameter_manual(GtkWidget *target, char *config_parameter, char *get_command, char *object_property){
    yon_dictionary_add_or_create_if_exists_with_data(__register_widgets,NULL,target);
    g_object_set_data(G_OBJECT(target),"config_parameter",yon_char_new(config_parameter));
    g_object_set_data(G_OBJECT(target),"get_command",yon_char_new(get_command));
    g_object_set_data(G_OBJECT(target),"object_property",yon_char_new(object_property));
}

void yon_gtk_widget_register_parameter_activate(){
    dictionary *cur;
    for_dictionaries(cur,__register_widgets){
        on_widget_changed(yon_dictionary_get_data(cur,GObject*),NULL,NULL);
    }
}

char *yon_size_get_name_from_letter(char letter){
    switch (letter){
        case 'K': return yon_char_new(KB_LABEL); break;
        case 'M': return yon_char_new(MB_LABEL); break;
        case 'G': return yon_char_new(GB_LABEL); break;
        case 'T': return yon_char_new(TB_LABEL); break;
    }
    return NULL;
}

char *yon_size_long_convert_automatic_to_string(unsigned long bytes){
    char mod = '\0';
    double size_converted = yon_size_long_convert_automatic(bytes, &mod);
    char *size_converted_str = yon_char_from_double(size_converted);
    char *final = yon_char_unite(size_converted_str, " ", yon_size_get_name_from_letter(mod),NULL);
    return final;
}

char *yon_size_long_convert_automatic_to_string_preserve_negative(long bytes){
    char mod = '\0';
    gboolean negative = 0;
    if ((long)bytes<0){
        negative = 1;
        bytes=bytes*-1;
    }
    double size_converted = yon_size_long_convert_automatic(bytes, &mod);
    char *size_converted_str = yon_char_from_double(size_converted);
    char *final = yon_char_unite(negative?"-":"",size_converted_str, " ", yon_size_get_name_from_letter(mod),NULL);
    return final;
}

int yon_environment_check_wayland(){
    const char *session = getenv("XDG_SESSION_TYPE");
    const char *desktop = g_environ_getenv(g_get_environ(),"XDG_CURRENT_DESKTOP");
    if (!yon_char_is_empty(session)&&!strcmp(session,"wayland")&&!strcmp(desktop,"KDE")){
        return 0;
    }
    if (!yon_char_is_empty(session)&&!strcmp(session,"wayland")){
        if (setenv("GDK_BACKEND","x11",1)){
            return 1;
        }
    }
    return 0;
} 

gboolean yon_environment_get_env_for_launch(char **env){
    *env = NULL;
    const char *session = g_environ_getenv(g_get_environ(),"XDG_SESSION_TYPE");
    const char *desktop = g_environ_getenv(g_get_environ(),"XDG_CURRENT_DESKTOP");
    if (!yon_char_is_empty(session)&&!strcmp(session,"wayland")&&!strcmp(desktop,"KDE")){
        *env=NULL;
        return 0;
    } else if (!yon_char_is_empty(session)&&!strcmp(session,"wayland")){
        *env=yon_char_new("GDK_BACKEND=x11");
    }
    if (!GDK_IS_X11_DISPLAY(gdk_display_get_default())){
        return 0;
    }
    return 1;
}

// dictionary *__yon_config_listeners = NULL;
// typedef struct {
//     GtkWidget *target;
//     char *widget_parameter;
//     void *check_function;
//     char *parameter;
//     char *save_command;
// } config_listener;

// void __yon_gtk_config_activated(GtkWidget *, config_listener *current){
//     char *parameter;

//     if (((int (*)(char*, GtkWidget*, char*))(current->check_function))(parameter,current->target, current->parameter)) {
//     }

// }

// int yon_gtk_config_add_listener (GtkWidget *target, char *parameter, char *save_command,int(check_function)(char*,GtkWidget*,char*)){
//     if (__yon_config_listeners){
//         dictionary *dict;
//         for_dictionaries(dict,__yon_config_listeners){
//             if (target==((config_listener*)dict->data)->target){
//                 return 0;
//             }
//         }
//     }
//     config_listener *current = malloc(sizeof(config_listener));
//     current->target=target;
//     current->parameter = parameter;
//     current->save_command = save_command;
//     current->check_function = (void*)check_function;
//     if (GTK_IS_TOGGLE_BUTTON(target)){
//         g_signal_connect(G_OBJECT(target),"toggled",C_CALLBACK(__yon_gtk_config_activated),current);

//     }

//     yon_dictionary_add_or_create_if_exists_with_data(__yon_config_listeners,NULL,current);
//     return 0;
// }

#endif