#include "libublsettingsui-gtk3.h"

void on_file_chooser_accept(GtkWidget *, filechooser_window *window){
    switch (gtk_file_chooser_get_action(GTK_FILE_CHOOSER(window->MainFileChooser))){
        case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
        case GTK_FILE_CHOOSER_ACTION_SAVE:
        if (yon_char_is_empty(window->last_success_selection)){
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),yon_char_get_localised_from_lib(NOTHING_CHOSEN_LABEL),5,BACKGROUND_IMAGE_FAIL_TYPE);
            return;
        }
            window->responce=GTK_RESPONSE_APPLY;
            break;

        case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
        case GTK_FILE_CHOOSER_ACTION_OPEN:
            window->responce = GTK_RESPONSE_ACCEPT;
            break;
    }
    gtk_widget_destroy(window->Window);
    gtk_main_quit();
}

void on_file_chooser_cancel(GtkWidget *, filechooser_window *window){
    window->responce=GTK_RESPONSE_CANCEL;
    gtk_widget_destroy(window->Window);
    gtk_main_quit();
    
}

void on_file_chooser_selected(GtkWidget *self, filechooser_window *window){
    if (window->last_any_selection) free(window->last_any_selection);
    if (window->last_success_selection) free(window->last_success_selection);

    GSList *list = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(window->MainFileChooser));
    config_str paths_all = NULL;
    config_str paths_success = NULL;
    int all_size=0;
    int success_size=0;
    GtkFileChooserAction action = gtk_file_chooser_get_action(GTK_FILE_CHOOSER(window->MainFileChooser));
    for (int i=0;i<g_slist_length(list);i++){
        char *data = (char*)g_slist_nth_data(list,i);
        if (action == GTK_FILE_CHOOSER_ACTION_SAVE){
            if (!access(data,F_OK)&&yon_file_is_directory(data)){
                char *temp = yon_char_unite(data,"/",gtk_file_chooser_get_current_name(GTK_FILE_CHOOSER(window->MainFileChooser)),NULL);
                data = temp;
            }
        }
        if (!data) continue;
        if ((action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER&&success_size==0) ||
        (action == GTK_FILE_CHOOSER_ACTION_OPEN&&gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->ChooseFolderCheck))&&yon_file_is_directory(data)) ||
        (action == GTK_FILE_CHOOSER_ACTION_OPEN&&!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->ChooseFolderCheck))&&!yon_file_is_directory(data)) ||
        (action == GTK_FILE_CHOOSER_ACTION_SAVE&&(!yon_file_is_directory(data)||access(data,F_OK))) ||
        (action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER&&yon_file_is_directory(data))
        ){
            // gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(window->MainFileChooser),g_path_get_basename(data));
            yon_char_parsed_add_or_create_if_exists(paths_success,&success_size,data);
        }
        
        yon_char_parsed_add_or_create_if_exists(paths_all,&all_size,data);
    }
    char *full_success = NULL;
    char *full_all = NULL;
    if (success_size){
        full_success = yon_char_parsed_to_string(paths_success,success_size,",");
    }
    if (all_size){
        full_all = yon_char_parsed_to_string(paths_all,all_size,",");
    }
    window->last_any_selection = yon_char_is_empty(full_all)?NULL:full_all;
    window->last_success_selection = yon_char_is_empty(full_success)?NULL:full_success;
    if (all_size) yon_char_parsed_free(paths_all,all_size);
    if (success_size) yon_char_parsed_free(paths_success,success_size);
}

filechooser_window *__yon_filechooser_window = NULL;

void yon_file_chooser_set_button_label(char *label){
    if (__yon_filechooser_window){
        gtk_button_set_label(GTK_BUTTON(__yon_filechooser_window->SaveButton),label);
    }
}

void on_file_chooser_exit(GtkWidget *, filechooser_window *){
    __yon_filechooser_window = NULL;
}

void on_file_chooser_check_root(GtkWidget *, filechooser_window *window){
    char *cur_path = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(window->MainFileChooser));
    if (!yon_char_is_empty(window->root)&&!strstr(cur_path,window->root)) gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(window->MainFileChooser),window->root);
}

