/***************************************************************************
                         SP Mail project : Simple Powerful Mail
                          CMailAccounts.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.                                   *
 *                                                                         *
 ***************************************************************************/

#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();
	CXmlNode* topic = m_registry->findFirst("accounts");
	if (topic) { 
		CXmlNode::iterator itor = topic->begin();
		CXmlNode::iterator iend = topic->end();
		for (; itor != iend; itor++) {
			CXmlNode& accnt = *(*itor);
			CStrings sl;
			sl.push_back(accnt.attributes["type"]);
			sl.push_back(accnt.attributes["name"]);
			sl.push_back(accnt.attributes["server"]);
			sl.push_back(accnt.attributes["user"]);
			sl.push_back(accnt.attributes["password"]);
			CPackedStrings *row = new CPackedStrings(sl);
			m_accountsView->addRow(row);
		}
	}
}

void CMailAccounts::saveConfig() {
	CXmlNode* topic = m_registry->findFirst("accounts");
	if (!topic) 
		topic = new CXmlElement(*m_registry,"accounts");
	else topic->clear();
	for (unsigned i = 0; i < m_accountsView->size(); i++) {
		CPackedStrings& row = *m_accountsView->row(i);
		CXmlNode& accnt = *(new CXmlElement(*topic,"account"));
		accnt.attributes["type"] = row[0];
		accnt.attributes["name"] = row[1];
		accnt.attributes["server"] = row[2];
		accnt.attributes["user"] = row[3];
		accnt.attributes["password"] = row[4];
	}
	m_registry->save();
}
