#include "mcdplayer.h"
#include "mcdplayer_gui.h"

struct Gadget *MainGadget[31];

WORD gad_x[] = { 267, 288, 309, 330, 351, 267, 288, 309, 330, 351, 267, 288, 309, 330, 351,
                 267, 288, 309, 330, 351, 267, 302, 336,   7,  52,  77, 102, 137, 172, 207,
                 242 };

WORD gad_y[] = {   6,   6,   6,   6,   6,  18,  18,  18,  18,  18,  30,  30,  30,  30,  30,
                  42,  42,  42,  42,  42,  53,  53,  53,  65,  65,  65,  65,  65,  65,  65,
                  65 };

WORD gad_w[] = {  19,  19,  19,  19,  19,  19,  19,  19,  19,  19,  19,  19,  19,  19,  19,
                  19,  19,  19,  19,  19,  34,  33,  34,  44,  24,  24,  34,  34,  34,  34,
                  23 };

WORD gad_h[] = {   9,   9,   9,   9,   9,   9,   9,   9,   9,   9,   9,   9,   9,   9,   9,
                   9,   9,   9,   9,   9,  11,  11,  11,  13,  13,  13,  13,  13,  13,  13,
                  13 };

UWORD offy = 0;

struct TagItem    MainWindowTags[] = {
    WA_Left,          0,
    WA_Top,           0,
    WA_Width,         390,
    WA_Height,        0,
    WA_IDCMP,         IDCMP_GADGETUP|IDCMP_REFRESHWINDOW|IDCMP_CLOSEWINDOW,
    WA_Flags,         WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH|WFLG_ACTIVATE,
    WA_Gadgets,       0l,
    WA_Title,         "MCDPlayer V" VERSION,
    WA_ScreenTitle,   0l,
    WA_PubScreen,     NULL,
    TAG_DONE };

struct Image MainImage = {
    0,0,382,84,2,
    NULL,
    0x3,0x0,NULL
    };

struct Image PfeilImage = {
    0,0,9,5,2,
    NULL,
    0x3,0x0,NULL
    };

struct Image PauseImage = {
    0,0,9,5,2,
    NULL,
    0x3,0x0,NULL
    };

struct Image PrgmImage = {
    0,0,21,5,2,
    NULL,
    0x3,0x0,NULL
    };

char FontName[130];

struct TextAttr DispFont = {
    &FontName[0], 10, 0x00, 0x01 };


struct Window *MainWnd = NULL;
struct TextFont *DFont = NULL;
UWORD *MW_Data;

struct EasyStruct ErrorES = {
    sizeof (struct EasyStruct),
    0,
    0l,
    "%s",
    0l,
    };

void PlaceWnd( struct Screen *Scr, WORD WndWidth, WORD WndHeight, WORD *x_pos, WORD *y_pos, char Place )
    {
    ULONG             ModeID;
    struct Rectangle  OSRect;
    WORD              ScrWidth = 0, ScrHeight = 0;

    if (INVALID_ID != (ModeID = GetVPModeID(&(Scr->ViewPort) ) ) )
        {
        if (QueryOverscan (ModeID, &OSRect, OSCAN_TEXT) )
            {
            ScrWidth = OSRect.MaxX - OSRect.MinX + 1;
            ScrHeight = OSRect.MaxY - OSRect.MinY + 1;
            };
        };
    switch (Place)
        {
        case 0:
            *x_pos = (ScrWidth/2 - WndWidth/2) - Scr->LeftEdge;
            *y_pos = (ScrHeight/2 - WndHeight/2) - Scr->TopEdge;
            break;
        case 1:
            *x_pos = Scr->Width - WndWidth;
            *y_pos = Scr->BarHeight + 1;
            break;
        case 2:
            *x_pos = 0;
            *y_pos = Scr->BarHeight + 1;
            break;
        case 3:
            *x_pos = 0;
            *y_pos = Scr->Height - WndHeight;
            break;
        case 4:
            *x_pos = Scr->Width - WndWidth;
            *y_pos = Scr->Height - WndHeight;
            break;
        }
    }

void DrawPlaySymb( int stat )
    {
    switch( stat )
        {
        case 1:
            if (MW_Data) DrawImage( MainWnd->RPort,&PfeilImage, 248, 65+offy );
            break;
        case 0:
            SetAPen( MainWnd->RPort, 1 );
            RectFill( MainWnd->RPort, 248, 65+offy, 262, 70+offy );
            break;
        default:
            if (MW_Data) DrawImage( MainWnd->RPort,&PauseImage, 248, 65+offy );
            break;
        }
    }

