/*
 * Audacious: A cross-platform multimedia player
 * Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; under version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses>.
 *
 * The Audacious team does not consider modular code linking to
 * Audacious or using our public API to be a derived work.
 */

#include <gtk/gtkmain.h>
#include <glib-object.h>
#include <glib/gmacros.h>
#include <gtk/gtkmarshal.h>
#include <gtk/gtkwindow.h>
#include <string.h>
#include <audlegacy/plugin.h>
#include "platform/smartinclude.h"
#include "ui_skin.h"
#include "skins_cfg.h"
#include "ui_dock.h"
#include "ui_skinned_window.h"
#include "ui_main.h"
#include "ui_playlist.h"

static void ui_skinned_window_class_init(SkinnedWindowClass *klass);
static void ui_skinned_window_init(GtkWidget *widget);
static GtkWindowClass *parent = NULL;

GType
ui_skinned_window_get_type(void)
{
  static GType window_type = 0;

  if (!window_type)
    {
      static const GTypeInfo window_info =
      {
        sizeof (SkinnedWindowClass),
        NULL,           /* base_init */
        NULL,           /* base_finalize */
        (GClassInitFunc) ui_skinned_window_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (SkinnedWindow),
        0,              /* n_preallocs */
        (GInstanceInitFunc) ui_skinned_window_init
      };

      window_type =
        g_type_register_static (GTK_TYPE_WINDOW, "SkinnedWindow",
                                &window_info, 0);
    }

  return window_type;
}

static void
ui_skinned_window_map(GtkWidget *widget)
{
    (* GTK_WIDGET_CLASS (parent)->map) (widget);

    SkinnedWindow *window = SKINNED_WINDOW(widget);
    if (window->type == WINDOW_MAIN)
        gtk_widget_shape_combine_mask(widget, skin_get_mask(aud_active_skin, SKIN_MASK_MAIN + config.player_shaded), 0, 0);
    else if (window->type == WINDOW_EQ)
        gtk_widget_shape_combine_mask(widget, skin_get_mask(aud_active_skin, SKIN_MASK_EQ + config.equalizer_shaded), 0, 0);

    gtk_window_set_keep_above(GTK_WINDOW(widget), config.always_on_top);
}

static gboolean
ui_skinned_window_motion_notify_event(GtkWidget *widget,
                                      GdkEventMotion *event)
{
    if (dock_is_moving(GTK_WINDOW(widget)))
        dock_move_motion(GTK_WINDOW(widget), event);

    return FALSE;
}

static gboolean ui_skinned_window_focus_in(GtkWidget *widget, GdkEventFocus *focus) {
    gboolean val = GTK_WIDGET_CLASS (parent)->focus_in_event (widget, focus);
    gtk_widget_queue_draw(widget);
    return val;
}

static gboolean ui_skinned_window_focus_out(GtkWidget *widget, GdkEventFocus *focus) {
    gboolean val = GTK_WIDGET_CLASS (parent)->focus_out_event (widget, focus);
    gtk_widget_queue_draw(widget);
    return val;
}

static gboolean ui_skinned_window_button_press(GtkWidget *widget, GdkEventButton *event) {
    if (event->button == 1 && event->type == GDK_BUTTON_PRESS &&
        (config.easy_move || config.equalizer_shaded || (event->y / config.scale_factor) < 14)) {
         dock_move_press(get_dock_window_list(), GTK_WINDOW(widget),
                         event, SKINNED_WINDOW(widget)->type == WINDOW_MAIN ? TRUE : FALSE);
    }

    return TRUE;
}

static gboolean ui_skinned_window_button_release(GtkWidget *widget, GdkEventButton *event) {
    if (dock_is_moving(GTK_WINDOW(widget)))
       dock_move_release(GTK_WINDOW(widget));

    return TRUE;
}

