#include "upac-gtk.h"


gboolean on_install_slider_hover_highlight(GtkWidget *, GdkEvent *, desktop_struct *desktop){
    gtk_widget_show(desktop->PrevInstallationSliderImage);
    gtk_widget_show(desktop->NextInstallationSliderImage);
    return 0;
}

gboolean on_install_slider_hover_highlight_end(GtkWidget *, GdkEvent *, desktop_struct *desktop){
    gtk_widget_hide(desktop->PrevInstallationSliderImage);
    gtk_widget_hide(desktop->NextInstallationSliderImage);
    return 0;
}

gboolean on_desktop_image_prev_clicked(GtkWidget *,GdkEvent *, desktop_struct *desktop){
    desktop->current_image--;
    if (desktop->current_image<0){
        desktop->current_image=desktop->image_size-1;
    }
    GdkPixbuf *pixbuf = gdk_pixbuf_new_from_resource_at_scale(desktop->image_paths[desktop->current_image],350,250,1,NULL);
    gtk_image_set_from_pixbuf(GTK_IMAGE(desktop->MainImage),pixbuf);
    if (pixbuf) g_object_unref(pixbuf);
    return 0;
}

gboolean on_desktop_image_next_clicked(GtkWidget *,GdkEvent *, desktop_struct *desktop){
    desktop->current_image++;
    if (desktop->current_image==desktop->image_size){
        desktop->current_image=0;
    }
    GdkPixbuf *pixbuf = gdk_pixbuf_new_from_resource_at_scale(desktop->image_paths[desktop->current_image],350,250,1,NULL);
    gtk_image_set_from_pixbuf(GTK_IMAGE(desktop->MainImage),pixbuf);
    g_object_unref(pixbuf);
    return 0;
}

void on_desktop_checked(GtkWidget *, main_window *widgets){
    yon_size_update(widgets);
}

void on_desktop_about(GtkWidget *, desktop_struct *desktop){
    // yon_package_info_window_create(desktop->package);
    yon_package_info_window_open(desktop->package);
    // yon_packages_info *info = yon_packages_get_info_struct(YON_PACKAGES_SYNC,desktop->package);
    // char *info_string = yon_packages_get_info_string(info);
    // yon_window *window = yon_window_new();
    // yon_gtk_window_setup(GTK_WINDOW(window->Window),NULL,TITLE_INFO_LABEL,icon_path,NULL);
    // GtkWidget *info_label = gtk_label_new(NULL);
    // gtk_label_set_xalign(GTK_LABEL(info_label),0);
    // gtk_widget_set_margin_start(info_label,5);
    // gtk_widget_set_margin_top(info_label,5);
    // gtk_widget_set_margin_end(info_label,5);
    // gtk_widget_set_margin_bottom(info_label,5);
    // gtk_label_set_markup(GTK_LABEL(info_label),info_string);
    // gtk_box_pack_start(GTK_BOX(window->MainBox),info_label,1,1,0);
    // gtk_widget_show(info_label);
    
    // gtk_widget_show(window->Window);
}

void yon_desktop_update(desktop_struct *desktop){
    gtk_label_set_text(GTK_LABEL(desktop->NameLabel),desktop->name);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(desktop->InstallCheck),yon_packages_check_installed(desktop->package));
    GdkPixbuf *pixbuf = gdk_pixbuf_new_from_resource_at_scale(desktop->image_paths[0],350,250,1,NULL);
    gtk_image_set_from_pixbuf(GTK_IMAGE(desktop->MainImage),pixbuf);
    if (pixbuf) g_object_unref(pixbuf);
}

