#include "mekeys.h"

#define ERR  (-1)
#define DIR_UP     0
#define DIR_DOWN   1
#define DIR_LEFT   2
#define DIR_RIGHT  3

#define MODE_SINGLE   1
#define MODE_DOUBLE   2


string DefaultGraphics[4];
string OverwriteGraphic[11];
int    LineMode;


init()
{
  assign_key("drawline", "<ALT-D>");

  DefaultGraphics[DIR_UP+1]    = "³³¿Ú";
  DefaultGraphics[DIR_DOWN+1]  = "³³ÙÀ";
  DefaultGraphics[DIR_LEFT+1]  = "ÀÚÄÄ";
  DefaultGraphics[DIR_RIGHT+1] = "Ù¿ÄÄ";

  OverwriteGraphic[1]  = "ÅÂÂÂÁÅÁÁÁÂÄÄÁÂÄÄ";
  OverwriteGraphic[2]  = "³³´Ã³³´ÃÃÃÅÃ´´´Å";
  OverwriteGraphic[3]  = "ÃÚÂÚÃÃÂÃÃÚÃÚÅÂÂÂ";
  OverwriteGraphic[4]  = "ÅÂÂÂÅÅÅÅÅÂÂÂÅÂÂÂ";
  OverwriteGraphic[5]  = "´¿¿Â´´´ÂÅÂÂÂ´¿¿Â";
  OverwriteGraphic[6]  = "ÃÃÅÃÃÃÅÃÃÃÅÃÅÅÅÅ";
  OverwriteGraphic[7]  = "ÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅÅ";
  OverwriteGraphic[8]  = "´´´Å´´´ÅÅÅÅÅ´´´Å";
  OverwriteGraphic[9]  = "ÃÃÅÃÀÃÁÀÀÃÁÀÁÅÁÁ";
  OverwriteGraphic[10] = "ÅÅÅÅÁÅÁÁÁÅÁÁÁÅÁÁ";
  OverwriteGraphic[11] = "´´´ÅÙ´ÙÁÁÅÁÁÙ´ÙÁ";

  LineMode = MODE_SINGLE;
}


drawline()
{
  int  iOldDirection, iNewDirection;

  iOldDirection = ERR;
  setprompt(2);
  debug = 1;

  while (1)
  {
start:
    if (LineMode == MODE_SINGLE)
      szMode = "Single";
    else if (LineMode == MODE_DOUBLE)
      szMode = "Double";

    message("%s-line drawing mode...", szMode);
    s = "";

    key = get_tty_char();
    switch (key)
    {
      case _UP :
        iNewDirection = DIR_UP;
        break;
      case _DOWN :
        iNewDirection = DIR_DOWN;
        break;
      case _LEFT :
        iNewDirection = DIR_LEFT;
        break;
      case _RIGHT:
        iNewDirection = DIR_RIGHT;
        break;
      case '1'   :
        LineMode = MODE_SINGLE;
/*      continue; */
        goto start;
      case '2'   :
        LineMode = MODE_DOUBLE;
/*      continue; */
        goto start;
      case ESC   :
        get_tty_str("Thanks for coming!");
        return;
      default :
        bell();
/*      continue; */
        goto start;
    }

    /*
      See if the character overwritten is another line graphic character
    */
    if ((chCurrGraphic = index("Ä³ÚÂ¿ÃÅ´ÀÁÙÍºÉË»ÌÎ¹ÈÊ¼", chr(currchar()))) >= 1)
    {
      chCurrGraphic = (chCurrGraphic / 12) + (chCurrGraphic % 12);

      if (debug)
        get_tty_str("chCurrGraphic is [%d]", chCurrGraphic);

      if ((s = MaybeCorner(iOldDirection, iNewDirection)) == "")
      {
        s = OverwriteGraphic[chCurrGraphic];
        s = substr(s, iOldDirection*4 + iNewDirection + 1, 1);
      }
    }

    else
    {
      /*
        We are not overwriting an existing graphic.
      */
      if (iOldDirection == ERR)
        iOldDirection = iNewDirection;
      szGraphics = DefaultGraphics[iOldDirection+1];
      s = substr(szGraphics, iNewDirection+1, 1);
      if (debug)
        get_tty_str("szGraphics is %s  and s is %s", szGraphics, s);
    }

    if (s != "")
    {
      if (currchar())
        delchar();
      insert(GraphicTranslate(tonum(s)));
      refresh();
    }

    switch (key)
    {
      case _UP :
        col = currcol()-1;
        up();
        setcol(col);
        break;
      case _DOWN :
        col = currcol()-1;
        if (currlinenum() == lastlinenum())
          append_line();
        else
          down();
        setcol(col);
        break;
      case _LEFT :
        left();
        if (currcol() > 1)
          left();
        break;
      case _RIGHT :
        break;
      default    :
/*      continue; */
        goto start;
    }

    iOldDirection = iNewDirection;

  } /* end while */
}


MaybeCorner(iOld, iNew)
{
  chAbove = chBelow = chRight = chLeft = 0;

  if (up())
  {
    chAbove = currchar();
    down();
  }
  if (down())
  {
    chBelow = currchar();
    up();
  }
  if (currcol() > 1)
  {
    left();
    chLeft = currchar();
    right();
  }
  right();
  chRight = currchar();
  left();


  dir = iOld * 4 + iNew;

  if (dir == 2 || dir == 13)
  {
    /* UP and LEFT or RIGHT and DOWN */
    if (chAbove == 0 && chRight == 0)
      return "¿";
  }
  else if (dir == 3 || dir == 9)
  {
    /* UP and RIGHT or LEFT and DOWN */
    if (chAbove == 0 && chLeft == 0)
      return "Ú";
  }
  else if (dir == 6 || dir == 12)
  {
    /* DOWN and LEFT or RIGHT and UP */
    if (chBelow == 0 && chRight == 0)
      return "Ù";
  }
  else if (dir == 7 || dir == 8)
  {
    /* DOWN and RIGHT or LEFT and UP */
    if (chBelow == 0 && chLeft == 0)
      return "À";
  }

  return "";
}


GraphicTranslate(chGraphic)
{
  switch (chGraphic)
  {
    case 'Ä':
      return substr("ÄÍ", LineMode, 1);
    case '³':
      return substr("³º", LineMode, 1);
    case 'Ú':
      return substr("ÚÉ", LineMode, 1);
    case 'Â':
      return substr("ÂË", LineMode, 1);
    case '¿':
      return substr("¿»", LineMode, 1);
    case 'Ã':
      return substr("ÃÌ", LineMode, 1);
    case 'Å':
      return substr("ÅÎ", LineMode, 1);
    case '´':
      return substr("´¹", LineMode, 1);
    case 'À':
      return substr("ÀÈ", LineMode, 1);
    case 'Á':
      return substr("ÁÊ", LineMode, 1);
    case 'Ù':
      return substr("Ù¼", LineMode, 1);
  }
}