void yon_file_chooser_set_root(filechooser_window *window, char *root_path){
    window->root = yon_char_new(root_path);
    g_signal_connect(G_OBJECT(window->MainFileChooser),"current-folder-changed",G_CALLBACK(on_file_chooser_check_root),window);
}

void yon_file_chooser_remove_root(filechooser_window *window){
    if (!yon_char_is_empty(window->root)) free(window->root);
    window->root=NULL;
    g_signal_handlers_disconnect_by_func(G_OBJECT(window->MainFileChooser),G_CALLBACK(on_file_chooser_check_root),window);

}

filechooser_window *yon_file_chooser_window_new(GtkFileChooserAction action){
    textdomain(template_ui_LocaleName);
    if (!__yon_filechooser_window){
        filechooser_window *window = malloc(sizeof(filechooser_window));
        __yon_filechooser_window = window;
        GtkBuilder *builder = gtk_builder_new_from_resource(ui_glade_path_filechooser);
        window->Window = yon_gtk_builder_get_widget(builder,"Window");
        window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
        window->HeaderTopic = yon_gtk_builder_get_widget(builder,"HeaderTopic");
        window->MainFileChooser = yon_gtk_builder_get_widget(builder,"MainFileChooser");
        window->SaveButton = yon_gtk_builder_get_widget(builder,"SaveButton");
        window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
        window->ChooseFolderCheck = yon_gtk_builder_get_widget(builder,"ChooseFolderCheck");
        window->last_any_selection=NULL;
        window->last_success_selection=NULL;
        window->root=NULL;
        g_signal_connect(G_OBJECT(window->ChooseFolderCheck),"toggled",G_CALLBACK(on_file_chooser_selected),window);
        g_signal_connect(G_OBJECT(window->MainFileChooser),"selection-changed",G_CALLBACK(on_file_chooser_selected),window);
        g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_file_chooser_cancel),window);
        g_signal_connect(G_OBJECT(window->SaveButton),"clicked",G_CALLBACK(on_file_chooser_accept),window);
        g_signal_connect(G_OBJECT(window->Window),"destroy",G_CALLBACK(on_file_chooser_exit),window);
        yon_window_config_custom_window_setup(GTK_WINDOW(window->Window),"file_chooser_window");        
        gtk_file_chooser_set_action(GTK_FILE_CHOOSER(window->MainFileChooser),action);
        switch(action){
            case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
                gtk_file_chooser_set_create_folders(GTK_FILE_CHOOSER(window->MainFileChooser),1);
                gtk_widget_hide(window->ChooseFolderCheck);
                gtk_button_set_label(GTK_BUTTON(window->SaveButton),yon_char_get_localised_from_lib(CREATE_FOLDER_LABEL));
                break;
            case GTK_FILE_CHOOSER_ACTION_OPEN:
                gtk_widget_hide(window->ChooseFolderCheck);
                gtk_button_set_label(GTK_BUTTON(window->SaveButton),yon_char_get_localised_from_lib(OPEN_LABEL));

                break;
            case GTK_FILE_CHOOSER_ACTION_SAVE:
                gtk_file_chooser_set_create_folders(GTK_FILE_CHOOSER(window->MainFileChooser),1);
                gtk_widget_hide(window->ChooseFolderCheck);
                gtk_button_set_label(GTK_BUTTON(window->SaveButton),yon_char_get_localised_from_lib(SAVE_LABEL));
                break;
            case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->ChooseFolderCheck),1);
                gtk_widget_hide(window->ChooseFolderCheck);
                gtk_button_set_label(GTK_BUTTON(window->SaveButton),yon_char_get_localised_from_lib(SELECT_FOLDER_LABEL));
                break;
        }

        g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
        textdomain(template_app_information.app_locale);
        return window;
    } else {
        textdomain(template_app_information.app_locale);
        return __yon_filechooser_window;
    }
}

GtkResponseType yon_file_chooser_start(filechooser_window *window){
    gtk_widget_show(window->Window);
    gtk_main();

    return window->responce;
}

void yon_file_chooser_remove_accept_function(filechooser_window *window){
    g_signal_handlers_disconnect_by_func(G_OBJECT(window->SaveButton),G_CALLBACK(on_file_chooser_accept),window);

}