#include <fspanel.h>


//#ifdef HAVE_XPM
#include <X11/xpm.h>
#include "ok.xpm"
//#endif


static int iconifyFromTaskBarFlag = 0;
static int drawIconLightFlag = 1;

applet_info_t info_g;

int start_x_panel_g;

#define TEXTPAD 6
#define ICONWIDTH 16
#define ICONHEIGHT 16
#define MAX_TASK_WIDTH 145

Pixmap check_icon;
Pixmap check_mask;



// ************** TODO .... we need an init function that can be called
//                          for each task .......
void plug_init()
{
//#ifdef HAVE_XPM

    printf("creating a checkmark xpm ....\n");

	XpmCreatePixmapFromData( x_get_display(),
                             x_get_root_window(),
                             ok_xpm,
                             &check_icon,
                             &check_mask,
                             NULL );
//#endif
}


applet_info_t get_applet_info( void )
{  
    return info_g;
}

set_applet_info( applet_info_t inf )
{
    info_g = inf;

    if( info_g.width == -1 )
    {
        info_g.width = 900;
    }

    // temp
    plug_init();
    
}

int event_notification( void *token )
{
    // the token is the task in our case....
    gui_draw_task( (task*) token );
}

int gui_draw( int x, void *token ) 
{
    int taskw;
    task *tk;
    int under = 0;
    int initial_x = x;
    
    int width = info_g.width;
    start_x_panel_g = x;
    
	if ( panel_get_taskcount() == 0 )
    {
		goto clear;
    }

    // double line at the left edge
    gui_draw_vline ( x );
    gui_draw_vline ( x + 2 );
 
    x += 2;    
	taskw = (width - 6) / panel_get_taskcount();
	if (taskw > MAX_TASK_WIDTH)
	{
		taskw = MAX_TASK_WIDTH;
		under = 1;
	}    

    
	tk = (task *) token;
	while (tk)
	{
		tk->pos_x = x;
		tk->width = taskw - 1;

        // bva - little look ahead. If this is the last
        //       task, wouldn't it be nice to make sure it
        //       fills to the entire width ? This makes the right
        //       edge look better
        if( tk->next == NULL )
        {
            // VODO! - I'm adding initial_x to make things relative to where
            //         the applet is in the bar. We've been accumlating "x", but
            //         we didn't start at zero, we started at "initial_x", so
            //         we'll have to add it back on. The "- 3" is to allow space
            //         on the end to draw our double line.
            tk->width = width + initial_x - x - 3;

            // but not if it makes us *really* *really* big ...
            if( tk->width > MAX_TASK_WIDTH )
            {
                tk->width = MAX_TASK_WIDTH - 1;
            }
        }
        
		if( gui_draw_task ( tk ) )
        {
            x += taskw;
        }
		tk = tk->next;
	}

	if (under)
	{
clear:
		gui_draw_vline ( x);
		set_foreground (0);
		fill_rect ( x + 2, 0, panel_get_winwidth(), panel_get_winheight() );
	}


    // double line at the right edge
    set_foreground (5);
    gui_draw_vline ( initial_x + width - 2 );
    gui_draw_vline ( initial_x + width );

    // the +1 is for padding ...
    return initial_x + width + 1;
}

