#include "ubl-settings-diskquota.h"

config_settings_gui* _main_config;

//signal emmit handlers - no header initialization

/**on_close_subwindow(GtkWidget *self)
 * [EN]
 * Closes window in which [self] is contained.
 * [RU]
 * Закрывает окно, в котором расположен виджет [self].
*/
void on_close_subwindow(GtkWidget *self){
    gtk_widget_destroy(gtk_widget_get_toplevel(self));
}

/**yon_open_browser(GtkWidget *self, char *link)
 * [EN]
 * Opens browser with [link] link.
 * [RU]
 * Открывает браузер с [link] ссылкой.
*/
void yon_open_browser(GtkWidget *self, char *link){
    yon_ubl_browser_window_open(link,TITLE_LABEL);
}

/**on_open_documentation_confirmation(GtkWidget *self, char *link)
 * [EN]
 * Opens confirmation window for [link] link.
 * [RU]
 * Открывает окно подтверждение перехода по ссылке [link].
*/
void on_open_documentation_confirmation(GtkWidget *self, char *link){
    if (_main_config->always_open_documentation==0){
        GtkBuilder *builder = gtk_builder_new_from_resource(glade_path);
        documentation_confirmation_window *widgets = malloc(sizeof(documentation_confirmation_window));
        widgets->Window = yon_gtk_builder_get_widget(builder,"helpConfirmationWindow");
        widgets->AcceptButton = yon_gtk_builder_get_widget(builder,"ReadHelpButton");
        widgets->CloseButton = yon_gtk_builder_get_widget(builder,"CancelHelpButton");
        widgets->HatText = yon_gtk_builder_get_widget(builder,"webHeaderNameLabel");
        widgets->HeaderText = yon_gtk_builder_get_widget(builder,"helpHeader");
        widgets->InfoText = yon_gtk_builder_get_widget(builder,"helpText");
        widgets->AlwaysOpenCheck = yon_gtk_builder_get_widget(builder,"AlwaysOpenDocumentationCheckbox");
        gtk_label_set_text(GTK_LABEL(widgets->HatText),TITLE_LABEL);
        gtk_label_set_text(GTK_LABEL(widgets->HeaderText),HELP_TITLE_LABEL);
        gtk_label_set_text(GTK_LABEL(widgets->InfoText),HELP_INFO_LABEL);
        gtk_button_set_label(GTK_BUTTON(widgets->AcceptButton),OPEN_HELP_LABEL);
        gtk_button_set_label(GTK_BUTTON(widgets->AlwaysOpenCheck),HELP_ALWAYS_OPEN_LABEL);
        gtk_button_set_label(GTK_BUTTON(widgets->CloseButton),CANCEL_LABEL);
        gtk_widget_show_all(widgets->Window);
        g_signal_connect(G_OBJECT(widgets->CloseButton),"clicked",G_CALLBACK(on_close_subwindow),NULL);
        g_signal_connect(G_OBJECT(widgets->AcceptButton),"clicked",G_CALLBACK(yon_open_browser),yon_char_new(link));
        g_signal_connect(G_OBJECT(widgets->AcceptButton),"clicked",G_CALLBACK(on_close_subwindow),NULL);
        
        
    } else {
        yon_open_browser(self,link);
    }
}

/**on_link(GtkWidget *self, char* uri, gpointer user_data)
 * [EN]
 * Signal for hadnling AboutDialog links.
 * Connect to "activate-link" signal.
 * [self] is AboutDialog window;
 * [uri] is activated link;
 * [user_data] is pointer for user data, hasn't used in standard handler;
 * [RU]
 * Функция для обработки сигнала нажатия на ссылку окна AboutDialog.
 * Присоединять к сигналу "activate-link".
 * [self] - окно AboutDialog;
 * [uri] - ссылка, по которой совершается переход;
 * [user_data] - указатель на любые другие данные, не используется в стандартном обработчике;
*/
void on_link(GtkWidget *self, char* uri, gpointer user_data){
    gtk_widget_destroy(self);
    on_open_documentation_confirmation(self,uri);
}

