#include "ubl-settings-update.h"

void on_repo_move_up(GtkWidget *,main_window *widgets){
    GtkTreeModel *model;
    GtkTreeIter iter,*previter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->RepositoriesTree)),&model,&iter)){
        char *source;
        gtk_tree_model_get(model,&iter,1,&source,-1);
        char *source_param = REPOSITORY(source);
        previter = gtk_tree_iter_copy(&iter);
        if (gtk_tree_model_iter_previous(model,previter)){
            char *target;
            gtk_tree_model_get(model,previter,1,&target,-1);
            char *target_param = REPOSITORY(target);
            gtk_list_store_move_before(widgets->ReposList,&iter,previter);
            gtk_tree_iter_free(previter);
            yon_config_move_before(source_param,target_param);
            yon_config_set_status(source_param,1);

            int size;
            config_str params = yon_config_get_all_by_key(REPOSITORY_search,&size);
            for (int i=0;i<size;i++){
                char *target_name = yon_char_divide_search(params[i],"=",-1);
                yon_config_compare_ignore_set(target_name,1);
                yon_config_remove_ignore(target_name);
                yon_config_set_status(target_name,1);
                free(target_name);
            }
            yon_char_parsed_free(params,size);
            free(target_param);
        }
        free(source_param);
    }
}

void on_repo_move_down(GtkWidget *,main_window *widgets){
    GtkTreeModel *model;
    GtkTreeIter iter,*previter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->RepositoriesTree)),&model,&iter)){
        char *source;
        gtk_tree_model_get(model,&iter,1,&source,-1);
        char *source_param = REPOSITORY(source);
        previter = gtk_tree_iter_copy(&iter);
        if (gtk_tree_model_iter_next(model,previter)){
            char *target;
            gtk_tree_model_get(model,previter,1,&target,-1);
            char *target_param = REPOSITORY(target);
            gtk_list_store_move_after(widgets->ReposList,&iter,previter);
            gtk_tree_iter_free(previter);
            yon_config_move_after(source_param,target_param);

            int size;
            config_str params = yon_config_get_all_by_key(REPOSITORY_search,&size);
            for (int i=0;i<size;i++){
                char *target_name = yon_char_divide_search(params[i],"=",-1);
                yon_config_compare_ignore_set(target_name,1);
                yon_config_remove_ignore(target_name);
                yon_config_set_status(target_name,1);
                free(target_name);
            }
            yon_char_parsed_free(params,size);
            free(target_param);
        }
        free(source_param);
    }
}
