#include <filezilla.h>
#include "recentserverlist.h"
#include "ipcmutex.h"
#include "filezillaapp.h"
#include "Options.h"
#include "xmlfunctions.h"

const std::list<CServer> CRecentServerList::GetMostRecentServers(bool lockMutex /*=true*/)
{
	std::list<CServer> mostRecentServers;

	CInterProcessMutex mutex(MUTEX_MOSTRECENTSERVERS, false);
	if (lockMutex)
		mutex.Lock();

	CXmlFile xmlFile(wxGetApp().GetSettingsFile(_T("recentservers")));
	auto element = xmlFile.Load();
	if (!element || !(element = element.child("RecentServers")))
		return mostRecentServers;

	bool modified = false;
	auto xServer = element.child("Server");
	while (xServer) {
		CServer server;
		if (!GetServer(xServer, server) || mostRecentServers.size() >= 10) {
			auto xRemove = xServer;
			xServer = xServer.next_sibling("Server");
			element.remove_child(xRemove);
			modified = true;
		}
		else {
			std::list<CServer>::const_iterator iter;
			for (iter = mostRecentServers.begin(); iter != mostRecentServers.end(); ++iter) {
				if (*iter == server)
					break;
			}
			if (iter == mostRecentServers.end())
				mostRecentServers.push_back(server);
			xServer = xServer.next_sibling("Server");
		}
	}

	if (modified) {
		xmlFile.Save(false);
	}

	return mostRecentServers;
}

void CRecentServerList::SetMostRecentServer(const CServer& server)
{
	CInterProcessMutex mutex(MUTEX_MOSTRECENTSERVERS);

	// Make sure list is initialized
	auto mostRecentServers = GetMostRecentServers(false);

	bool relocated = false;
	for (auto iter = mostRecentServers.begin(); iter != mostRecentServers.end(); ++iter) {
		if (iter->EqualsNoPass(server)) {
			mostRecentServers.erase(iter);
			mostRecentServers.push_front(server);
			relocated = true;
			break;
		}
	}
	if (!relocated) {
		mostRecentServers.push_front(server);
		if (mostRecentServers.size() > 10)
			mostRecentServers.pop_back();
	}

	if (COptions::Get()->GetOptionVal(OPTION_DEFAULT_KIOSKMODE) == 2)
		return;

	CXmlFile xmlFile(wxGetApp().GetSettingsFile(_T("recentservers")));
	auto element = xmlFile.CreateEmpty();
	if (!element)
		return;

	auto servers = element.child("RecentServers");
	if (!servers) {
		servers = element.append_child("RecentServers");
	}

	for (std::list<CServer>::const_iterator iter = mostRecentServers.begin(); iter != mostRecentServers.end(); ++iter) {
		auto server = servers.append_child("Server");
		SetServer(server, *iter);
	}

	xmlFile.Save(true);
}

void CRecentServerList::Clear()
{
	CInterProcessMutex mutex(MUTEX_MOSTRECENTSERVERS);

	CXmlFile xmlFile(wxGetApp().GetSettingsFile(_T("recentservers")));
	xmlFile.CreateEmpty();
	xmlFile.Save(true);
}
