#include "ubinstall-gtk.h"

void yon_resize_images_update(main_window *widgets){
    enum YON_PAGES page = gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->Notebook));
        gtk_widget_hide(widgets->BootloaderImage);
        gtk_widget_hide(widgets->RegionImage);
        gtk_widget_hide(widgets->KeyboardImage);
        gtk_widget_hide(widgets->UserImage);
        gtk_widget_hide(widgets->StartupImage);
        gtk_widget_hide(widgets->NetworkImage);
    
    switch(page){
        case YON_PAGE_BOOTLOADER:
        gtk_widget_show(widgets->BootloaderImage);
        break;
        case YON_PAGE_REGION:
        gtk_widget_show(widgets->RegionImage);
        break;
        case YON_PAGE_KEYBOARD:
        gtk_widget_show(widgets->KeyboardImage);
        break;
        case YON_PAGE_USERS:
        gtk_widget_show(widgets->UserImage);
        break;
        case YON_PAGE_STARTUP:
        gtk_widget_show(widgets->StartupImage);
        break;
        case YON_PAGE_NETWORK:
        gtk_widget_show(widgets->NetworkImage);
        break;
        case YON_PAGE_INSTALLATION:
            gtk_widget_show(widgets->SlidesImage);
            g_timeout_add(7000,on_image_slide,widgets);
            
        break;
        default:
        break;
    }
}

void on_region_resized(GtkWidget *,main_window *widgets){
    enum YON_PAGES page = gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->Notebook));
    GtkImage *target = NULL;
    GdkPixbuf *pixbuf = NULL;
    switch (page){
        case YON_PAGE_BOOTLOADER:
            target = GTK_IMAGE(widgets->BootloaderImage);
            pixbuf = widgets->bootloader_original;
        break;
        case YON_PAGE_REGION:
            target = GTK_IMAGE(widgets->RegionImage);
            pixbuf = widgets->region_original;
            
        break;
        case YON_PAGE_KEYBOARD:
            target = GTK_IMAGE(widgets->KeyboardImage);
            pixbuf = widgets->keyboard_original;
            
        break;
        case YON_PAGE_USERS:
            target = GTK_IMAGE(widgets->UserImage);
            pixbuf = widgets->user_original;
            
        break;
        case YON_PAGE_STARTUP:
            target = GTK_IMAGE(widgets->StartupImage);
            pixbuf = widgets->startup_original;
            
        break;
        case YON_PAGE_NETWORK:
            target = GTK_IMAGE(widgets->NetworkImage);
            pixbuf = widgets->network_original;
            
        break;
        case YON_PAGE_INSTALLATION:
            target = GTK_IMAGE(widgets->SlidesImage);
            pixbuf = g_list_nth_data(widgets->slides_original,main_config.cur_slide);
            
            
        break;
        default:
            break;
    }
    if (target){
        yon_image_resize_from_container(target,pixbuf);
    }
}

int yon_image_resize_from_container(GtkImage *target, GdkPixbuf *pixbuf_unscaled){
    int width = gtk_widget_get_allocated_width((gtk_widget_get_parent(GTK_WIDGET(target))));
    int height = gtk_widget_get_allocated_height((gtk_widget_get_parent(GTK_WIDGET(target))));

    int newImgWidthDef = (width <= 217) ? width : width - 30;
    int newImgHeightDef = (height <= 120) ? height : height - 80;

    int originalWidth = gdk_pixbuf_get_width(pixbuf_unscaled);
    int originalHeight = gdk_pixbuf_get_height(pixbuf_unscaled);
    int newImgHeight = (int)(originalHeight / ((double) originalWidth / newImgWidthDef));

    if (newImgHeight > newImgHeightDef) {
        newImgHeight = newImgHeightDef;
        newImgWidthDef = (int)(originalWidth / ((double) originalHeight / newImgHeight));
    }
    int newImageWidth = (int)(originalWidth / ((double) originalHeight / newImgHeight));

    GdkPixbuf *scaledPixBuf = gdk_pixbuf_scale_simple(pixbuf_unscaled, newImageWidth, newImgHeight, GDK_INTERP_BILINEAR);
    gtk_image_set_from_pixbuf(target, scaledPixBuf);

    g_object_unref(scaledPixBuf);

    return 1;
}

gboolean on_image_slide(void *data){
    main_window *widgets = (main_window*)data;
    int size;
    config_str target = yon_char_parsed_new(&size,slide_repeat_path);
    if (size)
        yon_char_parsed_free(target,size);
    gtk_image_set_from_pixbuf(GTK_IMAGE(widgets->SlidesImage),(GdkPixbuf*)g_list_nth_data(widgets->slides_original,main_config.cur_slide));
    // gtk_widget_queue_draw(widgets->SlidesImage);
    if ((int)main_config.cur_slide<size-1) main_config.cur_slide++; else main_config.cur_slide=1;
            g_mutex_lock(&main_config.install_mutex);
    if (!main_config.install_complete){
        g_mutex_unlock(&main_config.install_mutex);
        return 1;
    } else{
        g_mutex_unlock(&main_config.install_mutex);
            return 0;
    }
}
