#include "ubinstall-gtk.h"

void configuration_mode_accept(GtkWidget *,configuration_window *window){
    const char *path = gtk_entry_get_text(GTK_ENTRY(window->PathEntry));
    if (yon_char_is_empty(path)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),_EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->PathEntry);
        return;
    }
            if (!yon_char_is_empty(main_config.config_save_path)) free(main_config.config_save_path);
            main_config.config_save_path = yon_char_new(path);
    gtk_widget_destroy(window->Window);
}

void on_path_choose(GtkWidget *,configuration_window *window){
    filechooser_window *dialog = yon_file_chooser_window_new(GTK_FILE_CHOOSER_ACTION_SAVE);
        yon_gtk_window_setup(GTK_WINDOW(dialog->Window),GTK_WINDOW(window->Window),CONFIGURATION_MODE_TITLE_LABEL,icon_path,"filechooser_window");
    GtkFileFilter *filter = gtk_file_filter_new();
    gtk_file_filter_add_pattern(filter,"*.ini");
    gtk_file_filter_set_name(filter,"*.ini");
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog->MainFileChooser),filter);
    if (yon_file_chooser_start(dialog)==GTK_RESPONSE_APPLY){
        gtk_entry_set_text(GTK_ENTRY(window->PathEntry),dialog->last_success_selection);
        free(dialog);
    }
}

void on_configuration_exit(GtkWidget *,configuration_window *window){
    main_window *widgets = g_object_get_data(G_OBJECT(window->Window),"widgets");
    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widgets->ConfigurationModeMenuItem),0);
    gtk_widget_destroy(window->Window);
}

void on_configuration_mode_switch(GtkWidget *self,main_window *widgets){
    if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(self))){
        GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_configuration_mode);
        configuration_window *window = malloc(sizeof(configuration_window));
        window->Window = yon_gtk_builder_get_widget(builder,"MainWindow");
        window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
        window->PathEntry = yon_gtk_builder_get_widget(builder,"PathEntry");
        window->PathButton = yon_gtk_builder_get_widget(builder,"PathButton");
        window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
        window->ChooseButton = yon_gtk_builder_get_widget(builder,"ChooseButton");
        
        g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_configuration_exit),window);
        g_signal_connect(G_OBJECT(window->Window),"destroy",G_CALLBACK(gtk_main_quit),NULL);
        g_signal_connect(G_OBJECT(window->ChooseButton),"clicked",G_CALLBACK(configuration_mode_accept),window);
        g_signal_connect(G_OBJECT(window->PathButton),"clicked",G_CALLBACK(on_path_choose),window);
        g_object_set_data(G_OBJECT(window->Window),"widgets",widgets);
        yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->MainWindow),CONFIGURATION_MODE_TITLE_LABEL,icon_path,"configuration_window");

        if (!yon_char_is_empty(main_config.config_save_path)) gtk_entry_set_text(GTK_ENTRY(window->PathEntry),main_config.config_save_path);
        gtk_widget_show(window->Window);
        gtk_main();

        yon_ubl_status_box_spawn_infinite(GTK_CONTAINER(widgets->StatusBox),"config_mode",CONFIGURATION_MODE_STATUS_LABEL,BACKGROUND_IMAGE_INFO_TYPE);
        yon_ubl_status_box_spawn_infinite(GTK_CONTAINER(widgets->StatusBox2),"config_mede",CONFIG_PATH_LABEL(main_config.config_save_path),BACKGROUND_IMAGE_INFO_TYPE);
        GList *revealerlist = gtk_container_get_children(GTK_CONTAINER(widgets->StatusBox2));
        GList *list = gtk_container_get_children(GTK_CONTAINER(g_list_nth_data(revealerlist,0)));
        GtkWidget *box = GTK_WIDGET(list->data);

        g_list_free(revealerlist);
        g_list_free(list);

        gtk_style_context_remove_class(gtk_widget_get_style_context(box),"boxInfoMessOK");
        gtk_style_context_add_class(gtk_widget_get_style_context(box),"boxInfoMessGray");
        main_config.configure_mode = 1;
        gtk_widget_destroy(window->Window);
        return;

    } else {
        yon_ubl_status_box_despawn_infinite(GTK_CONTAINER(widgets->StatusBox));
        GList *revealerlist = gtk_container_get_children(GTK_CONTAINER(widgets->StatusBox2));
        gtk_widget_destroy(GTK_WIDGET(g_list_nth_data(revealerlist,0)));
        g_list_free(revealerlist);
        main_config.configure_mode = 0;

    }
}