int gui_draw_task ( task * tk )
{
	int len;
	int x = tk->pos_x;
	int taskw = tk->width;
    int winheight;
    
    winheight = panel_get_winheight();
    
	if (!tk->name)
    {
		return 0;
    }

// bva 
//    if( onlyMinmizedAppsFlag && !is_iconified( tk->win ) )
//    {
//        return 0;
//    }

	gui_draw_vline ( x );

/*set_foreground (3); *//* it's already 3 from gui_draw_vline() */
	draw_line ( x + 1, 0, x + taskw, 0);

	set_foreground (1);
	draw_line ( x + 1, winheight - 1, x + taskw, winheight - 1);

	if (tk->focused)
	{
		x++;
		/*set_foreground (1);*/		  /* mid gray */
		fill_rect (  x + 3, 3, taskw - 5, winheight - 6);
		set_foreground (3);		  /* white */
		draw_line ( x + 2, winheight - 2, x + taskw - 2, winheight - 2);
		draw_line ( x + taskw - 2, 2, x + taskw - 2, winheight - 2);
		set_foreground (0);
		draw_line ( x + 1, 2, x + 1, winheight - 2);
		set_foreground (4);		  /* darkest gray */
		draw_line ( x + 2, 2, x + taskw - 2, 2);
		draw_line ( x + 2, 2, x + 2, winheight - 3);
	}
    else
	{
		set_foreground( 0 );		  /* mid gray */
		fill_rect ( x + 2, 1, taskw - 1, winheight - 2);
	}

	{
		register int text_x = x + TEXTPAD + TEXTPAD + ICONWIDTH;

		/* check how many chars can fit */
		len = strlen (tk->name);
		while (XTextWidth (x_get_fontstruct(), tk->name, len) >= taskw - (text_x - x) - 2
				 && len > 0)
			len--;

        if( drawIconLightFlag )
        {
            // bva - this is the original mode !
            if ( tk->iconified )
            {
                /* draw task's name dark (iconified) */
                set_foreground ( 3 );
                gui_draw_string( tk->name, len, text_x, panel_get_y_text_origin() + 1 );

                set_foreground ( 4 );
            }
            else
            {
                set_foreground ( 5 );
            }
        }
        else
        {
            if ( !tk->iconified )
            {
                /* draw task's name dark (iconified) */
                set_foreground ( 3 );

                gui_draw_string( tk->name, len, text_x, panel_get_y_text_origin() + 1 );

                set_foreground ( 4 );
            }
            else
            {
                set_foreground ( 5 );
            }
        }

		/* draw task's name here */
        gui_draw_string( tk->name, len, text_x, panel_get_y_text_origin() );
	}

#ifndef HAVE_XPM
	if (!tk->icon)
    {
		return;
    }
#endif
    
	/* draw the task's icon */
    if( tk->marked == 1 )
    {
        XSetClipMask( x_get_display(), x_get_foregroundGC(), check_mask ); // tk->mask );
        XSetClipOrigin( x_get_display(),
                        x_get_foregroundGC(),
                        x + TEXTPAD,
                        (winheight - ICONHEIGHT) / 2 );
        XCopyArea ( x_get_display(),
                    check_icon, // tk->icon,
                    x_get_window(),
                    x_get_foregroundGC(),
                    0,
                    0,
                    ICONWIDTH,
                    ICONHEIGHT,
                    x + TEXTPAD,
                    (winheight - ICONHEIGHT) / 2 );
    }
    else
    {
        XSetClipMask( x_get_display(), x_get_foregroundGC(),tk->mask );
        XSetClipOrigin( x_get_display(),
                        x_get_foregroundGC(),
                        x + TEXTPAD,
                        (winheight - ICONHEIGHT) / 2 );
        XCopyArea ( x_get_display(),
                     tk->icon,
                    x_get_window(),
                    x_get_foregroundGC(),
                    0,
                    0,
                    ICONWIDTH,
                    ICONHEIGHT,
                    x + TEXTPAD,
                    (winheight - ICONHEIGHT) / 2 );
    }
    
	XSetClipMask( x_get_display(), x_get_foregroundGC(), None );

    return 1;
}

int gui_button_press ( int x, int y, int button, void *token )
{        
    task *tk;    

    printf("pressed with: %d\n", x );

    x = x + start_x_panel_g;
    
    // our token is the task list ...
    tk = (task *) token;
    while (tk)
    {
        if (x > tk->pos_x && x < tk->pos_x + tk->width)
        {
            // bva
            if( button == 3 && tk->marked == 0 )
            {
                printf("marking a task\n");
                tk->marked = 1;
            }
            
            if ( tk->iconified )
            {
                tk->iconified = 0;
                tk->focused = 1;
                XMapWindow ( x_get_display(), tk->win);
            }
            else
            {
                printf("bva - button press on a non-icon task\n");                
                                
                if( tk->focused )
                {
                    printf("\tThe task was already focussed\n");
                                    
                    // this will be configurable via the command lone
                    if( iconifyFromTaskBarFlag )
                    {
                        printf("\t\ticonifying\n");
                        tk->iconified = 1;
                        tk->focused = 0;
                        XIconifyWindow (x_get_display(), tk->win, x_get_screen() );
                    }
                    else
                    {
                        // this doesn't seem to be helping ...
                        XSetInputFocus (x_get_display(), tk->win, RevertToNone, CurrentTime);
                    }
                                    
                }                
                else
                {
                    printf("\tThe task wasn't focussed\n");
                                    
                    if( is_shaded( tk->win) ) // tk->shaded )
                    {
                        // we want to unshade the window. In the future this may
                        // only need to be done if a special button was used ...
                        printf("\tthe task was shaded\n");
                        tk->focused = 1;
                                        
                        XMapWindow (x_get_display(), tk->win);
                        XRaiseWindow (x_get_display(), tk->win);
                        XSetInputFocus (x_get_display(), tk->win, RevertToNone, CurrentTime);
                                        
                    }
                    else
                    {
                        printf("\tthe task wasn't shaded\n");
                        tk->focused = 1;
                        XRaiseWindow (x_get_display(), tk->win);
                        XSetInputFocus (x_get_display(), tk->win, RevertToNone, CurrentTime);
                    }
                }
            }
            gui_sync ();
            gui_draw_task ( tk );
        }
        else
        {
            if (tk->focused)
            {
                tk->focused = 0;
                gui_draw_task ( tk);
            }
        }
                        
        tk = tk->next;
    }
}

int gui_button_release ( int x, int y, int button, void *token )
{

}