static gboolean ui_skinned_window_expose(GtkWidget *widget, GdkEventExpose *event) {
    SkinnedWindow *window = SKINNED_WINDOW(gtk_widget_get_parent(widget));

    GdkPixbuf *obj = NULL;

    gint width = 0, height = 0;
    switch (window->type) {
        case WINDOW_MAIN:
            width = aud_active_skin->properties.mainwin_width;
            height = aud_active_skin->properties.mainwin_height;
            break;
        case WINDOW_EQ:
            width = 275 * (config.scaled ? config.scale_factor : 1);
            height = 116 * (config.scaled ? config.scale_factor : 1) ;
            break;
        case WINDOW_PLAYLIST:
            width = playlistwin_get_width();
            height = config.playlist_height;
            break;
        default:
            return FALSE;
    }
    obj = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);

    gboolean focus = gtk_window_has_toplevel_focus(GTK_WINDOW(window));

    switch (window->type) {
        case WINDOW_MAIN:
            skin_draw_pixbuf(widget, aud_active_skin, obj,SKIN_MAIN, 0, 0, 0, 0, width, height);
            skin_draw_mainwin_titlebar(aud_active_skin, obj, config.player_shaded, focus || !config.dim_titlebar);
            break;
        case WINDOW_EQ:
            skin_draw_pixbuf(widget, aud_active_skin, obj, SKIN_EQMAIN, 0, 0, 0, 0, width, height);
            if (focus || !config.dim_titlebar) {
                if (!config.equalizer_shaded)
                    skin_draw_pixbuf(widget, aud_active_skin, obj, SKIN_EQMAIN, 0, 134, 0, 0, width, 14);
                else
                    skin_draw_pixbuf(widget, aud_active_skin, obj, SKIN_EQ_EX, 0, 0, 0, 0, width, 14);
            } else {
                if (!config.equalizer_shaded)
                    skin_draw_pixbuf(widget, aud_active_skin, obj, SKIN_EQMAIN, 0, 149, 0, 0, width, 14);
                else
                    skin_draw_pixbuf(widget, aud_active_skin, obj, SKIN_EQ_EX, 0, 15, 0, 0, width, 14);
            }
            break;
        case WINDOW_PLAYLIST:
            focus |= !config.dim_titlebar;
            if (config.playlist_shaded) {
                skin_draw_playlistwin_shaded(aud_active_skin, obj, width, focus);
            } else {
                skin_draw_playlistwin_frame(aud_active_skin, obj, width, config.playlist_height, focus);
            }
            break;
    }

    ui_skinned_widget_draw(widget, obj, width, height,
                           window->type != WINDOW_PLAYLIST && config.scaled);

    g_object_unref(obj);

    return FALSE;
}

static void
ui_skinned_window_class_init(SkinnedWindowClass *klass)
{
    GtkWidgetClass *widget_class;

    widget_class = (GtkWidgetClass*) klass;

    parent = g_type_class_peek_parent(klass);

    widget_class->motion_notify_event = ui_skinned_window_motion_notify_event;
    widget_class->focus_in_event = ui_skinned_window_focus_in;
    widget_class->focus_out_event = ui_skinned_window_focus_out;
    widget_class->button_press_event = ui_skinned_window_button_press;
    widget_class->button_release_event = ui_skinned_window_button_release;
    widget_class->map = ui_skinned_window_map;
}

void
ui_skinned_window_hide(SkinnedWindow *window)
{
    g_return_if_fail(SKINNED_CHECK_WINDOW(window));

    gtk_window_get_position(GTK_WINDOW(window), &window->x, &window->y);
    gtk_widget_hide(GTK_WIDGET(window));
}

void
ui_skinned_window_show(SkinnedWindow *window)
{
    g_return_if_fail(SKINNED_CHECK_WINDOW(window));

    gtk_window_move(GTK_WINDOW(window), window->x, window->y);
    gtk_widget_show_all(GTK_WIDGET(window));
}

static void
ui_skinned_window_init(GtkWidget *widget)
{
    SkinnedWindow *window;
    window = SKINNED_WINDOW(widget);
    window->x = -1;
    window->y = -1;
}

