/*  mp3player hardware test

    15.05.2001: tk, initial implementation.

    Copyright (c)2000 by Thomas Kindler, thomas.kindler@gmx.de

    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; either version 2 of
    the License, or (at your option) any later version. Read the
    full License at http://www.gnu.org/copyleft for more details.
*/

// include files -----
//
#include <mem.h>
#include <malloc.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <string.h>
#include <io.h>
#include <stdlib.h>
#include "misc.h"
#include "ztimer.h"
#include "vs1001.h"
#include "checkbios.h"
#include "gfxcore.h"
#include "gfxdriver.h"
#include "gfxutil.h"
#include "gfxfont.h"
#include "remote.h"
#include "ztimer.h"
#include "clib/rtos.h"
#include "clib/cgiapi.h"


void  gfxTest()
{
  Bitmap *screen = BM_Alloc(122, 32);
//Bitmap *logo   = BM_Load("test.bmp");

  Graphics  gfx;
  GFX_Init(&gfx, screen);

  BM_Clear(screen);

  int x=0, y=24, u=33, v=0, w=122, h=8, op = 0;

/*  while (true) {
    char c = getch();
    char text[2] = {c, 0};

    if (c != 0) {
      if (c==27)
        break;
      if (c==13 || gfx.x + GFX_TextWidth(&gfx, text) >= screen->width)
        GFX_MoveTo(&gfx, 0, gfx.y + font->height+1);

      int bottom = gfx.y+font->height;
      if (bottom >= screen->height) {
        BM_Scroll(screen, 0, bottom - screen->height);
        GFX_MoveRel(&gfx, 0, - (bottom - screen->height));
      }

      GFX_Print(&gfx, text);
      LCD_Update(screen);
    }
  } */


  while (!kbhit()) {
    long key = RC_GetKey();
//    printf("%c", 12);
    switch (key & ~RCKEY_REPEAT) {
      case 0x10d52a10:   return;

      case 0x10d52a14:   x--; break;
      case 0x10d52a02:   x++; break;
      case 0x10d52a18:   y--; break;
      case 0x10d52a01:   y++; break;

      case 0x10d52a17:   u--; break;
      case 0x10d52a08:   u++; break;
      case 0x10d52a04:   v--; break;
      case 0x10d52a07:   v++; break;

      case 0x10d52a1a:   w--; break;
      case 0x10d52a03:   w++; break;
      case 0x10d52a06:   h--; break;
      case 0x10d52a09:   h++; break;

      case 0x10d52a13:   op++; break;

      case 0x10d52a19:
        Bitmap *foo;
        foo = BM_Alloc(screen->width+8, screen->height+8);
        BM_Clear(foo);
        BM_Blit(foo, 4, 4, screen);
        BM_Save(foo, "www\\shot.bmp");
        BM_Free(foo);
        break;

      default:
        printf("unkown code: 0x%lx\n", key);
        break;
    }
    BM_Clear(screen);

//    font->firstChar = u;
//    printf("%d\n", u);

    GFX_PrintAt(&gfx, 0,  0, "PalmOS Fonts rul0r3n!");
    GFX_PrintAt(&gfx, 0,  8, "K/oS MP3 Player v2");
    GFX_PrintfAt(&gfx, 0, 16, "www.kreapc.de op=%d", op);
/*    op &= 15;
    BM_Blit(screen, x, y, logo, u, v, -1, -1, op);
*/

/*    GFX_SetFillStyle(&gfx, xHatchFill);
    ZTimer_Start();
    GFX_FillRect(&gfx, x, y, w, h);
    int time1 = ZTimer_Stop();
    GFX_MoveTo(&gfx, 0, 0);
    GFX_Printf(&gfx, "%d,%d,%d,%d: %d us ", x, y, w, h, time1);
*/

/*    GFX_SetColor(&gfx, 0);
    GFX_SetBlitMode(&gfx, BLT_NOTCOPY);
    BM_Clear(screen, 0xff);
*/
/*    if (w&1)
      GFX_LineX(&gfx, 1, 1, 120, 30);
    else
      GFX_LineX(&gfx, x, y, x+u, y+v);
*/

//    GFX_RoundRect(&gfx, x, y, w, h);
    GFX_ProgressBar(&gfx, x, y, w, h, u);

/*    GFX_PrintfAt(&gfx, 61, -1, "%d us", time);
    int foo = GFX_PrintAt(&gfx, 0,  23, "Hello World");

    GFX_PrintfAt(&gfx, 61, 8, "%d == %d", foo,
                 GFX_TextWidth(&gfx, "Hello World"));
*/
    LCD_Update(screen);
  }

}




void huge _pascal Example1_Func(rpCgiPtr request)
{
  static char *text = "Nur ein kleiner Test!";
  request->fHttpResponse         = CgiHttpOk;
  request->fDataType             = CGIDataTypeText;
  request->fResponseBufferPtr    = text;
  request->fResponseBufferLength = strlen(text);
}


void main(int argc, char* argv[])
{
  if (!CheckBIOS())
    return;

  CGI_Entry cgi = {"example", CgiHttpGet, Example1_Func};
  CGI_Install(&cgi);

  LCD_Init();
  RC_Init();
  VS_Init();

  gfxTest();

  VS_Done();
  RC_Done();
  LCD_Done();

  CGI_Delete(cgi.PathPtr);
}
