#include "ubl-settings-keyboard.h"

void on_option_chosen(GtkCellRenderer *, gchar *path, layouts_window *window){
    GtkTreeIter iter;
    GtkTreeIter itar;
    gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(window->OptionsFilter),&iter,path);
    gtk_tree_model_filter_convert_iter_to_child_iter(GTK_TREE_MODEL_FILTER(window->OptionsFilter),&itar,&iter);
    int chosen;
    char *target;
    gtk_tree_model_get(GTK_TREE_MODEL(window->OptionsList),&itar,0,&target,2,&chosen,-1);
    gtk_list_store_set(GTK_LIST_STORE(window->OptionsList),&itar,2,!chosen,-1);

    char *options_string = yon_char_new(gtk_entry_get_text(GTK_ENTRY(window->OptionsEntry)));
    if (!chosen){
        char *temp = yon_char_append_element(options_string,target,",");
        gtk_entry_set_text(GTK_ENTRY(window->OptionsEntry),temp);
        free(temp);
    } else {
        char *temp = yon_char_remove_element(options_string,target,",");
        gtk_entry_set_text(GTK_ENTRY(window->OptionsEntry),temp);
        free(temp);
    }
}

gboolean yon_filter_refilter(GtkTreeModel *model, GtkTreeIter *iter, layouts_window *window){
    char *target_id;
    const char *id = gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->OptionsFilterCombo));
    if (yon_char_is_empty(id)) return 0;
    gtk_tree_model_get(model,iter,0,&target_id,-1);
    char *filter_section = yon_char_divide_search(target_id,":",-1);
    int result = !strcmp(filter_section,id);
    if (target_id)
        free(target_id);
    if (filter_section)
        free(filter_section);
    return result;
}

void yon_options_setup(layouts_window *window){
    int size;
    config_str option_commands = yon_char_parsed_new(&size,
    lv2_options_command,
    lv3_options_command,
    ctrl_options_command,
    grp_led_options_command,
    mod_led_options_command,
    keypad_options_command,
    kpdl_options_command,
    caps_options_command,
    altwin_options_command,
    Compose_options_command,
    compat_options_command,
    currencysign_options_command,
    lv5_options_command,
    nbsp_options_command,
    japan_options_command,
    korean_options_command,
    esperanto_options_command,
    solaris_options_command,
    terminate_options_command,
    NULL);

    char *command_full = yon_char_parsed_to_string(option_commands,size,"; ");

    int options_size;
    config_str options = yon_config_load(command_full,&options_size);
    GtkTreeIter iter;

    int filter_size;
    config_str filter = NULL;
    
    char *config_options = config(options_parameter);

    for (int i=0;i<options_size;i++){
        yon_char_remove_last_symbol(options[i],'\n');
        int parsed_size;
        config_str parsed = yon_char_parse(options[i],&parsed_size,"|");
        gtk_list_store_append(window->OptionsList,&iter);
        gtk_list_store_set(window->OptionsList,&iter,0,parsed[0],1,_(parsed[1]),2,yon_char_check_element(config_options,parsed[0],","),-1);
        char *str = yon_char_new(parsed[0]);
        char *type = yon_char_divide_search(str,":",-1);
        if (yon_char_parsed_check_exist(filter,filter_size,type)==-1){
            yon_char_parsed_add_or_create_if_exists(filter,&filter_size,type);
            gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(window->OptionsFilterCombo),type,type);
        }
        if (!yon_char_is_empty(str))free(str);
        if (!yon_char_is_empty(type))free(type);
        yon_char_parsed_free(parsed,parsed_size);
    }
    gtk_combo_box_set_active(GTK_COMBO_BOX(window->OptionsFilterCombo),0);
    yon_char_parsed_free(filter,filter_size);
    yon_char_parsed_free(option_commands,size);
}

void on_options_filter_changed(GtkWidget *, layouts_window *window){
    gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(window->OptionsFilter));
}

