#include "ubinstall-gtk.h"

int yon_keyboard_save(main_window *widgets){
    if (gtk_switch_get_active(GTK_SWITCH(widgets->KeyboardDefaultLayoutsSwitch))){
        yon_config_remove_by_key(xkblayout_parameter);
    } else {
        GtkTreeIter iter;
        if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(widgets->LayoutsList),&iter)){
            yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),yon_char_get_localised_from_lib(_EMPTY_IMPORTANT_LABEL),5,BACKGROUND_IMAGE_FAIL_TYPE);
            yon_ubl_status_highlight_incorrect(widgets->KeyboardLayoutTree);
            return 0;
        }
        char *final = NULL;
        for_iter(widgets->LayoutsList, &iter){
            char *target;
            char *code;
            gtk_tree_model_get(GTK_TREE_MODEL(widgets->LayoutsList),&iter,0,&target,1,&code,-1);
            char *temp = NULL;
            if (!yon_char_is_empty(code)){
                char *tmp = yon_char_unite(target,"_",code,NULL);
                temp = yon_char_append_element(final,tmp,",");
            } else {
                temp = yon_char_append_element(final,target,",");
            }
            if (!yon_char_is_empty(final)) free(final);
            final = temp;
        }
        yon_config_register(xkblayout_parameter,xkblayout_parameter_command,final);
        if(!yon_char_is_empty(final)) free(final);
    }
    const char *keyboard_model = gtk_combo_box_get_active_id(GTK_COMBO_BOX(widgets->KeyboardModelCombo));
    if (!yon_char_is_empty(keyboard_model)){
        yon_config_register(xkbmodel_parameter,xkbmodel_parameter_command,(char*)keyboard_model);
    } else {
        yon_config_remove_by_key(xkbmodel_parameter);
    }
    int numlock_active = gtk_combo_box_get_active(GTK_COMBO_BOX(widgets->KeyboardNumLockCombo));
    char *numlock = NULL;
    switch(numlock_active){
        case 1: numlock = "on";
            break;
        case 2: numlock = "off";
            break;
    }
    if (!yon_char_is_empty(numlock)){
        yon_config_register(num_lock_boot_parameter,num_lock_boot_parameter_command,numlock);
    } else {
        yon_config_remove_by_key(num_lock_boot_parameter);
    }
    if (!main_config.configure_mode){
        yon_debug_output("%s\n",yon_config_save_args_simple(main_config.config_load_path,xkblayout_parameter,xkbmodel_parameter,num_lock_boot_parameter,NULL));
    }
    on_options_save(NULL,widgets);
    return 1;
}

