#include "ubl-settings-keyboard.h"

void on_font_accept(GtkWidget *, layouts_window *window){
    GtkWidget *output_widget = g_object_get_data(G_OBJECT(window->Window),"output");
    if (gtk_switch_get_active(GTK_SWITCH(window->OptionsFilterCombo))){
        gtk_entry_set_text(GTK_ENTRY(output_widget),yon_char_get_localised_from_lib(DEFAULT_LABEL));
        yon_config_remove_by_key(console_keymap_parameter);
        on_subwindow_close(window->Window);
        return;
    }
    char *target = (char*)gtk_entry_get_text(GTK_ENTRY(window->OptionsEntry));
    if (yon_char_is_empty(target)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),yon_char_get_localised_from_lib(NOTHING_CHOSEN_LABEL),5,BACKGROUND_IMAGE_FAIL_TYPE);
        return;
    }
    gtk_entry_set_text(GTK_ENTRY(output_widget),target);
    yon_config_register(console_font_parameter,console_font_parameter_command,target);
    on_subwindow_close(window->Window);
}

void on_font_selection_changed(GtkWidget *,layouts_window *window){
    GtkTreeModel *model;
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(window->MainTree)),&model, &iter)){
        char *target;
        gtk_tree_model_get(model,&iter,0,&target,-1);
        gtk_entry_set_text(GTK_ENTRY(window->OptionsEntry),target);
    } else {
        gtk_entry_set_text(GTK_ENTRY(window->OptionsEntry),"");
    }
}

layouts_window *yon_font_window_new(){
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_font_path);
    layouts_window *window = malloc(sizeof(layouts_window));
    memset(window,0,sizeof(layouts_window));
    window->Window = yon_gtk_builder_get_widget(builder,"Window");
    window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    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,"FontTree");
    window->OptionsEntry = yon_gtk_builder_get_widget(builder,"ManualEntry");
    window->OptionsFilterCombo = yon_gtk_builder_get_widget(builder,"DefaultSwitch");
    window->OptionsList = GTK_LIST_STORE(gtk_builder_get_object(builder,"FontList"));
    window->parameters = yon_char_new(config(keyboard_layout_parameter));

    g_signal_connect(G_OBJECT(window->OptionsFilterCombo),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch_inversed),window->MainTree);
    g_signal_connect(G_OBJECT(window->OptionsFilterCombo),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch_inversed),window->OptionsEntry);
    g_signal_connect(G_OBJECT(window->MainTree),"cursor-changed",G_CALLBACK(on_font_selection_changed),window);
    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    int font_size;
    int size;
    config_str fonts = yon_resource_open_file(font_list_path, &font_size);
    for (int i=0;i<font_size;i++){
        yon_char_remove_last_symbol(fonts[i],'\n');
    }
    GtkTreeIter iter;
    config_str parameters = yon_config_load(tty_font_command,&size);
    char *config_value = config(console_font_parameter);
    if (yon_char_is_empty(config_value)){
        gtk_switch_set_active(GTK_SWITCH(window->OptionsFilterCombo),0);
    }
    for (int i=0;i<size;i++){
        yon_char_remove_last_symbol(parameters[i],'\n');
        char *found = yon_char_parsed_check_exist_begins_with(fonts,font_size,parameters[i]);
        if (found){
            int parsed_size;
            config_str parsed = yon_char_parse(found,&parsed_size,";");
            gtk_list_store_append(GTK_LIST_STORE(window->OptionsList),&iter);
            gtk_list_store_set(GTK_LIST_STORE(window->OptionsList),&iter,0,parsed[0],1,parsed[1],-1);
            if (!yon_char_is_empty(config_value)&&!strcmp(parsed[0],config_value)){
                gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(window->MainTree)),&iter);
                gtk_entry_set_text(GTK_ENTRY(window->OptionsEntry),config_value);
            }
        }
    }
    if (yon_char_is_empty(config_value)){
        gtk_switch_set_active(GTK_SWITCH(window->OptionsFilterCombo),1);
    }
    return window;
}

void yon_font_open(GtkWidget *,main_window *widgets){
    layouts_window *window = yon_font_window_new();
    g_object_set_data(G_OBJECT(window->Window),"output",widgets->TTYFontEntry);
    yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),FONT_TITLE_LABEL,icon_path,"font-window");
        
    g_object_set_data(G_OBJECT(window->Window),"widgets",widgets);
    g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(on_font_accept),window);
}