/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  Georg Rudoy
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#include "core.h"
#include <interfaces/iplugin2.h>
#include <interfaces/core/ientitymanager.h>
#include "collectionsmanager.h"
#include "localfileresolver.h"
#include "localcollection.h"
#include "xmlsettingsmanager.h"
#include "playlistmanager.h"
#include "sync/syncmanager.h"
#include "sync/syncunmountablemanager.h"
#include "sync/clouduploadmanager.h"
#include "interfaces/lmp/ilmpplugin.h"
#include "interfaces/lmp/icloudstorageplugin.h"
#include "lmpproxy.h"
#include "player.h"
#include "previewhandler.h"
#include "progressmanager.h"
#include "radiomanager.h"
#include "rganalysismanager.h"
#include "hookinterconnector.h"

namespace LC
{
namespace LMP
{
	std::shared_ptr<Core> Core::CoreInstance_;

	struct Core::Members
	{
		ICoreProxy_ptr Proxy_;

		LocalFileResolver Resolver_;

		HookInterconnector HookInterconnector_;

		LocalCollection Collection_;
		CollectionsManager CollectionsManager_;

		PlaylistManager PLManager_;

		SyncManager SyncManager_;
		SyncUnmountableManager SyncUnmountableManager_;
		CloudUploadManager CloudUpMgr_;

		ProgressManager ProgressManager_;

		RadioManager RadioManager_;

		Player Player_ { Proxy_ };
		PreviewHandler PreviewMgr_ { &Player_ };

		LMPProxy LmpProxy_ { &Collection_, &Resolver_, &PreviewMgr_ };

		RgAnalysisManager RgMgr_ { &Collection_ };

		Members (const ICoreProxy_ptr& proxy)
		: Proxy_ { proxy }
		{
		}
	};

	Core::Core (const ICoreProxy_ptr& proxy)
	: Proxy_ (proxy)
	, M_ (std::make_shared<Members> (proxy))
	{
		M_->ProgressManager_.AddSyncManager (&M_->SyncManager_);
		M_->ProgressManager_.AddSyncManager (&M_->SyncUnmountableManager_);
		M_->ProgressManager_.AddSyncManager (&M_->CloudUpMgr_);

		M_->CollectionsManager_.Add (M_->Collection_.GetCollectionModel ());
	}

	Core& Core::Instance ()
	{
		return *CoreInstance_;
	}

	void Core::InitWithProxy (const ICoreProxy_ptr& proxy)
	{
		CoreInstance_.reset (new Core { proxy });
	}

	void Core::Release ()
	{
		CoreInstance_.reset ();
	}

	ICoreProxy_ptr Core::GetProxy ()
	{
		return Proxy_;
	}

	void Core::SendEntity (const Entity& e)
	{
		Proxy_->GetEntityManager ()->HandleEntity (e);
	}

	void Core::InitWithOtherPlugins ()
	{
		M_->PreviewMgr_.InitWithPlugins ();
		M_->Player_.InitWithOtherPlugins ();
		M_->RadioManager_.InitProviders ();
	}

	LMPProxy* Core::GetLmpProxy () const
	{
		return &M_->LmpProxy_;
	}

	void Core::AddPlugin (QObject *pluginObj)
	{
		auto ip2 = qobject_cast<IPlugin2*> (pluginObj);
		auto ilmpPlug = qobject_cast<ILMPPlugin*> (pluginObj);

		if (!ilmpPlug)
		{
			qWarning () << Q_FUNC_INFO
					<< pluginObj
					<< "doesn't implement ILMPPlugin";
			return;
		}

		ilmpPlug->SetLMPProxy (&M_->LmpProxy_);

		const auto& classes = ip2->GetPluginClasses ();
		if (classes.contains ("org.LeechCraft.LMP.CollectionSync") &&
			qobject_cast<ISyncPlugin*> (pluginObj))
			SyncPlugins_ << pluginObj;

		if (classes.contains ("org.LeechCraft.LMP.CloudStorage") &&
			qobject_cast<ICloudStoragePlugin*> (pluginObj))
		{
			CloudPlugins_ << pluginObj;
			emit cloudStoragePluginsChanged ();
		}

		if (classes.contains ("org.LeechCraft.LMP.PlaylistProvider") &&
			qobject_cast<IPlaylistProvider*> (pluginObj))
			M_->PLManager_.AddProvider (pluginObj);

		M_->HookInterconnector_.AddPlugin (pluginObj);
	}

	QObjectList Core::GetSyncPlugins () const
	{
		return SyncPlugins_;
	}

	QObjectList Core::GetCloudStoragePlugins () const
	{
		return CloudPlugins_;
	}

	void Core::RequestArtistBrowser (const QString& artist)
	{
		emit artistBrowseRequested (artist);
	}

	HookInterconnector* Core::GetHookInterconnector () const
	{
		return &M_->HookInterconnector_;
	}

	LocalFileResolver* Core::GetLocalFileResolver () const
	{
		return &M_->Resolver_;
	}

	LocalCollection* Core::GetLocalCollection () const
	{
		return &M_->Collection_;
	}

	CollectionsManager* Core::GetCollectionsManager () const
	{
		return &M_->CollectionsManager_;
	}

	PlaylistManager* Core::GetPlaylistManager () const
	{
		return &M_->PLManager_;
	}

	SyncManager* Core::GetSyncManager () const
	{
		return &M_->SyncManager_;
	}

	SyncUnmountableManager* Core::GetSyncUnmountableManager () const
	{
		return &M_->SyncUnmountableManager_;
	}

	CloudUploadManager* Core::GetCloudUploadManager () const
	{
		return &M_->CloudUpMgr_;
	}

	ProgressManager* Core::GetProgressManager () const
	{
		return &M_->ProgressManager_;
	}

	RadioManager* Core::GetRadioManager () const
	{
		return &M_->RadioManager_;
	}

	Player* Core::GetPlayer () const
	{
		return &M_->Player_;
	}

	PreviewHandler* Core::GetPreviewHandler () const
	{
		return &M_->PreviewMgr_;
	}

	boost::optional<MediaInfo> Core::TryURLResolve (const QUrl& url) const
	{
		return M_->PLManager_.TryResolveMediaInfo (url);
	}

	void Core::rescan ()
	{
		M_->Collection_.Rescan ();
	}
}
}