void yon_keyboard_init(main_window *widgets){
    if (!main_config.configure_mode){
        yon_config_update_by_args(main_config.config_load_path, xkbmodel_parameter,num_lock_boot_parameter,NULL);
    }
    g_signal_handlers_block_by_func(G_OBJECT(widgets->KeyboardModelCombo),G_CALLBACK(on_model_changed),widgets);
    g_signal_handlers_block_by_func(G_OBJECT(widgets->KeyboardDefaultLayoutsSwitch),G_CALLBACK(on_layout_default_toggled),widgets);
    gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(widgets->KeyboardModelCombo));
    gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(widgets->KeyboardNumLockCombo));

    gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->KeyboardNumLockCombo),NULL,DEFAULT_LABEL);
    gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->KeyboardNumLockCombo),NULL,ON_LABEL);
    gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->KeyboardNumLockCombo),NULL,OFF_LABEL);
    int size;
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(widgets->KeyboardModelCombo),DEFAULT_LABEL);
    config_str models = yon_config_load(get_keyboard_models_command,&size);
    for (int i=0;i<size;i++){
        yon_char_remove_last_symbol(models[i],'\n');
        int parsed_size;
        config_str parsed = yon_char_parse(models[i],&parsed_size,"|");
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->KeyboardModelCombo),parsed[0],_(parsed[1]));
        yon_char_parsed_free(parsed,parsed_size);
    }
    yon_char_parsed_free(models,size);


    gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->KeyboardModelCombo),0);
    gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->KeyboardNumLockCombo),0);
    {
        char *kbmodel = yon_config_get_by_key(xkbmodel_parameter);
        if (kbmodel) {
            gtk_combo_box_set_active_id(GTK_COMBO_BOX(widgets->KeyboardModelCombo),kbmodel);
        } 
    }
    {
        gtk_list_store_clear(widgets->LayoutsList);
        char *layouts = config(xkblayout_parameter);

        int layouts_parsed_size;
        config_str layouts_parsed = yon_char_parse(layouts,&layouts_parsed_size,",");
        for(int i=0;i<layouts_parsed_size;i++){
            yon_layout_load(layouts_parsed[i],widgets);
        }
        if (yon_char_is_empty(layouts)||yon_config_check_ignore(xkblayout_parameter)){
            gtk_switch_set_active(GTK_SWITCH(widgets->KeyboardDefaultLayoutsSwitch),1);
        } else {
            gtk_switch_set_active(GTK_SWITCH(widgets->KeyboardDefaultLayoutsSwitch),0);
        }

        if (yon_char_is_empty(layouts)){
            char *element = yon_char_parsed_check_exist_begins_with(main_config.layouts_list,main_config.layouts_size,"us|");
            if (element){
                int parsed_size;
                config_str parsed = yon_char_parse(element,&parsed_size,"|");
                GtkTreeIter iter;
                gtk_list_store_append(widgets->LayoutsList,&iter);
                gtk_list_store_set(widgets->LayoutsList,&iter,0,parsed[0],2,_(parsed[1]),-1);
                yon_char_parsed_free(parsed,parsed_size);
            }
        }
    }
    {
        gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(widgets->KeyboardLayoutChangingCombo));
        gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(widgets->KeyboardLayoutChangingCombo),DEFAULT_LABEL);
        int size;
        gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->KeyboardLayoutChangingCombo),0);
        config_str options = yon_config_load(grp_options_command,&size);

        for (int i=0;i<size;i++){
            yon_char_remove_last_symbol(options[i],'\n');
            int parsed_size;
            config_str parsed = yon_char_parse(options[i],&parsed_size,"|");
            gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->KeyboardLayoutChangingCombo),parsed[0],_(parsed[1]));
            yon_char_parsed_free(parsed,parsed_size);
        }
        yon_char_parsed_free(options,size);
    }
    {
        char *name = yon_config_get_by_key(num_lock_boot_parameter);
        if (name) {
            if (!strcmp(name,"yes")||!strcmp(name,"yes")) gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->KeyboardNumLockCombo),1);
            else if (!strcmp(name,"no")||!strcmp(name,"off")) gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->KeyboardNumLockCombo),2);
        } else {
            gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->KeyboardNumLockCombo),0);
        }
    }
    {
        char *options = config(xkboptions_parameter);
        if (!yon_char_is_empty(options)){
            int size;
            config_str parsed = yon_char_parse(options,&size,",");
            if (!yon_char_is_empty(parsed[0])){
                gtk_combo_box_set_active_id(GTK_COMBO_BOX(widgets->KeyboardLayoutChangingCombo),parsed[0]);
            } else {
                gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->KeyboardLayoutChangingCombo),0);
            }
            if (yon_char_parsed_check_exist(parsed,size,"compose:rwin")>-1){
                gtk_switch_set_active(GTK_SWITCH(widgets->KeyboardComposeSwitch),1);
            } else {
                gtk_switch_set_active(GTK_SWITCH(widgets->KeyboardComposeSwitch),0);
            }
            if (yon_char_parsed_check_exist(parsed,size,"grp_led:scroll")>-1){
                gtk_switch_set_active(GTK_SWITCH(widgets->KeyboardScrollLockSwitch),1);
            } else {
                gtk_switch_set_active(GTK_SWITCH(widgets->KeyboardScrollLockSwitch),0);
            }
        }

    }

    g_signal_handlers_unblock_by_func(G_OBJECT(widgets->KeyboardModelCombo),G_CALLBACK(on_model_changed),widgets);
    g_signal_handlers_unblock_by_func(G_OBJECT(widgets->KeyboardDefaultLayoutsSwitch),G_CALLBACK(on_layout_default_toggled),widgets);
}