desktop_struct *yon_desktop_new(){
    desktop_struct *desktop = malloc(sizeof(desktop_struct));
    memset(desktop,0,sizeof(desktop_struct));

    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_desktop);
    desktop->MainChild = yon_gtk_builder_get_widget(builder,"MainChild");
    desktop->NameLabel = yon_gtk_builder_get_widget(builder,"NameLabel");
    desktop->InstallCheck = yon_gtk_builder_get_widget(builder,"InstallCheck");
    desktop->AboutButton = yon_gtk_builder_get_widget(builder,"AboutButton");
    desktop->ScrollRightEventBox = yon_gtk_builder_get_widget(builder,"ScrollRightEventBox");
    desktop->ScrollLeftEventBox = yon_gtk_builder_get_widget(builder,"ScrollLeftEventBox");
    desktop->ArrowsOverlay = yon_gtk_builder_get_widget(builder,"ArrowsOverlay");
    desktop->MainImage = yon_gtk_builder_get_widget(builder,"MainImage");
    desktop->PrevInstallationSliderImage = yon_gtk_builder_get_widget(builder,"PrevInstallationSliderImage");
    desktop->NextInstallationSliderImage = yon_gtk_builder_get_widget(builder,"NextInstallationSliderImage");

    g_object_set_data(G_OBJECT(desktop->MainChild),"desktop_struct",desktop);

    g_signal_connect(G_OBJECT(desktop->ScrollLeftEventBox),"button-press-event",G_CALLBACK(on_desktop_image_prev_clicked),desktop);
    g_signal_connect(G_OBJECT(desktop->ScrollRightEventBox),"button-press-event",G_CALLBACK(on_desktop_image_next_clicked),desktop);

    g_signal_connect(G_OBJECT(desktop->ScrollLeftEventBox),"enter-notify-event",G_CALLBACK(on_install_slider_hover_highlight),desktop);
    g_signal_connect(G_OBJECT(desktop->ScrollRightEventBox),"enter-notify-event",G_CALLBACK(on_install_slider_hover_highlight),desktop);
    g_signal_connect(G_OBJECT(desktop->ScrollLeftEventBox),"leave-notify-event",G_CALLBACK(on_install_slider_hover_highlight_end),desktop);
    g_signal_connect(G_OBJECT(desktop->ScrollRightEventBox),"leave-notify-event",G_CALLBACK(on_install_slider_hover_highlight_end),desktop);

    g_signal_connect(G_OBJECT(desktop->AboutButton),"clicked",G_CALLBACK(on_desktop_about),desktop);

    return desktop;
}

void yon_desktop_init(main_window *widgets){
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->DesktopFlow));
    GList *iter;
    for (iter=list;iter;iter=iter->next){
        desktop_struct *desktop = g_object_get_data(G_OBJECT(iter->data),"desktop_struct");
        gtk_widget_destroy(iter->data);
        free(desktop);
    }
    int size;
    config_str desktop_list = yon_resource_open_file(desktop_path,&size);
    for (int i=1;i<size;i++){
        int parsed_size = 0;
        config_str parsed = yon_char_parse(desktop_list[i],&parsed_size,";");
        if (yon_packages_check_exist(parsed[1])){
            int icons_size = 0;
            config_str icons = yon_char_parse(parsed[2],&icons_size,",");
            yon_char_parsed_prepend_strings(icons,icons_size,"/com/ublinux/image/");
            desktop_struct *desktop = yon_desktop_new();
            desktop->name = yon_char_new(parsed[0]);
            desktop->package = yon_char_new(parsed[1]);
            desktop->image_paths = icons;
            desktop->image_size = icons_size;
            desktop->initially_installed = yon_packages_check_installed(desktop->package);
            gtk_flow_box_insert(GTK_FLOW_BOX(widgets->DesktopFlow),desktop->MainChild,-1);
            yon_desktop_update(desktop);

            g_signal_connect(G_OBJECT(desktop->InstallCheck),"toggled",G_CALLBACK(on_desktop_checked),widgets);
        }
        yon_char_parsed_free(parsed,parsed_size);
    }
}

config_str yon_desktop_get_install_packages(int *size, main_window *widgets){
    (*size)=0;
    config_str packages = NULL;
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->DesktopFlow));
    GList *iter=NULL;
    for (iter=list;iter;iter=iter->data){
        desktop_struct *desktop = g_object_get_data(G_OBJECT(iter->data),"desktop_struct");
        int status = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(desktop->InstallCheck));
        if (status&&!desktop->initially_installed){
            yon_char_parsed_add_or_create_if_exists(packages,size,desktop->package);
        }
    }
    return packages;
}

config_str yon_desktop_get_remove_packages(int *size, main_window *widgets){
    (*size)=0;
    config_str packages = NULL;
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->DesktopFlow));
    GList *iter=NULL;
    for (iter=list;iter;iter=iter->data){
        desktop_struct *desktop = g_object_get_data(G_OBJECT(iter->data),"desktop_struct");
        int status = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(desktop->InstallCheck));
        if (!status&&desktop->initially_installed){
            yon_char_parsed_add_or_create_if_exists(packages,size,desktop->package);
        }
    }
    return packages;
}

long yon_desktop_get_size(main_window *widgets){
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->DesktopFlow));
    GList *iter;
    long overall_size = 0;
    for (iter=list;iter;iter=iter->next){
        desktop_struct *desktop = g_object_get_data(G_OBJECT(iter->data),"desktop_struct");
        int status = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(desktop->InstallCheck));
        long size_raw = yon_packages_get_installed_size(YON_PACKAGES_SYNC,desktop->package);
        if (status&&!desktop->initially_installed)
            overall_size += size_raw;
        else if (!status&&desktop->initially_installed)
            overall_size -= size_raw;

    }
    return overall_size;
}