#include "ubl-settings-bootloader.h"

void on_ringtone_accept(GtkWidget *,dictionary *dict){
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    yon_ringtone_window *window = yon_dictionary_get_data(dict->first->next,yon_ringtone_window*);
    const char *username = gtk_entry_get_text(GTK_ENTRY(window->NameEntry));
    const char *password = gtk_entry_get_text(GTK_ENTRY(window->CodeEntry));
    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->NameEntry);
        return;
    }
    if (yon_char_is_empty(window->prev_name)){
        GtkTreeIter iter;
        GtkTreeModel *model = GTK_TREE_MODEL(widgets->BootUsersList);
        for_iter(model,&iter){
            char *name;
            int user_created;
            gtk_tree_model_get(model,&iter,1,&name,3,&user_created,-1);
            if (user_created){
                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;
                    }
                    break;
                }
            } else {
                yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),NAME_OCCUPIED_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                yon_ubl_status_highlight_incorrect(window->NameEntry);
                return;
            }
        }
    }
    // 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->NameEntry);
    //     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->CodeEntry);
        return;
    }
    if (!yon_char_is_empty(window->prev_name)&&strcmp(username,window->prev_name)){
        yon_window_config_erase_instant_parameter(window->prev_name,"melodies");
        char *config_melody = config(GRUB_PLAY_parameter);
        if (!yon_char_is_empty(config_melody)&&!strcmp(config_melody,password)){
            yon_config_remove_by_key(GRUB_PLAY_parameter);
        }
    }
    yon_window_config_add_instant_parameter((char*)username,"melodies",(char*)password,YON_TYPE_STRING);
    gtk_widget_destroy(window->Window);
    yon_interface_update(widgets);
}

void on_ringtone_add_clicked(GtkWidget *,main_window *widgets){
    yon_ringtone_window *window = yon_ringtone_window_new();
        char *title = RINGTONE_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_ringtone_accept),dict);
    gtk_widget_show(window->Window);

}

void on_ringtone_edit_clicked(GtkWidget *,main_window *widgets){

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

    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->BootRingtoneTree)),&model,&iter)){
        yon_ringtone_window *window = yon_ringtone_window_new();
        int is_chosen;
        char *name, *code;
        gtk_tree_model_get(model,&iter,0,&is_chosen,1,&name,2,&code,-1);
        char *title = RINGTONE_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);
        free(title);
        window->prev_name = yon_char_new(name);
        
        gtk_entry_set_text(GTK_ENTRY(window->NameEntry),name);
            gtk_entry_set_text(GTK_ENTRY(window->CodeEntry),code);
        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_ringtone_accept),dict);
        gtk_widget_show(window->Window);
    }


}

void on_ringtone_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->BootRingtoneTree)),&model,&iter)){
        char *name, *code;
        gtk_tree_model_get(model,&iter,1,&name,2,&code,-1);
        dialog_confirmation_data *data = new(dialog_confirmation_data);
        data->data=NULL;
        data->function=NULL;
        data->action_text = RINGTONE_REMOVE_CONFIRM_LABEL(name);
        data->title = RINGTONE_DELETE_TITLE_LABEL(name);
        if (yon_confirmation_dialog_call(widgets->Window,data)==GTK_RESPONSE_ACCEPT){
            char *config_code = config(GRUB_PLAY_parameter);
            yon_window_config_erase_instant_parameter(name,"melodies");
            if (!yon_char_is_empty(config_code)&&!strcmp(code,config_code)){
                yon_config_remove_by_key(GRUB_PLAY_parameter);
            }

            yon_interface_update(widgets);
        }
    }
}

yon_ringtone_window *yon_ringtone_window_new(){
    yon_ringtone_window *window = new(yon_ringtone_window);
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_ringtone);
    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->NameEntry = yon_gtk_builder_get_widget(builder,"NameEntry");
    window->CodeEntry = yon_gtk_builder_get_widget(builder,"CodeEntry");
    window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    window->AcceptButton = yon_gtk_builder_get_widget(builder,"AcceptButton");
    window->prev_name = NULL;

    yon_on_entry_set_allowed_symbols(GTK_ENTRY(window->CodeEntry),"01234567890 ");
    yon_gtk_entry_block_restricted_symbols(GTK_ENTRY(window->NameEntry));
    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    return window;
}