void on_layouts_accept(GtkWidget *self, layouts_window *window){
    main_window *widgets = g_object_get_data(G_OBJECT(window->Window),"widgets");
    gtk_list_store_clear(widgets->LayoutsList);
    GtkTreeIter iter;
    GtkTreeIter child_iter;
    for_iter (window->list,&iter){
        int active;
        char *target;
        gtk_tree_model_get(GTK_TREE_MODEL(window->list),&iter,0,&target,3,&active,-1);
        if (active){
            yon_layout_load(target,widgets);
        }
        if (gtk_tree_model_iter_children(GTK_TREE_MODEL(window->list),&child_iter,&iter)){
            for (int valid2=1;valid2;valid2=gtk_tree_model_iter_next(GTK_TREE_MODEL(window->list),&child_iter)){
                char *code;
                gtk_tree_model_get(GTK_TREE_MODEL(window->list),&child_iter,0,&target,1,&code,3,&active,-1);
                if (active){
                    char *full_code = yon_char_unite(target,"_",code,NULL);
                    yon_layout_load(full_code,widgets);
                    free(full_code);
                }
            }

        }
    }
    on_subwindow_close(self);
}

void on_layouts_chosen(GtkCellRenderer *, gchar *path, layouts_window *window){
    GtkTreeIter iter;
    gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(window->list),&iter,path);
    int chosen;
    char *layout, *variant;
    gtk_tree_model_get(GTK_TREE_MODEL(window->list),&iter,0,&layout,1,&variant,3,&chosen,-1);
    gtk_tree_store_set(GTK_TREE_STORE(window->list),&iter,3,!chosen,-1);


    char *full_layout = NULL;
    if (!yon_char_is_empty(variant)){
        full_layout = yon_char_unite(layout,"_",variant,NULL);
    } else {
        full_layout = layout;
    }
    if (!chosen){
        char *temp = yon_char_append_element(window->parameters,full_layout,",");
        free(window->parameters);
        window->parameters = temp;
    } else {
        char *temp = yon_char_remove_element(window->parameters,full_layout,",");
        free(window->parameters);
        window->parameters = temp;
    }
    free(full_layout);
}

void on_system_layouts_add(GtkWidget *, main_window *widgets){
    yon_layouts_add(GTK_TREE_VIEW(widgets->KeyboardLayoutTree),widgets->LayoutsList,widgets);
}

gboolean on_keyboard_search_func(GtkTreeModel *model, gint ,const char *key, GtkTreeIter *iter, main_window *){
    char *target;
    char *loc_target;
    char *code;
    gtk_tree_model_get(model, iter, 0,&code, 4,&target, 2, &loc_target,-1);
    char *string_utf = g_utf8_casefold(key,-1);
    char *target_utf = g_utf8_casefold(target,-1);
    char *loc_target_utf = g_utf8_casefold(loc_target,-1);

    char *string_normalized = g_utf8_strdown(g_utf8_normalize(string_utf,-1,G_NORMALIZE_DEFAULT),-1);
    char *target_normalized = g_utf8_strdown(g_utf8_normalize(target_utf,-1,G_NORMALIZE_DEFAULT),-1);
    char *loc_target_normalized = g_utf8_strdown(g_utf8_normalize(loc_target_utf,-1,G_NORMALIZE_DEFAULT),-1);

    return !(g_str_has_prefix(target_normalized,string_normalized) || g_str_has_prefix(loc_target_normalized,string_normalized) || !strcmp(code,string_normalized));
}

