#include "precomp.h"

#include <memory>

#include "licensingclient.h"
#include "licensevalidationargs.h"
#include "licensevalidationresult.h"

using namespace std;

namespace ANSCENTER {
	namespace Licensing {

		template<>
		LicensingClientT<wchar_t>::LicensingClientT():
		m_Impl(*(new LicensingClientImpl(this, true)))
		{

		}

		template<>
		LicensingClientT<char>::LicensingClientT(): 
		m_Impl(*(new LicensingClientImpl(this, false)))
		{

		}

		template<>
		LicensingClientT<char>::~LicensingClientT()
		{
			delete & m_Impl;
		}

		template<>
		LicensingClientT<wchar_t>::~LicensingClientT()
		{
			delete & m_Impl;
		}

		template<>
		void LicensingClientT<char>::SetLicenseTemplate(LicenseTemplateT<char> * tmpl)
		{
			m_Impl.SetLicenseTemplate(&tmpl->m_Impl);
		}

		template<>
		void LicensingClientT<wchar_t>::SetLicenseTemplate(LicenseTemplateT<wchar_t> * tmpl)
		{
			m_Impl.SetLicenseTemplate(&tmpl->m_Impl);
		}

		template<>
		void LicensingClientT<char>::SetLicenseKey(const char * key)
		{
			m_Impl.SetLicenseKey(key);
		}

		template<>
		void LicensingClientT<wchar_t>::SetLicenseKey(const wchar_t * key)
		{
			m_Impl.SetLicenseKey(w2s(key).c_str());
		}

		template<>
		void LicensingClientT<char>::SetActivationKey(const char * key)
		{
			m_Impl.SetActivationKey(key);
		}

		template<>
		void LicensingClientT<wchar_t>::SetActivationKey(const wchar_t * key)
		{
			m_Impl.SetActivationKey(w2s(key).c_str());
		}

		template<>
		const char * LicensingClientT<char>::GetActivationKey()
		{
			return m_Impl.GetActivationKey();
		}

		template<>
		const wchar_t * LicensingClientT<wchar_t>::GetActivationKey()
		{
			static wstring wstr1;
			
			wstr1 = s2w(m_Impl.GetActivationKey());
			return wstr1.c_str();
		}

		template<>
		void LicensingClientT<char>::SetHardwareId(const char * hwid)
		{
			m_Impl.SetHardwareId(hwid);
		}

		template<>
		void LicensingClientT<wchar_t>::SetHardwareId(const wchar_t * hwid)
		{
			m_Impl.SetHardwareId(w2s(hwid).c_str());
		}

		template<>
		const char * LicensingClientT<char>::GetHardwareId()
		{
			return m_Impl.GetHardwareId();
		}

		template<>
		const wchar_t * LicensingClientT<wchar_t>::GetHardwareId()
		{
			static wstring wstr1;
			
			wstr1 = s2w(m_Impl.GetHardwareId());
			return wstr1.c_str();
		}

		template<>
		const wchar_t * LicensingClientT<wchar_t>::GetCurrentHardwareId()
		{
			static wstring wstr1;
			
			wstr1 = s2w(m_Impl.GetCurrentHardwareId());
			return wstr1.c_str();			
		}

		template<>
		const char * LicensingClientT<char>::GetCurrentHardwareId()
		{
			return m_Impl.GetCurrentHardwareId();
		}

		template<>
		bool LicensingClientT<char>::MatchCurrentHardwareId(const char * hwid)
		{
			return m_Impl.MatchCurrentHardwareId(hwid);
		}

		template<>
		bool LicensingClientT<wchar_t>::MatchCurrentHardwareId(const wchar_t * hwid)
		{
			return m_Impl.MatchCurrentHardwareId(w2s(hwid).c_str());
		}

		template<>
		void LicensingClientT<char>::SetLicensingServiceUrl(const char * url)
		{
			
			m_Impl.SetLicensingServiceUrl(url);
		}

		template<>
		void LicensingClientT<wchar_t>::SetLicensingServiceUrl(const wchar_t * url)
		{
			m_Impl.SetLicensingServiceUrl(w2s(url).c_str());
		}

