/***************************************************************************
                         SP Mail project : Simple Powerful Mail
                          main.cpp  -  description
                             -------------------
    begin                : Sun Jun 22 2003
    copyright            : (C) 2003-2006 by Alexey Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/


#ifdef __BORLANDC__
#include <vcl.h>
#pragma hdrstop
#endif

#include <FL/Fl.h>
#include <FL/Fl_Pixmap.h>
#include <FL/fl_ask.h>
#include <stdio.h>

#include <sptk3/CWindow.h>
#include <sptk3/CMenuBar.h>
#include <sptk3/CProgressBar.h>
#include <sptk3/CEditor.h>
#include <sptk3/CException.h>
#include <sptk3/CTreeControl.h>
#include <sptk3/CMessageDialog.h>

#include "CMailAccounts.h"
#include "CImapFolders.h"

using namespace std;

CRegistry      registry("sp-mail.ini","sp-mail");
CImapFolders  *imapFolders;
CListView     *messageList;

void quit_cb(Fl_Widget *w,void *) {
   w->window()->hide();
}

void accounts_cb(Fl_Widget *,void *) {
   CMailAccounts dialog(&registry);

   dialog.exec();
}

void test_accounts_cb(Fl_Widget *,void *) {
   try {
      imapFolders->loadAccounts();
   }
   catch (exception& e) {
      spError(e.what());
   }
}

void mailbox_cb(Fl_Widget *widget,void *) {
   try {
      CControl *control = dynamic_cast<CControl *>(widget);
      if (control && control->eventType() == CE_DATA_CHANGED) {
         string fullName = imapFolders->selectedPath().c_str() + 1;
         int p = fullName.find("/");
         if (p < 0) return;
         string accountName = fullName.substr(0,p);
         string mailBox = fullName.substr(p+1,1024);
         CImapAccount& account = imapFolders->account(accountName);
         account.DS().folder(mailBox);
         account.DS().fetchBody(false);
         messageList->fill(account.DS(),"msg_id");
      }
   }
   catch (exception& e) {
      spError(e.what());
   }
}

void message_cb(Fl_Widget *widget,void *) {
   try {
      CListView *control = dynamic_cast<CListView *>(widget);
      if (control && control->eventType() == CE_DATA_CHANGED) {
         int messageID = control->data();
         string fullName = imapFolders->selectedPath().c_str() + 1;
         int p = fullName.find("/");
         if (p < 0) return;
         string accountName = fullName.substr(0,p);
         string mailBox = fullName.substr(p+1,1024);
         CImapAccount& account = imapFolders->account(accountName);
         CImapDS& imapDS = account.DS();
         imapDS.folder(mailBox);
         imapDS.messageID(messageID);
         imapDS.fetchBody(true);
         imapDS.open();
         std::string msgBody = imapDS["body"];
         spError(msgBody);
         imapDS.close();
      }
   }
   catch (exception& e) {
      spError(e.what());
   }
}

Fl_Menu_Item menuTable[] = {
   {"&File",0,0,0,FL_SUBMENU},
   {"&Test",   FL_ALT+'t', test_accounts_cb},
   {"&Quit",   FL_ALT+'q', quit_cb},
   {0},
   {"&Settings",0,0,0,FL_SUBMENU},
   {"&Accounts",FL_SHIFT+'a',accounts_cb},
   {0},
   {0}
};

int main(int argc,char *argv[]) {
   try {
      registry.load();
   }
   catch (...) {}

   CWindow w(500,400);
   w.layoutSpacing(2);

   CMenuBar menuBar;
   menuBar.box(FL_FLAT_BOX);
   menuBar.menu(menuTable);

   CGroup statusBar("",5,SP_ALIGN_BOTTOM);
   statusBar.layoutSpacing(0);

   CBox statusBox("",100,SP_ALIGN_CLIENT);
   statusBox.data("Ready");
   statusBox.textSize(10);
   statusBox.box(FL_DOWN_BOX);

   CProgressBar progressBar("",100,SP_ALIGN_RIGHT);

   statusBar.end();

   CImapFolders folders(registry);
   imapFolders = &folders;
   imapFolders->callback(mailbox_cb);

   messageList = new CListView ("",150);
   messageList->dataMode(LV_DATA_KEY);
   CEditor messageEdit(10,SP_ALIGN_CLIENT);
   messageList->callback(message_cb);

   w.end();
   w.resizable(&w);

   imapFolders->loadAccounts();

   w.show(argc,argv);

   return Fl::run();
}
