/**********************************************************************
 * 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 "audiocall.h"
#include <QFuture>
#include <util/sll/functional.h>
#include <util/sll/slotclosure.h>
#include <util/sll/visitor.h>
#include <util/threads/futures.h>
#include "threadexceptions.h"
#include "audiocalldevice.h"
#include "toxcontact.h"

namespace LC
{
namespace Azoth
{
namespace Sarin
{
	namespace
	{
		void InitFormat (QAudioFormat& format)
		{
			format.setSampleSize (16);
			format.setByteOrder (QSysInfo::ByteOrder == QSysInfo::BigEndian ?
					QAudioFormat::BigEndian :
					QAudioFormat::LittleEndian);
			format.setCodec ("audio/pcm");
			format.setSampleType (QAudioFormat::SignedInt);
		}
	}

	AudioCall::AudioCall (const ToxContact *contact, CallManager *callMgr, Direction dir)
	: SourceId_ { contact->GetEntryID () }
	, SourcePubkey_ { contact->GetPubKey () }
	, Dir_ { dir }
	, CallMgr_ { callMgr }
	{
		connect (CallMgr_,
				&CallManager::callStateChanged,
				this,
				&AudioCall::HandleCallStateChanged);
		connect (CallMgr_,
				&CallManager::gotFrameParams,
				this,
				&AudioCall::HandleReadFrameParams);

		InitFormat (ReadFmt_);

		InitFormat (WriteFmt_);
		WriteFmt_.setChannelCount (1);
		WriteFmt_.setSampleRate (24000);
	}

	void AudioCall::SetCallIdx (const std::optional<qint32>& idx)
	{
		if (!idx)
		{
			qWarning () << Q_FUNC_INFO
					<< "no call idx";
			emit stateChanged (SFinished);
			return;
		}

		CallIdx_ = *idx;

		Device_ = std::make_shared<AudioCallDevice> (CallIdx_, CallMgr_);
		Device_->open (QIODevice::ReadWrite);
		Device_->SetWriteFormat (WriteFmt_);

		if (Dir_ == DOut)
			InitiateCall ();
	}

	IMediaCall::Direction AudioCall::GetDirection () const
	{
		return Dir_;
	}

	QString AudioCall::GetSourceID () const
	{
		return SourceId_;
	}

	void AudioCall::Accept ()
	{
		if (Dir_ == DOut)
			return;

		Util::Sequence (this, CallMgr_->AcceptCall (CallIdx_)) >>
				Util::Visitor
				{
					Util::BindMemFn (&AudioCall::HandleWriteParams, this),
					[this] (const auto& err)
					{
						qWarning () << Q_FUNC_INFO
								<< "error accepting the call:"
								<< Util::Visit (err, [] (auto&& e) { return e.what (); });
						emit stateChanged (SFinished);
					}
				};
	}

	void AudioCall::Hangup ()
	{
	}

	QIODevice* AudioCall::GetAudioDevice ()
	{
		return Device_.get ();
	}

	QAudioFormat AudioCall::GetAudioReadFormat () const
	{
		return ReadFmt_;
	}

	QAudioFormat AudioCall::GetAudioWriteFormat () const
	{
		return WriteFmt_;
	}

	QIODevice* AudioCall::GetVideoDevice ()
	{
		return nullptr;
	}

	void AudioCall::InitiateCall ()
	{
		Util::Sequence (this, CallMgr_->InitiateCall (SourcePubkey_.toUtf8 ())) >>
				Util::Visitor
				{
					[this] (const CallManager::AudioFormatParams& params)
					{
						HandleWriteParams (params);
						emit stateChanged (SConnecting);
					},
					[this] (const auto& err)
					{
						qWarning () << Q_FUNC_INFO
								<< "error initiating the call:"
								<< Util::Visit (err, [] (auto&& e) { return e.what (); });
						emit stateChanged (SFinished);
					}
				};
	}

	void AudioCall::HandleWriteParams (const CallManager::AudioFormatParams&)
	{
	}

	void AudioCall::HandleReadFrameParams (int32_t callIdx, int channels, int sampleRate)
	{
		if (callIdx != CallIdx_)
			return;

		if (ReadFmt_.channelCount () != channels ||
				ReadFmt_.sampleRate () != sampleRate)
		{
			ReadFmt_.setChannelCount (channels);
			ReadFmt_.setSampleRate (sampleRate);

			emit readFormatChanged ();
		}
	}

	void AudioCall::HandleCallStateChanged (int32_t callIdx, uint32_t state)
	{
		if (callIdx != CallIdx_)
			return;

		qDebug () << Q_FUNC_INFO << state;

		if (state & TOXAV_FRIEND_CALL_STATE_ERROR)
		{
			emit stateChanged (SFinished);
			qWarning () << Q_FUNC_INFO
					<< "got error state";
			return;
		}

		if (state & TOXAV_FRIEND_CALL_STATE_FINISHED)
		{
			emit stateChanged (SFinished);
			return;
		}

		if ((state & TOXAV_FRIEND_CALL_STATE_ACCEPTING_V) ||
				(state & TOXAV_FRIEND_CALL_STATE_SENDING_V))
			qWarning () << Q_FUNC_INFO
					<< "cannot handle videos yet";

		if (state & TOXAV_FRIEND_CALL_STATE_ACCEPTING_A)
			CurrentMode_ |= QIODevice::ReadOnly;

		if (state & TOXAV_FRIEND_CALL_STATE_SENDING_A)
			CurrentMode_ |= QIODevice::WriteOnly;

		if (CurrentMode_ == QIODevice::ReadWrite)
			emit stateChanged (SActive);

		emit audioModeChanged (CurrentMode_);
	}
}
}
}