void on_options_accept(GtkWidget *self, layouts_window *window){
    main_window *widgets = g_object_get_data(G_OBJECT(window->Window),"widgets");
    char *options = (char*)gtk_entry_get_text(GTK_ENTRY(window->OptionsEntry));
    if (!yon_char_is_empty(options)){
        yon_config_register(options_parameter,options_parameter_command,options);
    } else {
        yon_config_remove_by_key(options_parameter);
    }
    on_subwindow_close(self);
    yon_interface_update(widgets);
}

void on_options_save(GtkWidget *,main_window *widgets){
    char *main_id = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(widgets->LayoutChangingCombo));
    int parameter_parsed_size;
    config_str parameter_parsed = NULL;
    yon_char_parsed_add_or_create_if_exists(parameter_parsed,&parameter_parsed_size,!yon_char_is_empty(main_id)?main_id:"");
    GtkTreeIter iter;
    for_iter(GTK_TREE_MODEL(widgets->OptionsList),&iter){
        char *cur_id;
        gtk_tree_model_get(GTK_TREE_MODEL(widgets->OptionsList),&iter,0,&cur_id,-1);
        yon_char_parsed_add_or_create_if_exists(parameter_parsed,&parameter_parsed_size,cur_id);
    }
    char *final_parameter = yon_char_parsed_to_string_full(parameter_parsed,parameter_parsed_size,",");
    yon_char_parsed_free(parameter_parsed,parameter_parsed_size);
    if (!yon_char_is_empty(final_parameter)){
        yon_config_register(options_parameter,options_parameter_command,final_parameter);
        free(final_parameter);
    } else {
        yon_config_remove_by_key(options_parameter);
    }
}

layouts_window *yon_options_window_new(){
    
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_options_path);
    layouts_window *window = malloc(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->MainTree = yon_gtk_builder_get_widget(builder,"OptionsTree");
    window->OptionsFilterCombo = yon_gtk_builder_get_widget(builder,"OptionsFilterCombo");
    window->OptionsEntry = yon_gtk_builder_get_widget(builder,"OptionsEntry");
    window->OptionsList = GTK_LIST_STORE(gtk_builder_get_object(builder,"OptionsList"));
    window->OptionsFilter = GTK_TREE_MODEL(gtk_builder_get_object(builder,"OptionsFilter"));
    window->ChosenCell = GTK_CELL_RENDERER(gtk_builder_get_object(builder,"ChosenCell"));

    g_signal_connect(G_OBJECT(window->OptionsFilterCombo),"changed",G_CALLBACK(on_options_filter_changed),window);
    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_option_chosen),window);
    gtk_tree_view_set_model(GTK_TREE_VIEW(window->MainTree),GTK_TREE_MODEL(window->OptionsFilter));
    gtk_tree_model_filter_set_visible_func(GTK_TREE_MODEL_FILTER(window->OptionsFilter),(GtkTreeModelFilterVisibleFunc)yon_filter_refilter,window,NULL);
    yon_options_setup(window);
    char *parameter_options = config(options_parameter);
    gtk_entry_set_text(GTK_ENTRY(window->OptionsEntry),parameter_options);
    return window;
}

void on_options_open(GtkWidget *, main_window *widgets){
    layouts_window *window = yon_options_window_new();
    g_object_set_data(G_OBJECT(window->Window),"widgets",widgets);
    g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(on_options_accept),window);
}


void on_option_remove(GtkWidget*, main_window *widgets){
    GtkTreeIter iter;
    GtkTreeModel *model;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->OptionsTree)),&model, &iter)){
        dialog_confirmation_data *data = yon_confirmation_dialog_data_new();
        data->action_text = OPTION_REMOVE_LABEL;
        data->title = OPTION_REMOVE_TITLE_LABEL;
        if (yon_confirmation_dialog_call(widgets->Window,data)!=GTK_RESPONSE_ACCEPT) return;
        char *target;
        gtk_tree_model_get(model,&iter,0,&target,-1);
        yon_config_remove_element(options_parameter,target,",");
    }
    yon_interface_update(widgets);
}