#include "ubl-settings-usergroups.h"

void on_delete_system_user(GtkWidget *, dictionary *dict){
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    yon_confirmation_window *dialog = yon_dictionary_get_data(dict->first->next,yon_confirmation_window*);
    GtkTreeModel *model = GTK_TREE_MODEL(dialog->list);
    GtkTreeIter iter;
    char *command=NULL;
    for_iter(model,&iter){
        int status,delete_home_status;
        char *target;
        gtk_tree_model_get(model,&iter,0,&status,1,&target,3,&delete_home_status,-1);
        if (command) command=yon_char_unite(command,"; ",delete_home_status?remove_user_and_homedir_from_system_command(target):remove_user_from_system_command(target),NULL);
        else command = delete_home_status?remove_user_and_homedir_from_system_command(target):remove_user_from_system_command(target);
    }
    if (!yon_char_is_empty(command)){
        if (system(yon_debug_output("%s\n",command))){
            yon_ubl_status_box_render(USER_REMOVE_ERROR,BACKGROUND_IMAGE_FAIL_TYPE);
        } else {

            textdomain(template_ui_LocaleName);
            yon_ubl_status_box_render(SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
            textdomain(LocalePath);
        }
    }
    on_subwindow_close(dialog->Window);
    yon_system_load(widgets);
}

void on_delete_system_group(GtkWidget *, dictionary *dict){
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    yon_confirmation_window *dialog = yon_dictionary_get_data(dict->first->next,yon_confirmation_window*);
    GtkTreeModel *model = GTK_TREE_MODEL(dialog->list);
    GtkTreeIter iter;
    char *command=NULL;
    for_iter(model,&iter){
        int status,delete_home_status;
        char *target;
        gtk_tree_model_get(model,&iter,0,&status,1,&target,3,&delete_home_status,-1);
        if (command) command=yon_char_unite(command,"; ",remove_group_from_system_command(target),NULL);
        else command = remove_group_from_system_command(target);
    }
    if (!yon_char_is_empty(command)){
        if (system(yon_debug_output("%s\n",command))){
            yon_ubl_status_box_render(USER_REMOVE_ERROR,BACKGROUND_IMAGE_FAIL_TYPE);
        } else {

            textdomain(template_ui_LocaleName);
            yon_ubl_status_box_render(SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
            textdomain(LocalePath);
        }
    }
    on_subwindow_close(dialog->Window);
    yon_system_load(widgets);
}

void yon_remove_confirmation_window_accept_clicked(GtkWidget *self,dictionary *dict){
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window *);
    system_remove_confirmation_window *window = yon_dictionary_get_data(dict->first->next,system_remove_confirmation_window *);
    if (self == window->ConfigAcceptButton){
        if (gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->MainNotebook))==2){
            GtkTreeIter iter, itar,child;
            GtkTreeModel *model;
            char *target;
            if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->SystemTree)),&model,&iter)){
				yon_tree_model_sort_filter_get_model_iter(GTK_TREE_MODEL_SORT(model),&iter,&child);
                gtk_tree_model_get(GTK_TREE_MODEL(widgets->liststore1),&child,2,&target,-1);
            }
            for_iter(GTK_TREE_MODEL(widgets->UsersList),&itar){
                char *cur;
                gtk_tree_model_get(GTK_TREE_MODEL(widgets->UsersList),&itar,2,&cur,-1);
                if (!strcmp(target,cur)){
                    yon_tree_model_sort_filter_get_child_iter(GTK_TREE_MODEL_SORT(widgets->ConfigUsersSort),&itar,&child);
                    gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->UsersTree)),&child);
                    break;
                }
            }
            gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->MainNotebook),0);
            yon_delete_confirmation_open(widgets);
        } else if (self == window->AcceptButton || self == window->SystemAcceptButton) {
            GtkTreeIter iter, itar,child;
            GtkTreeModel *model;
            char *target;
            if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->SystemGroupsTree)),&model,&iter)){
                gtk_tree_model_get(model,&iter,1,&target,-1);
            }
            for_iter(GTK_TREE_MODEL(widgets->GroupsList),&itar){
                char *cur;
                gtk_tree_model_get(GTK_TREE_MODEL(widgets->GroupsList),&itar,1,&cur,-1);
                if (!strcmp(target,cur)){
                    yon_tree_model_sort_filter_get_child_iter(GTK_TREE_MODEL_SORT(widgets->ConfigUsersSort),&itar,&child);
                    gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->GroupsTree)),&child);
                    break;
                }
            }
            gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->MainNotebook),1);
            yon_group_delete_confirmation_open(widgets);
        }
    } else {
        yon_confirmation_window *dialog = yon_delete_confirmation_new();
        dictionary *dact = NULL;
        yon_dictionary_add_or_create_if_exists_with_data(dact,"widgets",widgets);
        yon_dictionary_add_or_create_if_exists_with_data(dact,"dialog",dialog);
        if (gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->MainNotebook))==2){
			gtk_label_set_text(GTK_LABEL(dialog->TitleLabel),DELETE_CONFIRMATION_CONFIRM_LABEL);
			gtk_label_set_text(GTK_LABEL(dialog->HatLabel),DELETE_CONFIRMATION_TITLE_LABEL);
            yon_gtk_window_setup(GTK_WINDOW(dialog->Window),GTK_WINDOW(widgets->Window),DELETE_CONFIRMATION_TITLE_LABEL,icon_path,"delete-system-user-window");
            g_signal_connect(G_OBJECT(dialog->AcceptButton),"clicked",G_CALLBACK(on_delete_system_user),dact);
            GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(widgets->SystemTree));
            GtkTreeIter iter,child;
            if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->SystemTree)),&model,&iter)){
				yon_tree_model_sort_filter_get_model_iter(GTK_TREE_MODEL_SORT(model),&iter,&child);
                char *name,*username,*homedir=NULL;
                gtk_tree_model_get(GTK_TREE_MODEL(widgets->liststore1),&child,2,&name,3,&username,6,&homedir,-1);
                gtk_list_store_append(dialog->list,&child);
                gtk_list_store_set(dialog->list,&child,0,1,1,name,2,homedir,4,!yon_char_is_empty(homedir),5,username,-1);
                gtk_widget_show(dialog->Window);
            }

        } else {
            GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(widgets->SystemGroupsTree));
            GtkTreeIter iter,child;
            if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->SystemGroupsTree)),&model,&iter)){
				yon_tree_model_sort_filter_get_model_iter(GTK_TREE_MODEL_SORT(model),&iter,&child);
                yon_gtk_window_setup(GTK_WINDOW(dialog->Window),GTK_WINDOW(widgets->Window),DELETE_GROUP_CONFIRMATION_TITLE_LABEL,icon_path,"delete-system-gruop-window");
                g_signal_connect(G_OBJECT(dialog->AcceptButton),"clicked",G_CALLBACK(on_delete_system_group),dact);
                gtk_widget_show(window->AcceptButton);
                gtk_widget_hide(window->ConfigAcceptButton);
                gtk_widget_hide(window->SystemAcceptButton);
                char *name;
                gtk_label_set_text(GTK_LABEL(window->TitleLabel),DELETE_GROUP_CONFIRMATION_CONFIRM_LABEL);
                gtk_label_set_text(GTK_LABEL(dialog->HatLabel),DELETE_GROUP_CONFIRMATION_TITLE_LABEL);
                gtk_tree_view_column_set_title(dialog->UserCell,GROUP_LABEL);
                gtk_tree_view_column_set_visible(gtk_tree_view_get_column(GTK_TREE_VIEW(dialog->DeletionTree),2),0);
                gtk_tree_view_column_set_visible(gtk_tree_view_get_column(GTK_TREE_VIEW(dialog->DeletionTree),3),0);
                gtk_tree_view_column_set_visible(gtk_tree_view_get_column(GTK_TREE_VIEW(dialog->DeletionTree),4),0);

                gtk_tree_model_get(GTK_TREE_MODEL(widgets->liststore2),&child,1,&name,-1);
                gtk_list_store_append(dialog->list,&child);
                gtk_list_store_set(dialog->list,&child,0,1,1,name,-1);
                gtk_widget_show(dialog->Window);
            }
        }
    }
        on_subwindow_close(self);
}

system_remove_confirmation_window *yon_system_remove_confirmation_window_new(){
    system_remove_confirmation_window *window = malloc(sizeof(system_remove_confirmation_window));
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_ubl_settings_usergroups_system_deletion_confirmation);

    window->Window = yon_gtk_builder_get_widget(builder,"Window");
    window->HeaderLabel = yon_gtk_builder_get_widget(builder,"HeaderLabel");
    window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    window->TitleLabel = yon_gtk_builder_get_widget(builder,"TitleLabel");
    window->AcceptButton = yon_gtk_builder_get_widget(builder,"AcceptButton");
    window->ConfigAcceptButton = yon_gtk_builder_get_widget(builder,"ConfigAcceptButton");
    window->SystemAcceptButton = yon_gtk_builder_get_widget(builder,"SystemAcceptButton");

    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);

    return window;
}