#include "ubl-settings-repomanager.h"


// void on_remove_arch_selection_changed(GtkWidget *self, repo_add_window *window){
//     if (gtk_combo_box_get_active(GTK_COMBO_BOX(window->ArchitectureCombo))==-1){
//         gtk_widget_set_sensitive(window->AddPackageButton,0);
//     } else {
//         gtk_widget_set_sensitive(window->AddPackageButton,1);
//     }
// }

void on_remove_accept(GtkWidget *self, dictionary *dict){
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    repo_add_window *window = yon_dictionary_get_data(dict->first->next,repo_add_window*);
    const char *storage_path = gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->StorageCombo));
    const char *repo = (char*)gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(window->RepositoryCombo));
    const char *arch = (char*)gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(window->ArchitectureCombo));
    const char *sign = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->RepoSignCombo));

    if (yon_char_is_empty(storage_path)||yon_char_is_empty(repo)||yon_char_is_empty(arch)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->StorageCombo);
        return;
    }
    if (yon_char_is_empty(repo)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->RepositoryCombo);
        return;
    }
    if (yon_char_is_empty(arch)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->ArchitectureCombo);
        return;
    }

        GtkTreeIter iter;
        GtkTreeModel *model = GTK_TREE_MODEL(window->PackagesList);
        int size;
        config_str packages=NULL;

        for_iter(model,&iter){
            char *target;
            gtk_tree_model_get(model,&iter,2,&target,-1);
            char *filename = yon_char_divide(target,yon_char_find_last(target,'/'));
            yon_char_parsed_add_or_create_if_exists(packages,&size,target);
        }
        if (!size){
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),PACKAGES_REMOVE_ERROR_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            yon_ubl_status_highlight_incorrect(window->PackagesTree);
            return;
        }
            char *final = yon_char_parsed_to_string(packages,size," ");
            char *command = remove_packages_command(storage_path,arch,repo,final,sign);
            if (system(yon_debug_output("%s\n",command))){
                yon_ubl_status_box_render(PACKAGES_REMOVE_ERROR_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
            } else {
                yon_ubl_status_box_render(PACKAGES_REMOVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
            }
    gtk_widget_destroy(window->MainWindow);
    free(window);
    yon_interface_update(widgets);
}

void on_remove_add_package(GtkWidget *self, repo_add_window *window){
    if (gtk_combo_box_get_active(GTK_COMBO_BOX(window->ArchitectureCombo))>-1){
        filechooser_window *dialog = yon_file_chooser_window_new(GTK_FILE_CHOOSER_ACTION_OPEN);
        yon_gtk_window_setup(GTK_WINDOW(dialog->Window),GTK_WINDOW(window->MainWindow),CHOSE_PACKAGES_TITLE_LABEL,icon_path,"ChooseMoveFileChooserWindow");
        gtk_label_set_text(GTK_LABEL(dialog->HeaderTopic),CHOSE_PACKAGES_TITLE_LABEL);
        GtkFileFilter *filter = gtk_file_filter_new();
        gtk_file_filter_add_pattern(filter,"*.pkg.tar*[^.sig]");
        gtk_file_filter_set_name(filter,"*.pkg.tar.*");
        gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog->MainFileChooser),filter);
        GtkTreeIter iter,itar;
        char *target;
        const char* storage_path = gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->StorageCombo));
        storage_struct *storage = g_hash_table_lookup(main_config.storages,storage_path);
        if (storage){
            const char *repo_name = gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->RepositoryCombo));
            repo_struct *repo = g_hash_table_lookup(storage->repos,repo_name);
            if (repo){
                target = repo->path;
            }
            gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog->MainFileChooser),1);
            gtk_widget_show(dialog->ChooseFolderCheck);

        }
        gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog->MainFileChooser),target);

            dictionary *dict = NULL;
            yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
            yon_dictionary_add_or_create_if_exists_with_data(dict,"dialog",dialog);

        yon_file_chooser_set_root(dialog,target);
        if (yon_file_chooser_start(dialog)==GTK_RESPONSE_ACCEPT){
            on_move_add_accept(self,dict);
        }
    }
}


repo_add_window *on_remove_clicked(GtkWidget *self, main_window *widgets){
    repo_add_window *window = yon_repo_add_window_new();
    yon_gtk_window_setup(GTK_WINDOW(window->MainWindow),GTK_WINDOW(widgets->Window),REMOVE_PACKAGES_TITLE_LABEL,icon_path,"add-repo-window");
        gtk_label_set_text(GTK_LABEL(window->HeadLabel),REMOVE_PACKAGES_TITLE_LABEL);
    gtk_label_set_text(GTK_LABEL(window->PackagesToAddLabel),REMOVE_PACKAGES_LABEL);

    int size;
    config_str storages = (config_str)g_hash_table_get_keys_as_array(main_config.storages,&size);
    for (int i=0;i<size;i++){
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(window->StorageCombo),storages[i],storages[i]);
    }

    yon_sign_list_update();
    yon_combo_box_text_set_signs(GTK_COMBO_BOX_TEXT(window->RepoSignCombo));
    yon_combo_box_text_set_signs(GTK_COMBO_BOX_TEXT(window->PackageSignCombo));
    gtk_combo_box_set_active(GTK_COMBO_BOX(window->RepoSignCombo),0);

    gtk_widget_hide(gtk_widget_get_parent(window->PackageSignCombo));

    gtk_button_set_label(GTK_BUTTON(window->AddButton),REMOVE_LABEL);
    dictionary *dict = NULL;
    yon_dictionary_add_or_create_if_exists_with_data(dict,"widgets",widgets);
    yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
    g_signal_connect(G_OBJECT(window->ArchitectureCombo),"changed",G_CALLBACK(on_arch_chosen),window);
    g_signal_connect(G_OBJECT(window->StorageCombo),"changed",G_CALLBACK(on_add_target_repo_selection_changed),window);
    g_signal_connect(G_OBJECT(window->RepositoryCombo),"changed",G_CALLBACK(on_add_target_arch_selection_changed),window);
    g_signal_connect(G_OBJECT(window->ArchitectureCombo),"changed",G_CALLBACK(on_add_target_package_selection_changed),window);
    g_signal_connect(G_OBJECT(window->AddButton),"clicked",G_CALLBACK(on_remove_accept),dict);
    g_signal_connect(G_OBJECT(window->AddPackageButton),"clicked",G_CALLBACK(on_remove_add_package),window);
    g_signal_connect(G_OBJECT(window->RemoveButton),"clicked",G_CALLBACK(on_move_remove_package),window);
    // g_signal_connect(G_OBJECT(window->RepositoryCombo),"changed",G_CALLBACK(on_repo_sign_load),window);
    yon_add_window_set_selected(widgets,window);
    yon_add_window_add_chosen_packages(widgets,window);
    gtk_widget_show(window->MainWindow);
    return window;
}