#include <fspanel.h>

int menu_is_toggle_flag = 1;

Window menu;
applet_info_t info_g;


applet_info_t get_applet_info( void )
{    
    return info_g;
}

void set_applet_info( applet_info_t inf )
{
    info_g = inf;

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

Window gui_create_menu_window ()
{
	Window win;
	MWMHints mwm;
	XSizeHints size_hints;
	XWMHints wmhints;
	XSetWindowAttributes att;
    int winheight = panel_get_winheight();
    
	att.background_pixel = panel_get_palette( 0 );
	att.event_mask = ButtonPressMask | ExposureMask | ButtonReleaseMask;

	win = XCreateWindow (
                          /* display */ x_get_display(),
                          /* parent  */ x_get_root_window(), 
                          /* x       */ 6,
                          /* y       */ x_get_screen_height() - winheight - 100,
                          /* width   */ 100, // WINWIDTH,
                          /* height  */ winheight + 100 - winheight,
                          /* border  */ 0,
                          /* depth   */ CopyFromParent,
                          /* class   */ InputOutput,
                          /* visual  */ CopyFromParent,
                          /*value mask*/ CWBackPixel | CWEventMask,
                          /* attribs */ &att);

	/* don't let any windows cover fspanel */
	set_prop (win, wm_get_winlayer_atom, 10);	/* WIN_LAYER_ABOVE_DOCK */

    // bva - TODO .. prevent the window from taking any focus or becoming
    //               the active window.
    
	set_prop ( win,
               wm_get_winstate_atom(),
               WIN_STATE_STICKY |
               WIN_STATE_FIXED_POSITION );

	set_prop ( win,
               wm_get_winhints_atom(),
               WIN_HINTS_SKIP_FOCUS |
               WIN_HINTS_SKIP_WINLIST |
               WIN_HINTS_SKIP_TASKBAR |
               WIN_HINTS_DO_NOT_COVER );
    
    
	/* borderless motif hint */
	memset (&mwm, 0, sizeof (mwm));
	mwm.flags = MWM_HINTS_DECORATIONS;
	XChangeProperty ( x_get_display(),
                      win,
                      wm_get_mwmhints_atom(),
                      wm_get_mwmhints_atom(),
                      32,
                      PropModeReplace,
                      (unsigned char *) &mwm,
                      sizeof (MWMHints) / 4 );
    
	/* make sure the WM obays our window position */
	size_hints.flags = PPosition;
	/*XSetWMNormalHints (x_get_display(), win, &size_hints);*/
	XChangeProperty ( x_get_display(), win, XA_WM_NORMAL_HINTS,
                      XA_WM_SIZE_HINTS, 32, PropModeReplace,
                      (unsigned char *) &size_hints, sizeof (XSizeHints) / 4);

	/* make our window unfocusable */
	wmhints.flags = InputHint;
	wmhints.input = 0;
	/*XSetWMHints (x_get_display(), win, &wmhints);*/
	XChangeProperty ( x_get_display(), win, XA_WM_HINTS,
                      XA_WM_HINTS, 32, PropModeReplace,
                      (unsigned char *) &wmhints, sizeof (XWMHints) / 4);
    
	XMapWindow (x_get_display(), win);

//	tb = calloc (1, sizeof (taskbar));
//	tb->win = win;

	return win;
}

int gui_draw( int x, void *token )
{
    int winheight = panel_get_winheight();
    int width = info_g.width;

    set_foreground (0);
    fill_rect ( x, 0, width, winheight);
    
    //XUnmapWindow( x_get_display(), menu );
    
    //toggle_state = 0;          

    return x + width;
}

int gui_button_press ( int x, int y, int button, void *token )
{
    int winheight = panel_get_winheight();
    static int windowCreated = 0;
    int width = info_g.width;
    
    set_foreground (1);
    // draw_line (tb, x + 1, winheight - 1, x + width, winheight - 1);
    
    //x++;
    /*set_foreground (1);*/		  /* mid gray */
    fill_rect ( x + 3, 3, width - 5, winheight - 6);
    set_foreground (3);		  /* white */
    draw_line ( x + 2, winheight - 2, x + width - 2, winheight - 2);
    draw_line ( x + width - 2, 2, x + width - 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 + width - 2, 2);
    draw_line ( x + 2, 2, x + 2, winheight - 3);
                        
    // ----- the menu ! --------
    if( windowCreated == 0 )
    {
        menu = gui_create_menu_window();
        windowCreated = 1;
    }
    else
    {
        XMapWindow ( x_get_display(), menu );
    }
            
}

int gui_button_release ( int x, int y, int button, void *token )
{
    int winheight = panel_get_winheight();
    int width = info_g.width;

    printf("gui_button_release .. in menu\n");
    
    // button release
    if( !menu_is_toggle_flag )
    {
        set_foreground (0);
        fill_rect ( x, 0, width, winheight );
        
        XUnmapWindow( x_get_display(), menu );
    }
}


int event_notification( void *token )
{

}