layouts_window *yon_layouts_window_new(){
    textdomain(LocaleName);
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_layouts_path);
    layouts_window *window = malloc(sizeof(layouts_window));
    memset(window,0,sizeof(layouts_window));
    window->Window = yon_gtk_builder_get_widget(builder,"Window");
    window->AcceptButton = yon_gtk_builder_get_widget(builder,"AcceptButton");
    window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    window->ChosenCell = GTK_CELL_RENDERER(gtk_builder_get_object(builder,"ChosenCell"));
    window->MainTree = yon_gtk_builder_get_widget(builder,"LayoutsTree");
    window->parameters = yon_char_new(config(xkblayout_parameter));

    gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(window->MainTree),(GtkTreeViewSearchEqualFunc)on_keyboard_search_func,window,NULL);
    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    g_signal_connect(G_OBJECT(window->ChosenCell),"toggled",G_CALLBACK(on_layouts_chosen),window);
    return window;
}

void yon_layouts_add(GtkTreeView *, GtkListStore *, main_window *widgets){
    layouts_window *window = yon_layouts_window_new();
    yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->MainWindow),KEYBOARD_LAYOUT_LABEL,icon_path,"layout_window");

    window->list = main_config.layouts_store;

    gtk_tree_view_set_model(GTK_TREE_VIEW(window->MainTree), GTK_TREE_MODEL(main_config.layouts_store));
    gtk_tree_view_column_clicked(GTK_TREE_VIEW_COLUMN(gtk_tree_view_get_column(GTK_TREE_VIEW(window->MainTree),0)));
    yon_layout_build(window);
    g_object_set_data(G_OBJECT(window->Window),"widgets",widgets);
    g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(on_layouts_accept),window);

    char *languages = config(xkblayout_parameter);
    int size;
    config_str parsed = yon_char_parse(languages,&size,",");
    GtkTreeIter iter,itar;
    for_iter(window->list,&iter){
        int found = 0;
        char *id, *id_var;
        gtk_tree_model_get(GTK_TREE_MODEL(window->list),&iter,0,&id,-1);
        for (int i=0;i<size;i++){
            char *key = NULL;
                key = yon_char_new(parsed[i]);
            if (!strcmp(id,key)){
                gtk_tree_store_set(GTK_TREE_STORE(window->list),&iter,3,1,-1);
                found=1;
                break;
            }
        }
        if (!found) {
            gtk_tree_store_set(GTK_TREE_STORE(window->list),&iter,3,0,-1);
        }
        if (gtk_tree_model_iter_children(GTK_TREE_MODEL(window->list),&itar,&iter)){
            for (int valid2=1;valid2;valid2 = gtk_tree_model_iter_next(GTK_TREE_MODEL(window->list),&itar)){
                gtk_tree_model_get(GTK_TREE_MODEL(window->list),&itar,0,&id,1,&id_var,-1);
                for (int i=0;i<size;i++){
                    char *key, *variant = NULL;
                    variant = yon_char_new(parsed[i]);
                    key = yon_char_divide_search(variant,"_",-1);
                    if (!strcmp(key,id)&&!yon_char_is_empty(id_var)&&!strcmp(variant,id_var)){
                        gtk_tree_store_set(GTK_TREE_STORE(window->list),&itar,3,1,-1);
                        found=1;
                        break;
                    }
                }
            }
        }

    }
}

void yon_layouts_remove(GtkTreeView *tree, GtkListStore *cur_list, main_window *widgets){
    GtkTreeModel *model = GTK_TREE_MODEL(cur_list);
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(tree),&model,&iter)){
        dialog_confirmation_data *data = yon_confirmation_dialog_data_new();
        data->action_text = LAYOUT_REMOVE_LABEL;
        data->title = LAYOUT_REMOVE_TITLE_LABEL;
        if (yon_confirmation_dialog_call(widgets->MainWindow,data)!=GTK_RESPONSE_ACCEPT) return;
        gtk_list_store_remove(widgets->LayoutsList,&iter);
    }
}

void on_system_layouts_remove(GtkWidget *, main_window *widgets){
    yon_layouts_remove(GTK_TREE_VIEW(widgets->KeyboardLayoutTree),widgets->LayoutsList,widgets);
}

// standard functions

