//-----------------------------------------------------------------------------
/*
 * Description: This file contains a small test application
 *
 * See the file README in the main directory for a description of
 * this software, copyright information, and how to reach the author.
 *
 * Author: alex
 * Date:   17.11.2003
 *
 * Last modfied:
 *   $Author: alex $
 *   $Date: 2003/12/14 03:12:59 $
 *   $Revision: 1.2 $
 */
//-----------------------------------------------------------------------------

//=============================================================================
//     includes
//=============================================================================
//----- qt --------------------------------------------------------------------
//----- CORBA -----------------------------------------------------------------
//----- ICE -------------------------------------------------------------------
//----- C++ -------------------------------------------------------------------
//----- C ---------------------------------------------------------------------
//----- AxLib -----------------------------------------------------------------
#include <Ax/Tools/Trace.h>
#include <Ax/Tools/TraceOn.h>
#include <Ax/Mail/MailBox.h>
#include <Ax/Mail/MailPart.h>
#include <Ax/Mail/Mail.h>

//----- vdr -------------------------------------------------------------------
//----- local plugin ----------------------------------------------------------
//----- c-client --------------------------------------------------------------
//----- locals ----------------------------------------------------------------
#include "AxMailTools.h"

//=============================================================================
//     defines
//=============================================================================

//=============================================================================
//     using namespaces
//=============================================================================

//=============================================================================
//     
//=============================================================================
// when compiled against AxLibDummy, we use vdr's logging
// defines and they need this variable
int SysLogLevel = 3;

