#include "wimp.h"
#include "wimpt.h"
#include "win.h"
#include "event.h"
#include "baricon.h"
#include "res.h"
#include "resspr.h"
#include "menu.h"
#include "template.h"
#include "dbox.h"
#include "werr.h"
#include "bbc.h"
#include "os.h"
#include "xfersend.h"
#include "saveas.h"
#include "colourmenu.h"
#include "sprite.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#define icon_menu_INFO        1
#define icon_menu_APPSIZE     2
#define icon_menu_SPRITETEXT  3
#define icon_menu_BORDERCOL   4
#define icon_menu_BGNDCOL     5
#define icon_menu_TEXTCOL     6
#define icon_menu_CREATE      7
#define icon_menu_QUIT        8

#define appsize_length 5
#define spritetext_length 9

/******************************* GLOBAL DATA *******************************/

char appsize_buff[appsize_length];
char spritetext_buff[spritetext_length];

char current_pathname[256] = "!App";

int appsize;

int bordercol = 11;
int bgndcol = 8;
int textcol = 9;

wimp_i baricon_handle;

menu icon_menu, appsize_menu, spritetext_menu, bordercol_menu, bgndcol_menu, textcol_menu;

/******************************** FUNCTIONS ********************************/

void null_proc(wimp_i icon)
{
  icon = icon;
}

BOOL create_app(char *pathname, void *handle)
{
char buff[256], leafname[12];
sprite_id sp;
FILE *fp;

  handle = handle;                      /* dummy */

    /* find leafname, add ! if necessary */

  saveas_read_leafname_during_send(leafname, 12);
  if (leafname[0] != '!')
  {
    int index = strlen(pathname)-strlen(leafname);

    pathname[index] = '!';
    pathname[index+1] = '\0';
    strcat(pathname, leafname);

    sprintf(buff, "!%s", leafname);
    strcpy(leafname, buff);
  }

    /* create application directory */

  sprintf(buff, "CDir %s", pathname);
  if (wimpt_complain(os_cli(buff)) != 0)
    return FALSE;

    /* generate !Sprites file */

  sp.tag = sprite_id_name;
  sp.s.name = "file_000";

  if (wimpt_complain(sprite_rename(resspr_area(), &sp, leafname)) != 0)
    return FALSE;
  sp.s.name = leafname;

  sprintf(buff, "%s.!Sprites", pathname);
  if (wimpt_complain(sprite_area_save(resspr_area(), buff)) != 0)
    return FALSE;

  wimpt_noerr(sprite_rename(resspr_area(), &sp, "file_000"));

    /* generate !Run file */

  sprintf(buff, "%s.!Run", pathname);
  fp = fopen(buff, "w");

  fprintf(fp, "Set %s$Dir <Obey$Dir>\012", leafname+1);
  fprintf(fp, "IconSprites <Obey$Dir>.!Sprites\012");
  fprintf(fp, "WimpSlot -min %dK -max %dK\012", appsize, appsize);
  fprintf(fp, "Run <Obey$Dir>.!RunImage\012");

  if (fclose(fp) != 0)
    return(FALSE);

  sprintf(buff, "SetType %s.!Run Obey", pathname);
  if (wimpt_complain(os_cli(buff)) != 0)
    return FALSE;

    /* update screen display */

  sprintf(buff, "IconSprites %s.!Sprites", pathname);
  (void) os_cli(buff);
  wimpt_forceredraw();

  strcpy(current_pathname, pathname);

  return TRUE;
}

int text_val(char *p)
{
int val = 0;

  while((*p >= '0') && (*p <= '9'))
  {
    val = val * 10 + (*p - '0');
    p++;
  }

  return(val);
}

void icon_draw(void)
{
int i;

    /* draw border */
  bbc_gcol(0, bordercol);
  bbc_rectanglefill(0, 0, 68, 68);

    /* draw background */
  bbc_gcol(0, bgndcol);
  bbc_rectanglefill(2, 4, 60, 56);

    /* add text */
  bbc_gcol(0, textcol);
  bbc_vdu(5);
  bbc_move(2,60);
  for (i = 0; i < 8; i++)
  {
    if (i == 4)
      bbc_move(2,28);
    bbc_vdu(spritetext_buff[i]);
  }
}

void icon_redraw(void)
{
sprite_id sp;
sprite_state state;

  sp.tag = sprite_id_name;
  sp.s.name = "file_000";

    /* update icon bar sprite */
  wimpt_noerr(sprite_outputtosprite(resspr_area(), &sp, NULL, &state));
  icon_draw();
  wimpt_noerr(sprite_restorestate(state));
  wimp_set_icon_state(win_ICONBAR, baricon_handle, 0, 0);

    /* update Wimp sprite (for 'saveas' dialogue box) */
  wimpt_noerr(sprite_outputtosprite((sprite_area *) wimp_baseofsprites(), &sp, NULL, &state));
  icon_draw();
  wimpt_noerr(sprite_restorestate(state));
}