		template<>
		void LicensingClientT<char>::SetLicenseTemplateId(const char * templateId)
		{	
			m_Impl.SetLicenseTemplateId(templateId);
		}

		template<>
		void LicensingClientT<wchar_t>::SetLicenseTemplateId(const wchar_t * templateId)
		{
			m_Impl.SetLicenseTemplateId(w2s(templateId).c_str());
		}

		template<>
		void LicensingClientT<char>::AcquireLicense()
		{
			m_Impl.AcquireLicense();
		}

		template<>
		void LicensingClientT<wchar_t>::AcquireLicense()
		{
			m_Impl.AcquireLicense();
		}

		template<>
		bool LicensingClientT<char>::IsLicenseValid()
		{
			return m_Impl.IsLicenseValid();
		}

		template<>
		bool LicensingClientT<wchar_t>::IsLicenseValid()
		{
			return m_Impl.IsLicenseValid();
		}

		template<>
		LicenseValidationResultT<char> * LicensingClientT<char>::ValidateLicense(LicenseValidationArgsT<char> * args)
		{
			static LicenseValidationResultT<char> result;

			auto& resultImpl = m_Impl.ValidateLicense(&args->m_Impl);

			result.m_Impl.MoveFrom(resultImpl);

			return &result;
		}

		template<>
		LicenseValidationResultT<wchar_t> * LicensingClientT<wchar_t>::ValidateLicense(LicenseValidationArgsT<wchar_t> * args)
		{
			static LicenseValidationResultT<wchar_t> result;

			auto& resultImpl = m_Impl.ValidateLicense(&args->m_Impl);

			result.m_Impl.MoveFrom(resultImpl);

			return &result;
		}

		template<>
		void LicensingClientT<char>::SetLicenseKeyValidationData(void * data, int len)
		{
			return m_Impl.SetLicenseKeyValidationData(data, len);
		}

		template<>
		void LicensingClientT<wchar_t>::SetLicenseKeyValidationData(void * data, int len)
		{
			return m_Impl.SetLicenseKeyValidationData(data, len);
		}

		template<>
		int LicensingClientT<char>::GetLicenseActivationStatus()
		{
			return m_Impl.GetLicenseActivationStatus();
		}

		template<>
		int LicensingClientT<wchar_t>::GetLicenseActivationStatus()
		{
			return m_Impl.GetLicenseActivationStatus();
		}

		template<>
		void LicensingClientT<char>::GetLicenseExpirationDate(int * year, int * month, int * day)
		{
			m_Impl.GetLicenseExpirationDate(year, month, day);
		}

		template<>
		void LicensingClientT<wchar_t>::GetLicenseExpirationDate(int * year, int * month, int * day)
		{
			m_Impl.GetLicenseExpirationDate(year, month, day);
		}

		template<>
		void LicensingClientT<char>::SetTimeValidationMethod(int method)
		{
			return m_Impl.SetTimeValidationMethod(method);
		}

		template<>
		void LicensingClientT<wchar_t>::SetTimeValidationMethod(int method)
		{
			return m_Impl.SetTimeValidationMethod(method);
		}
	};
};

const wchar_t * GetCurrentHardwareIdW(void * licensingClient)
{
	
	static std::wstring result = ((ANSCENTER::Licensing::LicensingClientT<wchar_t> *)licensingClient)->GetCurrentHardwareId();
	return result.c_str();
}

const char * GetCurrentHardwareIdA(void * licensingClient)
{
	return ((ANSCENTER::Licensing::LicensingClientT<char> *)licensingClient)->GetCurrentHardwareId();
}

bool MatchCurrentHardwareIdW(void * licensingClient, const wchar_t * hwid)
{
	
	return ((ANSCENTER::Licensing::LicensingClientT<wchar_t> *)licensingClient)->MatchCurrentHardwareId(hwid);
}

bool MatchCurrentHardwareIdA(void * licensingClient, const char * hwid)
{
	return ((ANSCENTER::Licensing::LicensingClientT<char> *)licensingClient)->MatchCurrentHardwareId(hwid);
}
