#include "ubinstall-gtk.h"

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

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


void on_install_slider_next(GtkWidget* ,GdkEventButton *,main_window *widgets){
    main_config.chosen_slide=main_config.chosen_slide->next?main_config.chosen_slide->next:g_list_first(main_config.chosen_slide);
    widgets->slider_stop=1;
    yon_install_slider_update(widgets);
}

void on_install_slider_prev(GtkWidget* ,GdkEventButton *,main_window *widgets){
    main_config.chosen_slide=main_config.chosen_slide->prev?main_config.chosen_slide->prev:g_list_last(main_config.chosen_slide);
    widgets->slider_stop=1;
    yon_install_slider_update(widgets);
}

void yon_install_slider_update(main_window *widgets){
    slider_object *cur_slider = (slider_object*)main_config.chosen_slide->data;
    slider_object *next_slider = main_config.chosen_slide->next?((slider_object*)main_config.chosen_slide->next->data):(slider_object*)(g_list_first(main_config.chosen_slide)->data);
    GtkWidget *prevleft = gtk_bin_get_child(GTK_BIN(widgets->ScrollLeftEventBox));
    GtkWidget *prevright = gtk_bin_get_child(GTK_BIN(widgets->ScrollRightEventBox));
    if (cur_slider->MainBox==prevleft) return;
    if (prevleft&&prevright) {
        g_object_ref(G_OBJECT(prevleft));
        g_object_ref(G_OBJECT(prevright));
        gtk_container_remove(GTK_CONTAINER(widgets->ScrollLeftEventBox),prevleft);
        gtk_container_remove(GTK_CONTAINER(widgets->ScrollRightEventBox),prevright);
    }
    gtk_container_add(GTK_CONTAINER(widgets->ScrollLeftEventBox),cur_slider->MainBox);
    gtk_container_add(GTK_CONTAINER(widgets->ScrollRightEventBox),next_slider->MainBox);
    gtk_container_check_resize(GTK_CONTAINER(widgets->ScrollLeftEventBox));
    gtk_container_check_resize(GTK_CONTAINER(widgets->ScrollRightEventBox));
    GList *dots = gtk_container_get_children(GTK_CONTAINER(widgets->SliderProgressBox));
    GList *iter;
    for (iter=dots;iter;iter=iter->next){
        gtk_widget_destroy(GTK_WIDGET(iter->data));
    }
    for (iter=main_config.slides;iter;iter=iter->next){
        GtkWidget *Image = NULL;
        int chosen = iter==main_config.chosen_slide||(main_config.chosen_slide->next&&iter==main_config.chosen_slide->next)||(!main_config.chosen_slide->next&&iter==g_list_first(main_config.chosen_slide));
        if (chosen){
            Image = gtk_image_new_from_icon_name(toggled_icon_path,GTK_ICON_SIZE_BUTTON);
            gtk_image_set_pixel_size(GTK_IMAGE(Image),8);
        } else {
            Image = gtk_image_new_from_icon_name(untoggled_icon_path,GTK_ICON_SIZE_BUTTON);
            gtk_image_set_pixel_size(GTK_IMAGE(Image),8);
        }
        gtk_box_pack_start(GTK_BOX(widgets->SliderProgressBox),Image,0,0,0);
        gtk_widget_show(Image);
    }
}

slider_object *yon_slider_object_new(){
    slider_object *slider = malloc(sizeof(slider_object));
    memset(slider,0,sizeof(slider_object));
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_slider);

    slider->MainBox = yon_gtk_builder_get_widget(builder,"MainBox");
    slider->TitleImage = yon_gtk_builder_get_widget(builder,"TitleImage");
    slider->TitleLabel = yon_gtk_builder_get_widget(builder,"TitleLabel");
    slider->TextLabel = yon_gtk_builder_get_widget(builder,"TextLabel");
    g_object_set_data(G_OBJECT(slider->MainBox),"slider_object",slider);
    return slider;
}

void yon_slider_setup(slider_object *slider, char *title, char *icon, char *text_markup){
    gtk_label_set_text(GTK_LABEL(slider->TitleLabel),title);
    gtk_label_set_markup(GTK_LABEL(slider->TextLabel),text_markup);
    gtk_image_set_from_icon_name(GTK_IMAGE(slider->TitleImage),icon,GTK_ICON_SIZE_DND);
}

slider_object *yon_slider_new_full(char *title, char *icon, char *text_markup){
    slider_object *slider = yon_slider_object_new();
    yon_slider_setup(slider,title,icon,text_markup);
    return slider;
}