void infobox(void)
{
dbox  d;

  if ((d = dbox_new("ProgInfo")) != NULL)
  {
    dbox_show(d);
    dbox_fillin(d);
    dbox_dispose(&d);
  }
}

void icon_menuproc(void *handle, char *hit)
{
wimp_mousestr m;

  handle = handle;

  switch (hit[0])
  {
  case icon_menu_INFO:
    infobox();
    break;

  case icon_menu_APPSIZE:
    appsize = text_val(appsize_buff);
    break;

  case icon_menu_SPRITETEXT:
    icon_redraw();
    break;

  case icon_menu_BORDERCOL:
    menu_setflags(bordercol_menu, bordercol+1, 0, 0);
    bordercol = hit[1] - 1;
    menu_setflags(bordercol_menu, bordercol+1, 1, 0);
    icon_redraw();
    break;

  case icon_menu_BGNDCOL:
    menu_setflags(bgndcol_menu, bgndcol+1, 0, 0);
    bgndcol = hit[1] - 1;
    menu_setflags(bgndcol_menu, bgndcol+1, 1, 0);
    icon_redraw();
    break;

  case icon_menu_TEXTCOL:
    menu_setflags(textcol_menu, textcol+1, 0, 0);
    textcol = hit[1] - 1;
    menu_setflags(textcol_menu, textcol+1, 1, 0);
    icon_redraw();
    break;

  case icon_menu_CREATE:
    wimpt_noerr(wimp_get_point_info(&m));
    if (m.bbits && (wimp_BRIGHT + wimp_BLEFT) != 0)
      create_app(current_pathname, (void *) 0);
    else
      saveas(0x000, current_pathname, 0, create_app, 0, 0, 0);
    break;

  case icon_menu_QUIT:
    exit(0);
  }
}

BOOL initialise(void)
{
  wimpt_init("AppMaker");
  res_init("AppMaker");
  resspr_init();
  template_init();
  dbox_init();

    /* top level menu */ 
  if ((icon_menu = menu_new("AppMaker", ">Info, Program size| Sprite text, Border colour, Background colour, Text colour| >Create, Quit")) == NULL)
    return FALSE;

   /* application size submenu */
  if ((appsize_menu = menu_new("Size (K)", "A")) == NULL)
    return FALSE;
  menu_make_writeable(appsize_menu, 1, appsize_buff, appsize_length, "a0-9");
  menu_submenu(icon_menu, icon_menu_APPSIZE, appsize_menu);
  strcpy(appsize_buff, "32");
  appsize_buff[2] = 13;
  appsize = 32;

    /* sprite text submenu */
  if ((spritetext_menu = menu_new("Sprite text", "A")) == NULL)
    return FALSE;
  menu_make_writeable(spritetext_menu, 1, spritetext_buff, spritetext_length, NULL);
  menu_submenu(icon_menu, icon_menu_SPRITETEXT, spritetext_menu);
  strcpy(spritetext_buff, "App Make");
  spritetext_buff[8] = 13;

    /* border colour menu */
  if ((bordercol_menu = colourmenu_make("Colour", FALSE)) == NULL)
    return FALSE;
  menu_setflags(bordercol_menu, bordercol+1, 1, 0);
  menu_submenu(icon_menu, icon_menu_BORDERCOL, bordercol_menu);

    /* background colour menu */
  if ((bgndcol_menu = colourmenu_make("Colour", FALSE)) == NULL)
    return FALSE;
  menu_setflags(bgndcol_menu, bgndcol+1, 1, 0);
  menu_submenu(icon_menu, icon_menu_BGNDCOL, bgndcol_menu);

    /* text colour menu */
  if ((textcol_menu = colourmenu_make("Colour", FALSE)) == NULL)
    return FALSE;
  menu_setflags(textcol_menu, textcol+1, 1, 0);
  menu_submenu(icon_menu, icon_menu_TEXTCOL, textcol_menu);

  baricon_handle = baricon("file_000", (int) resspr_area(), null_proc);
  if (!event_attachmenu(win_ICONBAR, icon_menu, icon_menuproc, 0))
    return FALSE;
  icon_redraw();

  return TRUE;
}

/****************************** MAIN PROGRAM *******************************/

int main()
{
  if (!initialise())
    return 1;

  while (TRUE)
    event_process();

  return 0;
}
