//---------------------------------------------------------------------------

#pragma hdrstop

#include <ctype.h>
#include <stdio.h>
#include <windows.h>

#include "DownloadDLL.h"

#define false 0
#define true  1

// Local Prototypes
unsigned short ReadNumber(char * CharPtr);



//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{
  // local variables which hold the parameters passed on the command line
  char*          FullFileName;
  unsigned short ccTalkAddress;
  unsigned short Device;
  unsigned short i;

  // keep track of which parameters have been detected
  unsigned short FilenameDefined = false;
  unsigned short AddressDefined = false;
  unsigned short DeviceDefined = false;

  // Parse the command line
  for (i = 1 ; i < argc ; ++i)
  {
    if (argv[i][0] == '/' && tolower(argv[i][1]) == 'f')
    {
      // File name
      FilenameDefined = true;
      FullFileName = &argv[i][2];
      //printf("File %s\n", FullFileName);
    }
    else if (argv[i][0] == '/' && tolower(argv[i][1]) == 'a')
    {
      // ccTalk Address
      AddressDefined = true;
      ccTalkAddress = ReadNumber(&argv[i][2]);
      //printf("ccTalk Address %u\n", ccTalkAddress);
    }
    else if (argv[i][0] == '/' && tolower(argv[i][1]) == 'd' && tolower(argv[i][2]) == 'e')
    {
      // Device Elite
      DeviceDefined = true;
      Device = DEVICE_ELITE;
      //printf("Elite\n");
    }
    else if (argv[i][0] == '/' && tolower(argv[i][1]) == 'd' && tolower(argv[i][2]) == 's')
    {
      // Device SR5i
      DeviceDefined = true;
      Device = DEVICE_SR5;
      //printf("SR5i\n");
    }
    else if (argv[i][0] == '/' && tolower(argv[i][1]) == 'd' && tolower(argv[i][2]) == 'l')
    {
      // Device Lumina
      DeviceDefined = true;
      Device = DEVICE_LUMINA;
      //printf("Lumina\n");
    }
    else
    {
      // unrecognised parameter
      printf("Parameter not recognised: %s\n", &argv[i][0]) ;
    }
  }

  // if all parameters defined then start the download
  if ( FilenameDefined && AddressDefined && DeviceDefined )
  {
    unsigned short ReturnCode;

    // the next call starts the download thread then returns with the thread still running
    // The second parameter is not used for downloads via PayLink and can be left as zero.
    ReturnCode = StartDefinitionDownload(FullFileName, 0, ccTalkAddress, Device);

    if ( ReturnCode == START_DOWNLOAD_TRANSMISSION_STARTED)
    {
      // download started succesfully. The download thread is running
      unsigned short DownloadStatus;
      unsigned short TerminationStatus;
      unsigned short Progress;
      unsigned short ProgressLimit;

      printf("Download started\n");

      while ( DefinitionDownloadStatus(&DownloadStatus, &TerminationStatus, &Progress, &ProgressLimit) == DOWNLOAD_THREAD_RUNNING)
      {
        // still running - wait update the progress display then wait 1s
        printf("\rProgress %3u of %3u. Download status %u  ", Progress, ProgressLimit, DownloadStatus);
        Sleep(1000);
      }
      printf("\n");

      if ( TerminationStatus == TERMINATION_STATUS_TRANSMISSION_SUCCESS )
      {
        printf("Download completed successfully\n");
      }
      else
      {
        printf("Download finished with errors:\n");
        printf("  DownloadStatus    = %u\n", DownloadStatus);
        printf("  TerminationStatus = %u\n", TerminationStatus);
        printf("  Progress          = %u\n", Progress);
        printf("  ProgressLimit     = %u\n", ProgressLimit);
        printf("\n");
      }
    }
    else
    {
      // The application failed to start the download thread
      printf("Download not started\n");
    }
  }
  else
  {
    // There was an error with the parameters passed to the application
    // Display help
    printf("\n");
    printf("\n") ;
    printf("AES SR5i Coin, Lumina & Elite Note Download Program (via PayLink). %s\n", __DATE__) ;
    printf("\n") ;
    printf("PayLinkDownload /f## /a## /d#  \n", &argv[0][0]);
    printf("  /f##         Name and path of definition file\n") ;
    printf("  /a##         ccTalk Address\n") ;
    printf("  /de          Device is Ardac Elite\n");
    printf("  /ds          Device is SR5i\n");
    printf("  /dl          Device is Lumina\n");
    printf("\n") ;
  }

  return 0;
}


/*
 *  TITLE       ReadNumber
 *  PARAMETERS  Pointer to an ascii number
 *  RETURN      Value of the ascii string
 *  FUNCTION    Convert the ascii string to the numberic value.
 */

unsigned short ReadNumber(char * CharPtr)
{
  unsigned short ReturnVal = 0;
  unsigned short Byte = *CharPtr++;

  while ( Byte >= '0' && Byte <= '9' )
  {
    // found a valid digit so scale up previous value and add new digit.
    ReturnVal *= 10;
    ReturnVal += Byte - '0';
    Byte = *CharPtr++;
  }
  return ReturnVal;
}