//=============================================================================
//     main()
//=============================================================================
int main(int, char *[])
{
  //----- configure MailBox-Setup -----
  printf("creating AxMailBoxSetup...\n");
  Ax::Mail::MailBoxSetup aMailBoxSetup;

  strcpy(aMailBoxSetup.AccountName   , "TestAccount");
        aMailBoxSetup.fAutoMarkSeen   = false;
        aMailBoxSetup.fExpungeOnClose = false;

  printf("User name: "); scanf("%s", aMailBoxSetup.LoginName);
  printf("Password : "); scanf("%s", aMailBoxSetup.LoginPass);
  printf("MailBox  : "); scanf("%s", aMailBoxSetup.MailBox  );

  //----- create mailbox -------------
  printf("creating AxMailBox...\n");
  Ax::Mail::MailBox aMailBox(&aMailBoxSetup);

  //----- open mailbox ---------------
  printf("opening AxMailBox...\n");
  if (!aMailBox.open())
  {
    fprintf(stderr, "Unable to open mailbox: '%s'\n", aMailBoxSetup.MailBox);
    return 1;
  } // if

  //----- check the status -----------
  printf("requesting status from AxMailBox...\n");
  if (!aMailBox.requestStatus())
  {
    fprintf(stderr, "Unable to request status for mailbox: '%s'\n", aMailBoxSetup.MailBox);
    return 1;
  } // if

  //----- work with mailbox ----------
  printf( "%ld recent  %ld unseen  %ld mails in %s\n"
        , aMailBox.getCountRecent()
        , aMailBox.getCountUnseen()
        , aMailBox.getCountMails()
        , aMailBox.getAccountName()
        );

  AxMailTools::AxEncStat anEncStat;

  int aNumberOfEncMailsOK  = 0;
  int aNumberOfEncMailsNOK = 0;

  int aNumberOfEncQwOK     = 0;
  int aNumberOfEncQwNOK    = 0;

  //----- request information about the mails -----
  for (long aMailNum = 1; aMailNum <= aMailBox.getCountMails(); ++aMailNum)
  {
    Ax::Mail::Mail *aMail = aMailBox.getMail(aMailNum);
    if (aMail == 0)
    {
      fprintf(stderr, "Unable to get mail %ld\n", aMailNum);
      continue;
    } // if

    bool fOK = true;
    std::string aSubject;
    std::string aSender ;

    anEncStat.CountCodesOK  = 0;
    anEncStat.CountCodesNOK = 0;

    fOK = AxMailTools::decodeEncodedWords(aMail->getSubject(), aSubject, &anEncStat) && fOK;
    fOK = AxMailTools::decodeEncodedWords(aMail->getFrom()   , aSender , &anEncStat) && fOK;

    if (anEncStat.CountCodesOK  != 0) aNumberOfEncMailsOK ++;
    if (anEncStat.CountCodesNOK != 0) aNumberOfEncMailsNOK++;

    aNumberOfEncQwOK     += anEncStat.CountCodesOK ;
    aNumberOfEncQwNOK    += anEncStat.CountCodesNOK;

    if (!fOK)
    {
      printf("=====================================================================\n");
      printf("MailNum: %ld = %ld\n", aMailNum, aMail->getMailNum());

      printf("Date   : %s\n"       , aMail->getDate    ().c_str());
      printf("Subject: '%s'\n"     , aMail->getSubject(4096).c_str());
      printf("Subject: '%s'\n"     , aSubject.c_str());
      printf("From   : %s\n"       , aMail->getFrom().c_str());
      printf("From   : %s\n"       , aSender.c_str());
      printf("Flags  : %s\n"       , aMail->getFlagsStr().c_str());
      printf("Size   : %ld  \n"    , aMail->getBodySize());
    } // if

#if DISPLAY_BODY
    Ax::Mail::MailPartList aPartList = aMail->getFlatPartList(false);
    for (Ax::Mail::MailPartList::const_iterator  anIter  = aPartList.begin();
                                                 anIter != aPartList.end();
                                               ++anIter)
    {
      const Ax::Mail::MailPart *aPart = *anIter;

      printf("---------------------------------------------------------------------\n");
      printf(" Section : %s    \n", aPart->getPartName().c_str());
      printf(" Type    : %s    \n", aPart->getTypeStr().c_str());
      printf(" Encoding: %s    \n", aPart->getEncStr().c_str());
      printf(" SubType : %-16s \n", aPart->getSubType().c_str());
      printf(" ID      : %-16s \n", aPart->getID().c_str());
      printf(" Desc    : %-16s \n", aPart->getDesc().c_str());
      printf(" #Lines  : %6ld  \n", aPart->getCountLines());
      printf(" #Bytes  : %6ld  \n", aPart->getCountBytes());

      Ax::Mail::String2StringMap aMap = aPart->getParams();
      for (Ax::Mail::String2StringMap::const_iterator aParamIter  = aMap.begin();
                                                      aParamIter != aMap.end();
                                                    ++aParamIter)
      {
        printf(" Param   : '%s' = '%s'\n"
              , aParamIter->first.c_str()
              , aParamIter->second.c_str()
              );
      } // for

      if (aPart->getType() == TYPETEXT)
      {
        printf(".....................................................................\n");
        std::string aBodyText = aPart->getBodyText();
        printf("%s\n", aBodyText.c_str());
      } // if
    } // for
#endif
  } // for

  printf("=====================================================================\n");
  // sort for statistics
  int aNumberOfMails    = aMailBox.getCountMails();
  int aNumberOfEncQW    = aNumberOfEncQwOK    + aNumberOfEncQwNOK   ;
  int aNumberOfEncMails = aNumberOfEncMailsOK + aNumberOfEncMailsNOK;
  std::list<std::string> anEncList;
  for (AxMailTools::AxEncStatMap::iterator anIter  = anEncStat.MapEnc.begin();
                                         anIter != anEncStat.MapEnc.end();
                                       ++anIter)
  {
    anEncList.push_back(anIter->first);
  } // for

  anEncList.sort();

  printf("Statistics: Number of mails            : %5d\n", aNumberOfMails   );

  printf("Statistics: Number of mails with QW    : %5d           = %5.2lf%% of all\n"
        , aNumberOfEncMails
        , double(aNumberOfEncMails) / double(aNumberOfMails) * 100.0
        );
  printf("Statistics: Number of mails with QW OK : %5d = %6.2lf%% = %5.2lf%% of all\n"
        , aNumberOfEncMailsOK
        , double(aNumberOfEncMailsOK ) / double(aNumberOfEncMails) * 100.0
        , double(aNumberOfEncMailsOK ) / double(aNumberOfMails   ) * 100.0
        );
  printf("Statistics: Number of mails with QW NOK: %5d = %6.2lf%% = %5.2lf%% of all\n"
        , aNumberOfEncMailsNOK
        , double(aNumberOfEncMailsNOK) / double(aNumberOfEncMails) * 100.0
        , double(aNumberOfEncMailsNOK) / double(aNumberOfMails   ) * 100.0
        );

  printf("Statistics: Number of QW               : %5d\n", aNumberOfEncQW);
  printf("Statistics: Number of QW OK            : %5d = %6.2lf%%\n"
        , aNumberOfEncQwOK
        , double(aNumberOfEncQwOK ) / double(aNumberOfEncQW) * 100.0
        );
  printf("Statistics: Number of QW NOK           : %5d = %6.2lf%%\n"
        , aNumberOfEncQwNOK
        , double(aNumberOfEncQwNOK) / double(aNumberOfEncQW) * 100.0
        );

  for (std::list<std::string>::iterator aStrListIter  = anEncList.begin();
                                        aStrListIter != anEncList.end();
                                      ++aStrListIter)
  {
    AxMailTools::AxEncStatMap::iterator aStat = anEncStat.MapEnc.find(*aStrListIter);
    if (aStat != anEncStat.MapEnc.end())
    {
      printf("Encoding: %-20s  Count: %4d  %6.2lf%% of qw\n"
            , aStat->first.c_str()
            , aStat->second
            , double(aStat->second) / double(aNumberOfEncQW) * 100.0
            );
    } // for
  } // for
  printf("=====================================================================\n");
  //----- close mailbox and exit -----
  aMailBox.close();

  return 0;
} // main()