void DrawPrgmSymb( int stat )
    {
    if (stat)
        {
        if (MW_Data) DrawImage( MainWnd->RPort,&PrgmImage, 218, 65+offy );
        }
    else
        {
        SetAPen( MainWnd->RPort, 1 );
        RectFill( MainWnd->RPort, 218, 65+offy, 240, 70+offy );
        }
    }

ErrorMsg(char *Msg)
    {
    ErrorES.es_Title = "MCDP Requester:";
    ErrorES.es_GadgetFormat = "Okay";

    EasyRequest( MainWnd, &ErrorES, 0, Msg );
    }

void CleanMainWnd()
    {
    if (MW_Data) DrawImage( MainWnd->RPort,&MainImage, 5, 12+offy );
    }

int OpenMainWnd( char Place, int xpos, int ypos )
    {
    struct Screen *Scr;
    UWORD i;
    WORD x,y;

    int Exit = 0;

    if (Scr = LockPubScreen( 0 ))
        {
        offy = Scr->WBorTop + Scr->RastPort.TxHeight - 10;
        MainWindowTags[ 9 ].ti_Data = Scr;
        PlaceWnd( Scr, 390, 98+offy, &x, &y, Place );
        if(xpos != -1) x = xpos;
        if(ypos != -1) y = ypos;
        MainWindowTags[ 0 ].ti_Data = x;
        MainWindowTags[ 1 ].ti_Data = y;
        MainWindowTags[ 3 ].ti_Data = 98+offy;
    
        if ( MW_Data = (UWORD *)AllocMem( 8148, MEMF_CHIP ) )
            {
            memcpy( MW_Data, MainWinData, 8064 );
            memcpy( ((char *)MW_Data)+8064, PfeilData, 20 );
            memcpy( ((char *)MW_Data)+8084, PauseData, 20 );
            memcpy( ((char *)MW_Data)+8108, PrgmData, 40 );
            PfeilImage.ImageData = ((char *)MW_Data)+8064;
            PauseImage.ImageData = ((char *)MW_Data)+8084;
            PrgmImage.ImageData = ((char *)MW_Data)+8108;

            if ( MainWnd = OpenWindowTagList( 0l, MainWindowTags ))
                {
                MainImage.ImageData = MW_Data;
                DrawImage( MainWnd->RPort,&MainImage, 5, 12+offy );

                for (i=0; i<31; i++)
                    {
                    if (MainGadget[i] = (struct Gadget *)AllocMem( sizeof( struct Gadget ), MEMF_CLEAR ))
                        {
                        MainGadget[i]->LeftEdge = gad_x[i]+5;
                        MainGadget[i]->TopEdge  = gad_y[i]+12+offy;
                        MainGadget[i]->Width    = gad_w[i];
                        MainGadget[i]->Height   = gad_h[i];
                        MainGadget[i]->Flags    = GFLG_GADGHCOMP;
                        MainGadget[i]->Activation = GACT_RELVERIFY;
                        MainGadget[i]->GadgetType = GTYP_BOOLGADGET;
                        MainGadget[i]->GadgetID = i;
                        AddGadget( MainWnd, MainGadget[i], ~0 );
                        }
                    else
                        {
                        ErrorMsg( "Problem allocating gadgets!" );
                        Exit = 5;
                        }
                    };
                if (DFont = (struct TextFont *)OpenDiskFont( &DispFont ))
                    {
                    SetFont( MainWnd->RPort, DFont );
                    }
                else
                    {
                    ErrorMsg( "Can't open font." );
                    Exit = 4;
                    }
                }
            else
                {
                ErrorMsg( "Can't open window." );
                Exit = 1;
                }
            }
        else
            {
            ErrorMsg( "Can't allocate memory." );
            Exit = 2;
            }
        UnlockPubScreen( NULL, Scr );
        }
    else
        {
        ErrorMsg( "Can't lock the screen." );
        Exit = 3;
        }
    return ( Exit );
    }

CloseMainWnd()
    {
    UWORD i;

    if (MW_Data) FreeMem( MW_Data, 8148 );
    if (MainWnd) CloseWindow( MainWnd );
    for (i=0; i<31; i++)
         if (MainGadget[i]) FreeMem( MainGadget[i], sizeof( struct Gadget ) );
    if (DFont) CloseFont( DFont );
    }