void yon_install_slider_init(main_window *widgets){
    if (main_config.slides) return;
    slider_object *slider1 = yon_slider_new_full(slide_1_title, icon_path,slide_1_text);
    slider_object *slider2 = yon_slider_new_full(slide_2_title, icon_path,slide_2_text);
    slider_object *slider3 = yon_slider_new_full(slide_3_title, icon_path,slide_3_text);
    slider_object *slider4 = yon_slider_new_full(slide_4_title, icon_path,slide_4_text);
    slider_object *slider5 = yon_slider_new_full(slide_5_title, icon_path,slide_5_text);
    slider_object *slider6 = yon_slider_new_full(slide_6_title, icon_path,slide_6_text);
    slider_object *slider7 = yon_slider_new_full(slide_7_title, icon_path,slide_7_text);
    slider_object *slider8 = yon_slider_new_full(slide_8_title, icon_path,slide_8_text);
    slider_object *slider9 = yon_slider_new_full(slide_9_title, icon_path,slide_9_text);
    slider_object *slider10 = yon_slider_new_full(slide_10_title, icon_path,slide_10_text);
    slider_object *slider11 = yon_slider_new_full(slide_11_title, icon_path,slide_11_text);
    slider_object *slider12 = yon_slider_new_full(slide_12_title, icon_path,slide_12_text);
    slider_object *slider13 = yon_slider_new_full(slide_13_title, icon_path,slide_13_text);
    slider_object *slider14 = yon_slider_new_full(slide_14_title, icon_path,slide_14_text);
    slider_object *slider15 = yon_slider_new_full(slide_15_title, icon_path,slide_15_text);
    slider_object *slider16 = yon_slider_new_full(slide_16_title, icon_path,slide_16_text);
    slider_object *slider17 = yon_slider_new_full(slide_17_title, icon_path,slide_17_text);
    slider_object *slider18 = yon_slider_new_full(slide_18_title, icon_path,slide_18_text);
    slider_object *slider19 = yon_slider_new_full(slide_19_title, icon_path,slide_19_text);
    slider_object *slider20 = yon_slider_new_full(slide_20_title, icon_path,slide_20_text);
    slider_object *slider21 = yon_slider_new_full(slide_21_title, icon_path,slide_21_text);
    slider_object *slider22 = yon_slider_new_full(slide_22_title, icon_path,slide_22_text);
    main_config.slides = g_list_prepend(main_config.slides,slider22);
    main_config.slides = g_list_prepend(main_config.slides,slider21);
    main_config.slides = g_list_prepend(main_config.slides,slider20);
    main_config.slides = g_list_prepend(main_config.slides,slider19);
    main_config.slides = g_list_prepend(main_config.slides,slider18);
    main_config.slides = g_list_prepend(main_config.slides,slider17);
    main_config.slides = g_list_prepend(main_config.slides,slider16);
    main_config.slides = g_list_prepend(main_config.slides,slider15);
    main_config.slides = g_list_prepend(main_config.slides,slider14);
    main_config.slides = g_list_prepend(main_config.slides,slider13);
    main_config.slides = g_list_prepend(main_config.slides,slider12);
    main_config.slides = g_list_prepend(main_config.slides,slider11);
    main_config.slides = g_list_prepend(main_config.slides,slider10);
    main_config.slides = g_list_prepend(main_config.slides,slider9);
    main_config.slides = g_list_prepend(main_config.slides,slider8);
    main_config.slides = g_list_prepend(main_config.slides,slider7);
    main_config.slides = g_list_prepend(main_config.slides,slider6);
    main_config.slides = g_list_prepend(main_config.slides,slider5);
    main_config.slides = g_list_prepend(main_config.slides,slider4);
    main_config.slides = g_list_prepend(main_config.slides,slider3);
    main_config.slides = g_list_prepend(main_config.slides,slider2);
    main_config.slides = g_list_prepend(main_config.slides,slider1);
    main_config.slides = g_list_reverse(main_config.slides);
    main_config.chosen_slide = g_list_first(main_config.slides);
    yon_install_slider_update(widgets);
    gtk_overlay_add_overlay(GTK_OVERLAY(widgets->InstallationSliderArrowsOverlay),widgets->NextInstallationSliderImage);
    gtk_overlay_add_overlay(GTK_OVERLAY(widgets->InstallationSliderArrowsOverlay),widgets->PrevInstallationSliderImage);
    g_timeout_add_seconds(5,(GSourceFunc)on_install_slider_slide,widgets);
}

gboolean on_install_slider_slide_proceed(main_window *widgets){
    main_config.chosen_slide=main_config.chosen_slide->next?main_config.chosen_slide->next:g_list_first(main_config.chosen_slide);
    yon_install_slider_update(widgets);
    return G_SOURCE_REMOVE;
}

gboolean on_install_slider_slide(main_window *widgets){
    if (!widgets->slider_stop){
        g_idle_add((GSourceFunc)on_install_slider_slide_proceed,widgets);
        return G_SOURCE_CONTINUE;
    }
    return G_SOURCE_REMOVE;
}

// 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;
//     target = GTK_IMAGE(widgets->RegionImage);
//     pixbuf = widgets->region_original;
//     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;
// }