/**on_about()
 * [EN]
 * Function for setting up and showing AboutDialog.
 * Connect it to "activate" signal of Documentation MenuItem.
 * [RU]
 * Функиця для настройки и показа окна AboutDialog.
 * Присоединять к сигналу "activate" кнопки справки типа MenuItem.
*/
void on_about(){
    GtkBuilder *builder=gtk_builder_new_from_resource(glade_path);
    GtkWidget *window=yon_gtk_builder_get_widget(builder,"AboutWindow");
    GtkWidget *title=yon_gtk_builder_get_widget(builder,"headerAboutTopic");
    GtkWidget *hideButtonBox=yon_gtk_builder_get_widget(builder,"buttonBoxHide");
    gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(window),version_application);
    gtk_about_dialog_set_website_label(GTK_ABOUT_DIALOG(window),PROJECT_HOME_LABEL);
    gtk_about_dialog_set_comments(GTK_ABOUT_DIALOG(window),TITLE_LABEL);
    gtk_label_set_text(GTK_LABEL(title),TITLE_LABEL);
    g_signal_connect(G_OBJECT(window),"activate-link",G_CALLBACK(on_link),NULL);
    gtk_widget_set_visible(hideButtonBox,0);
    gtk_widget_destroy(hideButtonBox);
    gtk_widget_show(window);
}

//functions

