/***************************************************************************
                         SP Mail project : Simple Powerful Mail
                          CMailAccounts.cpp  -  description
                             -------------------
    begin                : Sun Jun 22 2003
    copyright            : (C) 2003 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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <stdio.h>
#include <time.h>

#include <sptk3/CException.h>

#include "CMailAccounts.h"
using namespace std;

void CMailAccounts::cb_addAccount(Fl_Widget *w,void *) {
   CMailAccounts *dialog = (CMailAccounts *)w->window();
   dialog->addAccount();
}

void CMailAccounts::cb_removeAccount(Fl_Widget *w,void *) {
   CMailAccounts *dialog = (CMailAccounts *)w->window();
   dialog->removeAccount();
}

void CMailAccounts::cb_accountUpdated(Fl_Widget *w,void *) {
   CMailAccounts *dialog = (CMailAccounts *)w->window();
   CControl *control = (CControl *)w;
   if (control->eventType() == CE_DATA_CHANGED)
      dialog->accountUpdated();
}

void CMailAccounts::cb_accountSelected(Fl_Widget *w,void *) {
   CMailAccounts *dialog = (CMailAccounts *)w->window();
   CControl *control = (CControl *)w;
   if (control->eventType() == CE_DATA_CHANGED)
      dialog->accountSelected();
}

CMailAccounts::CMailAccounts(CRegistry *registry)
: CDialog(500,400,"Mail Accounts") {
   m_registry = registry;
   newPage("default");

   m_accountsView = new CListView("",200,SP_ALIGN_LEFT);
   m_accountsView->addColumn("type",VAR_STRING,50);
   m_accountsView->addColumn("name",VAR_STRING,146);
   m_accountsView->addColumn("server",VAR_STRING,100,false);
   m_accountsView->addColumn("user",VAR_STRING,100,false);
   m_accountsView->addColumn("password",VAR_STRING,100,false);
   m_accountsView->callback(cb_accountSelected);

   m_accountInfoGroup = new CGroup("",10,SP_ALIGN_CLIENT);
   m_serverName = new CInput("server");
   m_userName = new CInput("user name");
   m_userPassword = new CPasswordInput("password");
   m_accountInfoGroup->end();

   for (unsigned i = 0; i < (unsigned)m_accountInfoGroup->children(); i++) {
      CControl *control = dynamic_cast<CControl *>(m_accountInfoGroup->child(i));
      if (control) {
         control->labelWidth(80);
         control->callback(cb_accountUpdated);
      }
   }

   end();

   addExtraButton(SP_DELETE_BUTTON,"Remove Account",cb_removeAccount);
   addExtraButton(SP_ADD_BUTTON,"New Account",cb_addAccount);
}

CMailAccounts::~CMailAccounts() {
}

bool CMailAccounts::exec() {
   loadConfig();
   if (showModal()) {
      saveConfig();
      return true;
   }
   return false;
}

void CMailAccounts::addAccount() {
   CDialog getStringDialog(300,110,"Create New Account");
   getStringDialog.newPage("default");
   CInput  input("Account Name");

   CStrings sl;
   sl.push_back(idstring("IMAP",1));
   sl.push_back(idstring("POP",2));
   sl.push_back(idstring("SMTP",3));
   CComboBox accountType("AccountType");
   accountType.addRows("type",sl);
   accountType.data(1);

   getStringDialog.end();
   if (getStringDialog.showModal()) {
      int id = time(NULL);
      CStrings sl;
      sl.push_back(accountType.selectedRow()[0]);
      sl.push_back(input.data().asString());
      sl.push_back("");
      sl.push_back("");
      sl.push_back("");
      m_accountsView->addRow(sl,id);
      m_accountsView->data(id);
   }
}

void CMailAccounts::removeAccount() {
}

void CMailAccounts::accountUpdated() {
   try {
      CPackedStrings& ps = m_accountsView->selectedRow();
      CStrings sl;
      sl.push_back(ps[0]);
      sl.push_back(ps[1]);
      sl.push_back(m_serverName->data().asString());
      sl.push_back(m_userName->data().asString());
      sl.push_back(m_userPassword->data().asString());
      ps = CPackedStrings(sl);
   }
   catch (exception& e) {
      puts(e.what());
   }
}

void CMailAccounts::accountSelected() {
   try {
      CPackedStrings& ps = m_accountsView->selectedRow();
      m_serverName->data(ps[2]);
      m_userName->data(ps[3]);
      m_userPassword->data(ps[4]);
   }
   catch (exception& e) {
      puts(e.what());
   }
}

void CMailAccounts::loadConfig() {
   m_accountsView->clear();
   CRegistry& registry = *m_registry;
   registry.topic("Accounts");
   int accountNumber = registry["account number"];
   for (int i = 0; i < accountNumber; i++) {
      registry.topic("Account " +int2string(i));
      CStrings sl;
      sl.push_back(registry["type"].c_str());
      sl.push_back(registry["name"].c_str());
      sl.push_back(registry["server"].c_str());
      sl.push_back(registry["user"].c_str());
      sl.push_back(registry["password"].c_str());
      CPackedStrings *row = new CPackedStrings(sl);
      m_accountsView->addRow(row);
   }
}

void CMailAccounts::saveConfig() {
   CRegistry& registry = *m_registry;
   registry.topic("Accounts");
   int accountNumber = m_accountsView->size();
   registry["account number"] = accountNumber;
   for (int i = 0; i < accountNumber; i++) {
      registry.topic("Account " +int2string(i));
      CPackedStrings& row = *m_accountsView->row(i);
      registry["type"] = row[0];
      registry["name"] = row[1];
      registry["server"] = row[2];
      registry["user"] = row[3];
      registry["password"] = row[4];
   }
   registry.save();
}
