/*

  SelectLanguage
  ==============

    The main purpose of this door is to get the user to pick a language/screen type

*/

#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/alib_protos.h>

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <time.h>

#include <HBBS/ANSI_Codes.h>
#include <HBBS/Defines.h>
#include <HBBS/types.h>
#include <HBBS/structures.h>
#include <HBBS/hbbscommon_protos.h>
#include <HBBS/hbbscommon_pragmas.h>
#include <HBBS/Hbbsnode_protos.h>
#include <HBBS/Hbbsnode_pragmas.h>
#include <HBBS/release.h>
char *versionstr="$VER: SelectLanguage "RELEASE_STR;

struct Library *HBBSCommonBase=NULL;
struct Library *HBBSNodeBase=NULL;

struct BBSGlobalData *BBSGlobal=NULL;
struct NodeData *N_ND=NULL;
int    N_NodeNum=-1;
char   outstr[1024]; // temp string for displaying text..

int    gargc;
char   **gargv;


#ifdef __SASC
int CXBRK(void) { return(0); }
int _CXBRK(void) { return(0); }
void chkabort(void) {}
#endif


static VOID cleanup(ULONG num)
{
  if (HBBSNodeBase)
  {
    HBBS_CleanUpDoor();
    CloseLibrary (HBBSNodeBase);
  }

  if (HBBSCommonBase)
  {
    HBBS_CleanUpCommon();
    CloseLibrary (HBBSCommonBase);
  }

  if (num) printf("Door Error = %d\n",num);

  exit(0);
}

static VOID init(char *name)
{
  if(!(HBBSCommonBase = OpenLibrary("HBBSCommon.library",0)))
  {
    cleanup(1);
  }

  if (!(HBBS_InitCommon()))
  {
    cleanup(2);
  }

  if(!(HBBSNodeBase = OpenLibrary("HBBSNode.library",0)))
  {
    cleanup(3);
  }

  if (!(HBBS_InitDoor(N_NodeNum,name)))
  {
    cleanup(4);
  }
  SetProgramName(name);
}

void DisplaySelection( int newlang,int oldlang)
{
  struct Node *node=NULL;

  DOOR_WriteText(ANSI_RESET);
  DOOR_DisplaySpecialScreen("Banner_TOP");

  if (oldlang)
  {
    if (oldlang>=1 && oldlang <= BBSGlobal->Languages )
      node=GetNode(BBSGlobal->LanguageName,oldlang-1);

    sprintf(outstr,ANSI_FG_PURPLE "  Current Language/Style: "ANSI_FG_WHITE"%s\r\n",node ? node->ln_Name : "INVALID!");
    DOOR_WriteText(ANSI_RESET);
    DOOR_WriteText(outstr);
  }

  if (newlang)
  {
    if (newlang>=1 && newlang <= BBSGlobal->Languages )
      node=GetNode(BBSGlobal->LanguageName,newlang-1);

    sprintf(outstr,ANSI_FG_CYAN   "      New Language/Style: "ANSI_FG_WHITE"%s\r\n",node ? node->ln_Name : "INVALID!");
    DOOR_WriteText(ANSI_RESET);
    DOOR_WriteText(outstr);
  }
  DOOR_WriteText(ANSI_RESET);
  DOOR_DisplaySpecialScreen("Banner_BOT");
}

void DoorMain( void )
{
  LONG loop;
  V_BIGNUM newlang,oldlang;
  BOOL Done=FALSE;
  struct Node *node;

  // Only ask if more than one language defined!

  oldlang=N_ND->User->CallData->Language;
  newlang=N_ND->User->CallData->Language;

  if (HBBS_NodesInList(BBSGlobal->LanguageName)>1)
  {
    if (gargc==3)
    {
      if (sscanf(gargv[2],"%ld",&loop) == 1)
      {
        if (loop>=1 && loop<=BBSGlobal->Languages)
        {
          newlang=loop;
          Done=TRUE;

        }
        else
        {
          sprintf(outstr,"Language/Style %d not available, please pick one\r\n",loop);
          DOOR_WriteText(outstr);
        }
      }
    }

    if (!Done)
    {
      DisplaySelection(0,oldlang);

      for (loop=1,node = BBSGlobal->LanguageName->lh_Head ; node->ln_Succ ; node=node->ln_Succ,loop++)
      {
        sprintf(outstr,ANSI_FG_CYAN"%d"ANSI_FG_BLUE") "ANSI_FG_CYAN"%s\r\n"ANSI_RESET,loop,node->ln_Name);
        DOOR_WriteText(outstr);
      }

      do
      {
        DOOR_WriteText(ANSI_FG_YELLOW "Select A Language/Screen Type "ANSI_FG_BLUE": "ANSI_FG_WHITE);

        strcpy(N_ND->CharsAllowed,"0123456789");
                                                         // more than 99 langauges ?  I don't think so.. :-)
        DOOR_GetLine(GL_EDIT|GL_DISPLAY|GL_USECHARS|GL_IMMEDIATE,'\0',BBSGlobal->Languages > 9 ? 2 : 1,0,NULL);

        if (N_ND->OnlineStatus==OS_ONLINE)
        {
          if (sscanf(N_ND->CurrentLine,"%ld",&loop) == 1)
          {
            if (loop>=1 && loop<=BBSGlobal->Languages)
            {
              newlang=loop;
              Done=TRUE;
            }
          }
        }
        else
        {
          Done=TRUE;
        }
      } while (!Done);
    }

    N_ND->User->CallData->Language=newlang;
    N_ND->User->NormalData->Language=newlang;

    DisplaySelection(newlang,oldlang);

  }
  else
  { 
    DOOR_WriteText(ANSI_FG_WHITE"Only one language defined!");
    DisplaySelection(newlang,oldlang);
    
    N_ND->User->CallData->Language=1;
    N_ND->User->NormalData->Language=1;
  }
}

int main(int argc,char **argv)
{
  gargc=argc;
  gargv=argv;

  if (sscanf(argv[1],"%d",&N_NodeNum)==0)
  {
    printf("Invalid/No Paramaters for door!\n");
    exit (20);
  }
  init("SelectLanguage");

  if (BBSGlobal=HBBS_GimmeBBS())
  {
    if (N_ND=HBBS_NodeDataPtr(N_NodeNum)) // this should not fail in normal circumstances..
    {
      DoorMain();
    }
  }
  cleanup(0);
}