void yon_load_proceed(char *command){
    if (yon_config_load_register(command))
        yon_ubl_status_box_render(LOCAL_SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
    else
        yon_ubl_status_box_render(LOAD_FAILED_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);
    
}

// standard functions

void config_init(config_settings_gui* main_config){
    _main_config = main_config;
    _main_config->always_open_documentation=0;
    _main_config->win_height=0;
    _main_config->win_width=0;
    _main_config->win_pos_x=0;
    _main_config->win_pos_y=0;
    _main_config->socket_id=-1;
    _main_config->save_socket_id=-1;
    _main_config->load_socket_id=-1;
    _main_config->lock_help=0;
    _main_config->lock_help=0;
    _main_config->lock_load_global=0;
    _main_config->lock_save_global=0;
    _main_config->lock_save_local=0;
}

void wrapper_fill_tree_view_generate(config_all* all_config, custom_window* custom) {
    g_object_ref(custom->liststoreGeneral);
    gtk_list_store_clear(custom->liststoreGeneral);
    fill_tree_view_generate(&all_config->v_user, &all_config->v_disk_status, "usrquota",custom->treeViewGeneral, custom->liststoreGeneral);
    fill_tree_view_generate(&all_config->v_group, &all_config->v_disk_status, "grpquota",custom->treeViewGeneral, custom->liststoreGeneral);
    fill_tree_view_generate(&all_config->v_project, &all_config->v_disk_status, "prjquota",custom->treeViewGeneral, custom->liststoreGeneral);
    
}

main_window *setup_window(main_window* widgets, custom_window* custom_widgets){
    /* Widgets getting | Получение виджетов */
    widgets = malloc(sizeof(main_window));
    device_disk_parsed();
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path);
    gtk_builder_set_translation_domain(builder, LocaleName);
    widgets->Window = yon_gtk_builder_get_widget(builder,"MainWindow");
    widgets->HatLabel = yon_gtk_builder_get_widget(builder,"headerTopic");
    widgets->PlugBox = yon_gtk_builder_get_widget(builder,"plugBox");

    widgets->HeadOverlay = yon_gtk_builder_get_widget(builder,"HeadOverlay");
    widgets->HeadImage = yon_gtk_builder_get_widget(builder,"HeadBackgroundImage");
    widgets->HeadBox = yon_gtk_builder_get_widget(builder,"HeaderBox");
    widgets->HeadTitleLabel = yon_gtk_builder_get_widget(builder,"HeaderTitleLabel");
    widgets->HeadInfoLabel = yon_gtk_builder_get_widget(builder,"HeaderInfoLabel");
    
    widgets->StatusBox = yon_gtk_builder_get_widget(builder,"mainStatusBox");
    widgets->StatusIcon = yon_gtk_builder_get_widget(builder,"mainStatusIcon");
    widgets->StatusLabel = yon_gtk_builder_get_widget(builder,"mainStatusLabel");

    widgets->SaveLabel = yon_gtk_builder_get_widget(builder,"headerSaveConfigLabel");
    widgets->SaveMenuItem = yon_gtk_builder_get_widget(builder,"SaveGlobalLocalConfigurationMenuItem");
    widgets->SaveGlobalMenuItem = yon_gtk_builder_get_widget(builder,"SaveGlobalConfigurationMenuItem");
    widgets->SaveLocalMenuItem = yon_gtk_builder_get_widget(builder,"SaveLocalConfigurationMenuItem");
    widgets->RightBox = yon_gtk_builder_get_widget(builder,"HeaderRightBox");

    widgets->LoadLabel = yon_gtk_builder_get_widget(builder,"headerLoadConfigLabel");
    widgets->LoadGlobalMenuItem = yon_gtk_builder_get_widget(builder,"LoadGlobalConfigurationMenuItem");
    widgets->LoadLocalMenuItem = yon_gtk_builder_get_widget(builder,"LoadLocalConfigurationMenuItem");
    widgets->LeftBox = yon_gtk_builder_get_widget(builder,"HeaderLeftBox");

    widgets->DocumentationMenuItem = yon_ubl_menu_item_documentation_new(DOCUMENTATION_LABEL); // yon_gtk_builder_get_widget(builder,"DocumentationMenuItem");
    widgets->AboutMenuItem = yon_ubl_menu_item_about_new(ABOUT_LABEL); // yon_gtk_builder_get_widget(builder,"AboutMenuItem");
    
    gtk_window_set_title(GTK_WINDOW(widgets->Window),TITLE_LABEL);
    GtkWidget *menu = yon_gtk_builder_get_widget(builder,"menu2");
    gtk_menu_shell_append(GTK_MENU_SHELL(menu),widgets->DocumentationMenuItem);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu),widgets->AboutMenuItem);

    if (_main_config->lock_load_global == 1){
        gtk_widget_set_sensitive(widgets->LoadGlobalMenuItem,0);
    }
    if (_main_config->lock_save_global == 1){
        gtk_widget_set_sensitive(widgets->SaveGlobalMenuItem,0);
        gtk_widget_set_sensitive(widgets->SaveMenuItem,0);
    }
    if (_main_config->lock_save_local == 1){
        gtk_widget_set_sensitive(widgets->SaveLocalMenuItem,0);
        gtk_widget_set_sensitive(widgets->SaveMenuItem,0);
    }

    /* Signal connection | Присоединение сигналов */
    g_signal_connect(G_OBJECT(widgets->Window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
    g_signal_connect(G_OBJECT(widgets->DocumentationMenuItem),"activate",G_CALLBACK(on_open_documentation_confirmation),WIKI_LINK);
    g_signal_connect(G_OBJECT(widgets->AboutMenuItem),"activate",G_CALLBACK(on_about),NULL);
    
    setup_window_custom(custom_widgets, builder);
    gtk_widget_show(widgets->Window);
    return widgets;
}


custom_window *setup_window_custom(custom_window* custom_widgets, GtkBuilder *builder) {
    custom_widgets->user.name = yon_char_new("usrquota");
    custom_widgets->group.name = yon_char_new("grpquota");
    custom_widgets->project.name = yon_char_new("grpquota");

    custom_widgets->user.note_selected = 0;
    custom_widgets->group.note_selected = 1;
    custom_widgets->project.note_selected = 2;
    custom_widgets->device.note_selected = 3;

    custom_widgets->user.btnAdd = yon_gtk_builder_get_widget(builder,"btnAddUsers");
    custom_widgets->user.btnEdit = yon_gtk_builder_get_widget(builder,"btnEditUsers");
    custom_widgets->user.btnDel = yon_gtk_builder_get_widget(builder,"btnDelUsers");
    custom_widgets->user.btnFilter = yon_gtk_builder_get_widget(builder,"btnFilterUsers");
    
    custom_widgets->group.btnAdd = yon_gtk_builder_get_widget(builder,"btnAddGroups");
    custom_widgets->group.btnEdit = yon_gtk_builder_get_widget(builder,"btnEditGroups");
    custom_widgets->group.btnDel = yon_gtk_builder_get_widget(builder,"btnDelGroups");
    custom_widgets->group.btnFilter = yon_gtk_builder_get_widget(builder,"btnFilterGroups");
    
    custom_widgets->project.btnAdd = yon_gtk_builder_get_widget(builder,"btnAddProject");
    custom_widgets->project.btnEdit = yon_gtk_builder_get_widget(builder,"btnEditProject");
    custom_widgets->project.btnDel = yon_gtk_builder_get_widget(builder,"btnDelProject");
    custom_widgets->project.btnFilter = yon_gtk_builder_get_widget(builder,"btnFilterProject");
    
    custom_widgets->user.tree_view = yon_gtk_builder_get_widget(builder,"usersQuotasTree");
    custom_widgets->group.tree_view = yon_gtk_builder_get_widget(builder,"groupsQuotasTree");
    custom_widgets->project.tree_view = yon_gtk_builder_get_widget(builder,"ProjectQuotasTree");
    custom_widgets->user.liststore = GTK_LIST_STORE(gtk_builder_get_object(builder, "liststoreUser"));
    custom_widgets->group.liststore = GTK_LIST_STORE(gtk_builder_get_object(builder, "liststoreGroups"));
    custom_widgets->project.liststore = GTK_LIST_STORE(gtk_builder_get_object(builder, "liststoreProject"));
    custom_widgets->liststoreGeneral = GTK_LIST_STORE(gtk_builder_get_object(builder, "liststoreGeneral"));
    custom_widgets->treeViewGeneral = yon_gtk_builder_get_widget(builder,"GeneralQuotasTree");
    custom_widgets->device.btnEdit = yon_gtk_builder_get_widget(builder,"btnEditDevice");
    custom_widgets->device.btnAdd = yon_gtk_builder_get_widget(builder,"btnAddDevice");
    custom_widgets->device.btnDel = yon_gtk_builder_get_widget(builder,"btnDelDevice");
    custom_widgets->device.tree_view = yon_gtk_builder_get_widget(builder,"DeviceQuotasTree");
    custom_widgets->device.Update = yon_gtk_builder_get_widget(builder,"btnUpdateDevices");
    custom_widgets->device.liststore = GTK_LIST_STORE(gtk_builder_get_object(builder, "liststoreDevice"));


    GList *list = gtk_tree_view_get_columns(GTK_TREE_VIEW(custom_widgets->treeViewGeneral));
    for (int i = 0; i < g_list_length(list); i++){
        yon_window_config_add_listener(g_list_nth_data(list,i),yon_char_append("general_tree",yon_char_from_int(i)),"fixed-width",YON_TYPE_INT);
        yon_gtk_column_minimal_fixed_size_set((GtkTreeViewColumn*)g_list_nth_data(list,i));
    }
    g_list_free(list);
    list = gtk_tree_view_get_columns(GTK_TREE_VIEW(custom_widgets->device.tree_view));
    for (int i = 0; i < g_list_length(list); i++){
        yon_window_config_add_listener(g_list_nth_data(list,i),yon_char_append("device_tree",yon_char_from_int(i)),"fixed-width",YON_TYPE_INT);
        yon_gtk_column_minimal_fixed_size_set((GtkTreeViewColumn*)g_list_nth_data(list,i));
    }
    g_list_free(list);

    list = gtk_tree_view_get_columns(GTK_TREE_VIEW(custom_widgets->user.tree_view));
    for (int i = 0; i < g_list_length(list); i++){
        yon_window_config_add_listener(g_list_nth_data(list,i),yon_char_append("user_tree",yon_char_from_int(i)),"fixed-width",YON_TYPE_INT);
        yon_gtk_column_minimal_fixed_size_set((GtkTreeViewColumn*)g_list_nth_data(list,i));
    }
    g_list_free(list);

    list = gtk_tree_view_get_columns(GTK_TREE_VIEW(custom_widgets->group.tree_view));
    for (int i = 0; i < g_list_length(list); i++){
        yon_window_config_add_listener(g_list_nth_data(list,i),yon_char_append("group_tree",yon_char_from_int(i)),"fixed-width",YON_TYPE_INT);
        yon_gtk_column_minimal_fixed_size_set((GtkTreeViewColumn*)g_list_nth_data(list,i));
    }
    g_list_free(list);

    list = gtk_tree_view_get_columns(GTK_TREE_VIEW(custom_widgets->project.tree_view));
    for (int i = 0; i < g_list_length(list); i++){
        yon_window_config_add_listener(g_list_nth_data(list,i),yon_char_append("project_tree",yon_char_from_int(i)),"fixed-width",YON_TYPE_INT);
        yon_gtk_column_minimal_fixed_size_set((GtkTreeViewColumn*)g_list_nth_data(list,i));
    }
    g_list_free(list);

    return custom_widgets;
}