GtkWidget *
ui_skinned_window_new(const gchar *wmclass_name)
{
    GtkWidget *widget = g_object_new(ui_skinned_window_get_type(), NULL);
    GtkWindow *window = GTK_WINDOW(widget);

    window->type = SKINNED_WINDOW_TYPE;

    if (wmclass_name)
        gtk_window_set_wmclass(GTK_WINDOW(widget), wmclass_name, "Audacious");

    gtk_widget_add_events(GTK_WIDGET(widget),
                          GDK_FOCUS_CHANGE_MASK | GDK_BUTTON_MOTION_MASK |
                          GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
                          GDK_SCROLL_MASK | GDK_KEY_PRESS_MASK |
                          GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK);
    gtk_widget_realize(GTK_WIDGET(widget));

    set_dock_window_list(dock_window_set_decorated(get_dock_window_list(),
                                                   GTK_WINDOW(widget),
                                                   config.show_wm_decorations));
    gtk_widget_set_app_paintable(GTK_WIDGET(widget), TRUE);
    gdk_window_set_back_pixmap(widget->window, NULL, FALSE);
    gtk_widget_shape_combine_mask(widget, NULL, 0, 0);

    if (!strcmp(wmclass_name, "player"))
        SKINNED_WINDOW(widget)->type = WINDOW_MAIN;
    if (!strcmp(wmclass_name, "equalizer"))
        SKINNED_WINDOW(widget)->type = WINDOW_EQ;
    if (!strcmp(wmclass_name, "playlist"))
        SKINNED_WINDOW(widget)->type = WINDOW_PLAYLIST;

    SKINNED_WINDOW(widget)->normal = gtk_fixed_new();
    gtk_fixed_set_has_window(GTK_FIXED(SKINNED_WINDOW(widget)->normal), TRUE);
    gtk_widget_add_events(SKINNED_WINDOW(widget)->normal, GDK_ALL_EVENTS_MASK);

    SKINNED_WINDOW(widget)->shaded = gtk_fixed_new();
    gtk_fixed_set_has_window(GTK_FIXED(SKINNED_WINDOW(widget)->shaded), TRUE);
    gtk_widget_add_events(SKINNED_WINDOW(widget)->shaded, GDK_ALL_EVENTS_MASK);

    g_object_ref(SKINNED_WINDOW(widget)->normal);
    g_object_ref(SKINNED_WINDOW(widget)->shaded);

    gtk_container_add(GTK_CONTAINER(widget), GTK_WIDGET(SKINNED_WINDOW(widget)->normal));

    gtk_widget_realize(widget);
    gtk_widget_realize(SKINNED_WINDOW(widget)->normal);
    gtk_widget_realize(SKINNED_WINDOW(widget)->shaded);

    g_signal_connect(SKINNED_WINDOW(widget)->normal, "expose-event", G_CALLBACK(ui_skinned_window_expose), NULL);
    g_signal_connect(SKINNED_WINDOW(widget)->shaded, "expose-event", G_CALLBACK(ui_skinned_window_expose), NULL);

    return widget;
}

void ui_skinned_window_draw_all(GtkWidget *widget) {
    if (SKINNED_WINDOW(widget)->type == WINDOW_MAIN)
        mainwin_refresh_hints();

    gtk_widget_queue_draw(widget);
}

void ui_skinned_window_set_shade(GtkWidget *widget, gboolean shaded) {
    if (shaded) {
        gtk_container_remove(GTK_CONTAINER(widget), SKINNED_WINDOW(widget)->normal);
        gtk_container_add(GTK_CONTAINER(widget), SKINNED_WINDOW(widget)->shaded);
        gtk_widget_show_all(SKINNED_WINDOW(widget)->shaded);
    } else {
        gtk_container_remove(GTK_CONTAINER(widget), SKINNED_WINDOW(widget)->shaded);
        gtk_container_add(GTK_CONTAINER(widget), SKINNED_WINDOW(widget)->normal);
        gtk_widget_show_all(SKINNED_WINDOW(widget)->normal);
    }
}