config_str yon_layout_get_children(char *layout_id, int *size){
    (*size)=0;
    config_str final = NULL;
    for (int i=0;i<main_config.variants_size;i++){
        int parsed_size;
        config_str parsed = yon_char_parse(main_config.variants_list[i],&parsed_size,"|");
        if (!strcmp(parsed[0],layout_id)){
            yon_char_parsed_add_or_create_if_exists(final,size,main_config.variants_list[i]);
        }
    }
    return final;
}

void yon_layout_load(char *layout,main_window *widgets){
    GtkTreeIter iter;
    if (strstr(layout,"_")){
        char *cur_variant = yon_char_new(layout);
        char *cur_layout = yon_char_divide_search(cur_variant,"_",-1);
        for (int i=0;i<main_config.variants_size;i++){
            int parsed_size;
            config_str parsed = yon_char_parse(main_config.variants_list[i],&parsed_size,"|");
            if (!strcmp(parsed[0],cur_layout)&&!strcmp(parsed[1],cur_variant)){
                gtk_list_store_append(widgets->LayoutsList,&iter);
                gtk_list_store_set(widgets->LayoutsList,&iter,0,parsed[0],1,parsed[1],2,_(parsed[2]),-1);
            }
        }

    } else {
        for (int i=0;i<main_config.layouts_size;i++){
            int parsed_size;
            config_str parsed = yon_char_parse(main_config.layouts_list[i],&parsed_size,"|");
            if (!strcmp(parsed[0],layout)){
                gtk_list_store_append(widgets->LayoutsList,&iter);
                gtk_list_store_set(widgets->LayoutsList,&iter,0,parsed[0],2,_(parsed[1]),-1);
            }
        }
    }
}

void yon_layout_build(layouts_window *window){
    gtk_tree_store_clear(window->list);
    GtkTreeIter parent;
    GtkTreeIter iter;
    int size;
    config_str layouts = yon_config_load(layouts_command,&size);
    for (int i=0;i<size;i++){
        yon_char_remove_last_symbol(layouts[i],'\n');
        int parsed_size;
        config_str parsed = yon_char_parse(layouts[i],&parsed_size,"|");
        gtk_tree_store_append(window->list,&parent,NULL);
        gtk_tree_store_set(window->list,&parent,0,parsed[0],2,_(parsed[1]),4,parsed[1],-1);
        int variants_size;
        config_str variants = yon_layout_get_children(parsed[0],&variants_size);
        yon_char_parsed_free(parsed,parsed_size);
        for (int k=0;k<variants_size;k++){
            parsed = yon_char_parse(variants[k],&parsed_size,"|");
            gtk_tree_store_append(window->list,&iter,&parent);
            gtk_tree_store_set(window->list,&iter,0,parsed[0],1,parsed[1],2,_(parsed[2]),4,parsed[2],-1);
            yon_char_parsed_free(parsed,parsed_size);
        }
        yon_char_parsed_free(variants,variants_size);
        
    }
    yon_char_parsed_free(layouts,size);
    
}

void on_layout_move_up(GtkWidget *, main_window *widgets){
    GtkTreeModel *model;
    GtkTreeIter iter,*previter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->KeyboardLayoutTree)),&model,&iter)){
        previter = gtk_tree_iter_copy(&iter);
        if (!gtk_tree_model_iter_previous(model,previter)) return;
        char *a, *b;
        gtk_tree_model_get(model,&iter,0,&a,-1);
        gtk_tree_model_get(model,previter,0,&b,-1);
        char *layouts = config(xkblayout_parameter);
        char *temp = yon_char_swap_element(layouts,a,b,",");
        yon_config_register(xkblayout_parameter,xkblayout_parameter_command,temp);
        free(temp);
        gtk_list_store_swap(widgets->LayoutsList,&iter,previter);
        gtk_tree_iter_free(previter);
    }
}

