#include "ubl-settings-bootloader.h"

void on_user_accept(GtkWidget *,dictionary *dict){
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    yon_user_window *window = yon_dictionary_get_data(dict->first->next,yon_user_window*);
    const char *username = gtk_entry_get_text(GTK_ENTRY(window->UsernameEntry));
    
    if (yon_char_is_empty(window->prev_name)){
        GtkTreeIter iter;
        GtkTreeModel *model = GTK_TREE_MODEL(widgets->BootUsersList);
        for_iter(model,&iter){
            char *name;
            gtk_tree_model_get(model,&iter,1,&name,-1);
            if (!strcmp(name,username)){
                dialog_confirmation_data *data = yon_confirmation_dialog_data_new();
                data->action_text = USER_EXIST_LABEL(username);
                if (yon_confirmation_dialog_call(window->Window,data)!=GTK_RESPONSE_ACCEPT){
                    return;
                }
            }
        }
    }
    const char *password = gtk_entry_get_text(GTK_ENTRY(window->PasswordEntry));
    if (yon_char_is_empty(username)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->UsernameEntry);
        return;
    }
    if (yon_char_is_empty(password)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->PasswordEntry);
        return;
    }
    if (!yon_char_is_empty(window->prev_name)&&strcmp(username,window->prev_name)){
        yon_config_remove_by_key(GRUB_PASSWORD(window->prev_name));
        int size;
        char *superusers = config(GRUB_SUPERUSERS_parameter);
        config_str parsed = yon_char_parse(superusers,&size,",");
        int pos = yon_char_parsed_check_exist(parsed,size,(char*)username);
        if (pos>-1){
            parsed = yon_char_parsed_rip(parsed,&size,pos);
            char *superusers = yon_char_parsed_to_string(parsed,size,",");
            if (!yon_char_is_empty(superusers)){
                yon_config_register(GRUB_SUPERUSERS_parameter,GRUB_SUPERUSERS_command,superusers);
            } else {
                yon_config_remove_by_key(GRUB_SUPERUSERS_parameter);
            }
        }
    }
    yon_config_register(GRUB_PASSWORD(username),GRUB_PASSWORD_command,(char*)password);

    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->AdminCheck))){
        int size;
        char *superusers = config(GRUB_SUPERUSERS_parameter);
        config_str parsed = yon_char_parse(superusers,&size,",");
        if (yon_char_parsed_check_exist(parsed,size,(char*)username)==-1){
            yon_char_parsed_add_or_create_if_exists(parsed,&size,(char*)username);
            char *superusers = yon_char_parsed_to_string(parsed,size,",");
            yon_config_register(GRUB_SUPERUSERS_parameter,GRUB_SUPERUSERS_command,superusers);
        }
    } else {
        int size;
        char *superusers = config(GRUB_SUPERUSERS_parameter);
        config_str parsed = yon_char_parse(superusers,&size,",");
        int pos = yon_char_parsed_check_exist(parsed,size,(char*)username);
        if (pos!=-1){
            parsed = yon_char_parsed_rip(parsed,&size,pos);

            char *superusers = yon_char_parsed_to_string(parsed,size,",");
            yon_config_register(GRUB_SUPERUSERS_parameter,GRUB_SUPERUSERS_command,superusers);
        }

    }
    gtk_widget_destroy(window->Window);
    yon_interface_update(widgets);
}

void on_user_add_clicked(GtkWidget *,main_window *widgets){
    yon_user_window *window = yon_user_window_new();
        char *title = USER_ADD_TITLE_LABEL;
        yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),title,icon_path,"ringtone_add_window");
        gtk_label_set_text(GTK_LABEL(window->TitleLabel),title);
    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->AcceptButton),"clicked",G_CALLBACK(on_user_accept),dict);
    gtk_widget_show(window->Window);

}

void on_user_edit_clicked(GtkWidget *,main_window *widgets){

    GtkTreeIter iter;
    GtkTreeModel *model = GTK_TREE_MODEL(widgets->BootUsersList);

    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->BootUsersTree)),&model,&iter)){
        yon_user_window *window = yon_user_window_new();
        int is_admin;
        char *name;
        gtk_tree_model_get(model,&iter,0,&is_admin,1,&name,-1);
        char *title = USER_EDIT_TITLE_LABEL(name);
        yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),title,icon_path,"ringtone_add_window");
        gtk_label_set_text(GTK_LABEL(window->TitleLabel),title);
        window->prev_name = yon_char_new(name);
        char *password = config(GRUB_PASSWORD(name));
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->AdminCheck),is_admin);
        gtk_entry_set_text(GTK_ENTRY(window->UsernameEntry),name);
        if (password){
            gtk_entry_set_text(GTK_ENTRY(window->PasswordEntry),password);
        }
        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->AcceptButton),"clicked",G_CALLBACK(on_user_accept),dict);
        gtk_widget_show(window->Window);
    } 


}

void on_user_remove_clicked(GtkWidget *,main_window *widgets){
    GtkTreeIter iter;
    GtkTreeModel *model;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->BootUsersTree)),&model,&iter)){
        char *name;
        gtk_tree_model_get(model,&iter,1,&name,-1);
        dialog_confirmation_data *data = yon_confirmation_dialog_data_new();
        data->action_text = USER_REMOVE_CONFIRM_LABEL(name);
        data->title = USER_DELETE_TITLE_LABEL(name);
        if (yon_confirmation_dialog_call(widgets->Window,data)==GTK_RESPONSE_ACCEPT){
            yon_config_remove_by_key(GRUB_PASSWORD(name));
            yon_config_remove_element(GRUB_SUPERUSERS_parameter,name,",");
            yon_interface_update(widgets);
        }
    }
}

yon_user_window *yon_user_window_new(){
    yon_user_window *window = new(yon_user_window);
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_user);
    window->Window = yon_gtk_builder_get_widget(builder,"MainWindow");
    window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    window->TitleLabel = yon_gtk_builder_get_widget(builder,"TitleLabel");
    window->AdminCheck = yon_gtk_builder_get_widget(builder,"AdminCheck");
    window->UsernameEntry = yon_gtk_builder_get_widget(builder,"UsernameEntry");
    window->PasswordEntry = yon_gtk_builder_get_widget(builder,"PasswordEntry");
    window->PasswordButton = yon_gtk_builder_get_widget(builder,"PasswordButton");
    window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    window->AcceptButton = yon_gtk_builder_get_widget(builder,"AcceptButton");
    window->prev_name = NULL;

    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    g_signal_connect(G_OBJECT(window->PasswordButton),"clicked",G_CALLBACK(yon_password_change),window->PasswordEntry);
    yon_gtk_entry_set_password_visibility_icon(GTK_ENTRY(window->PasswordEntry));
    // yon_gtk_entry_block_restricted_symbols(GTK_ENTRY(window->UsernameEntry));
    return window;
}