void on_layout_move_down(GtkWidget *, main_window *widgets){
    GtkTreeModel *model;
    GtkTreeIter iter,*nextiter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->KeyboardLayoutTree)),&model,&iter)){
        nextiter = gtk_tree_iter_copy(&iter);
        if (!gtk_tree_model_iter_next(model,nextiter)) return;
        char *a, *b;
        gtk_tree_model_get(model,&iter,0,&a,-1);
        gtk_tree_model_get(model,nextiter,0,&b,-1);
        char *layouts = config(xkblayout_parameter);
        char *temp = yon_char_swap_element(layouts,a,b,",");
        yon_config_register(xkblayout_parameter,xkblayout_parameter_command,temp);
        free(temp);
        gtk_list_store_swap(widgets->LayoutsList,&iter,nextiter);
        gtk_tree_iter_free(nextiter);
    }
}

void on_layout_default_sensitive_toggled(GtkWidget *, int status, main_window *widgets){
    gtk_widget_set_sensitive(widgets->KeyboardLayoutTree,!status);
    gtk_widget_set_sensitive(widgets->KeyboardLayoutAddButton,!status);
    if (status){
        gtk_widget_set_sensitive(widgets->KeyboardLayoutRemoveButton,!status);
        gtk_widget_set_sensitive(widgets->KeyboardLayoutMoveUpButton,!status);
        gtk_widget_set_sensitive(widgets->KeyboardLayoutMoveDownButton,!status);
    }else {
        on_layout_selection_changed(GTK_TREE_VIEW(widgets->KeyboardLayoutTree),widgets);

    }
}

void on_layout_default_toggled(GtkWidget *, int status, main_window *widgets){
    if (!status){
        GtkTreeModel *model = GTK_TREE_MODEL(widgets->LayoutsList);
        GtkTreeIter iter;
        char *locales = NULL;
        for_iter(model,&iter){
            char *target, *variant;

            gtk_tree_model_get(model,&iter,0,&target,1,&variant,-1);
            char *key = NULL;
            if (!yon_char_is_empty(variant)){
                key = yon_char_unite(target,"_",variant,NULL);
            } else {
                key = yon_char_new(target);
            }
            char *temp = yon_char_append_element(locales?locales:"",key,",");
            if (!yon_char_is_empty(locales)) free(locales);
            locales = temp;
        }
        if (!yon_char_is_empty(locales)){
            yon_config_register(xkblayout_parameter,xkblayout_parameter_command,locales);
        }
    }
}

void on_layout_selection_changed(GtkTreeView *self,main_window *widgets){
    int status = gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(self)),NULL,NULL);
        gtk_widget_set_sensitive(widgets->KeyboardLayoutMoveDownButton,status);
        gtk_widget_set_sensitive(widgets->KeyboardLayoutMoveUpButton,status);
        gtk_widget_set_sensitive(widgets->KeyboardLayoutRemoveButton,status);

}

void on_model_changed(GtkComboBoxText *self, main_window *widgets){
    switch (gtk_combo_box_get_active(GTK_COMBO_BOX(self))){
        case 0:
            yon_config_remove_by_key(xkbmodel_parameter);
            break;
        default: 
                char *id = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(widgets->KeyboardModelCombo));
                yon_config_register(xkbmodel_parameter,xkbmodel_parameter_command,id);
            break;
    }
}

void on_options_save(GtkWidget *,main_window *widgets){
    char *main_id = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(widgets->KeyboardLayoutChangingCombo));
    char *options = yon_char_append_element(NULL,main_id,",");
    if (gtk_switch_get_active(GTK_SWITCH(widgets->KeyboardComposeSwitch))){
        char *temp = yon_char_append_element(options,"compose:rwin",",");
        if (!yon_char_is_empty(options)) free(options);
        options = temp;
    }
    if (gtk_switch_get_active(GTK_SWITCH(widgets->KeyboardScrollLockSwitch))){
        char *temp = yon_char_append_element(options,"grp_led:scroll",",");
        if (!yon_char_is_empty(options)) free(options);
        options = temp;

    }
    if (!yon_char_is_empty(options)){
        yon_config_register(xkboptions_parameter,xkboptions_parameter_command,options);
        free(options);
    } else {
        yon_config_remove_by_key(xkboptions_parameter);
    }
}