//
// Copyright (c) 2014 ANSCENTER. All rights reserved.
//

#include "precomp.h"
#include "anslicensing.h"
#include "template.h"
#include "base32.h"
#include "base64.h"
#include "sha1.h"
#include "except.h"
#include "bitstream.h"
#include "uniconv.h"
#include "generator.h"

namespace ANSCENTER {
	namespace Licensing {
		template<>
		KeyGeneratorT<char>::KeyGeneratorT():
		m_Impl( *new KeyGeneratorImpl())
		{

		}

		template<>
		KeyGeneratorT<wchar_t>::KeyGeneratorT():
		m_Impl( *new KeyGeneratorImpl())
		{

		}

		template<>
		KeyGeneratorT<char>::KeyGeneratorT(const LicenseTemplateT<char> * templ):
		m_Impl( *new KeyGeneratorImpl(&templ->m_Impl) )
		{

		}

		template<>
		KeyGeneratorT<wchar_t>::KeyGeneratorT(const LicenseTemplateT<wchar_t> * templ):
		m_Impl( *new KeyGeneratorImpl(&templ->m_Impl) )
		{

		}

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

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

		template<>
		KeyGeneratorT<char> * KeyGeneratorT<char>::Create()
		{
			return new KeyGeneratorT<char>();
		}

		template<>
		KeyGeneratorT<wchar_t> * KeyGeneratorT<wchar_t>::Create()
		{
			return new KeyGeneratorT<wchar_t>();
		}

		template<>
		void KeyGeneratorT<char>::Destroy(KeyGeneratorT<char> * obj)
		{
			delete obj;
		}

		template<>
		void KeyGeneratorT<wchar_t>::Destroy(KeyGeneratorT<wchar_t> * obj)
		{
			delete obj;
		}

		template<>
		void KeyGeneratorT<char>::SetKeyData(const char * fieldName, const void * buf, int len)
		{
			m_Impl.KeyGeneratorImpl::SetKeyData(fieldName, buf, len);
		}

		template<>
		void KeyGeneratorT<wchar_t>::SetKeyData(const wchar_t * fieldName, const void * buf, int len)
		{
			m_Impl.KeyGeneratorImpl::SetKeyData(w2s(fieldName).c_str(), buf, len);
		}

		template<>
		void KeyGeneratorT<char>::SetKeyData(const char * fieldName, int data)
		{
			
			m_Impl.SetKeyData(fieldName, data);
		}

		template<>
		void KeyGeneratorT<wchar_t>::SetKeyData(const wchar_t * fieldName, int data)
		{
			m_Impl.SetKeyData(w2s(fieldName).c_str(), data);
		}

		template<>
		void KeyGeneratorT<char>::SetKeyData(const char * fieldName, const char * data)
		{
			m_Impl.SetKeyData(fieldName, data);
		}

		template<>
		void KeyGeneratorT<wchar_t>::SetKeyData(const wchar_t * fieldName, const wchar_t * data)
		{
			m_Impl.SetKeyData(w2s(fieldName).c_str(), w2s(data).c_str());
		}

		template<>
		void KeyGeneratorT<char>::SetKeyData(const char * fieldName, int year, int month, int day)
		{
			m_Impl.SetKeyData(fieldName, year, month, day);
		}

		template<>
		void KeyGeneratorT<wchar_t>::SetKeyData(const wchar_t * fieldName, int year, int month, int day)
		{
			m_Impl.SetKeyData(w2s(fieldName).c_str(), year, month, day);
		}

		template<>
		void KeyGeneratorT<char>::SetValidationData(const char * fieldName, const void * buf, int len)
		{
			
			m_Impl.SetValidationData(fieldName, buf, len);
		}

		template<>
		void KeyGeneratorT<wchar_t>::SetValidationData(const wchar_t * fieldName, const void * buf, int len)
		{
			m_Impl.SetValidationData(w2s(fieldName).c_str(), buf, len);
		}

		template<>
		void KeyGeneratorT<char>::SetValidationData(const char * fieldName, int data)
		{
			m_Impl.SetValidationData(fieldName, data);
		}

		template<>
		void KeyGeneratorT<wchar_t>::SetValidationData(const wchar_t * fieldName, int data)
		{
			m_Impl.SetValidationData(w2s(fieldName).c_str(), data);
		}

		template<>
		void KeyGeneratorT<char>::SetValidationData(const char * fieldName, const char * data)
		{
			m_Impl.SetValidationData(fieldName, data);
		}

		template<>
		void KeyGeneratorT<wchar_t>::SetValidationData(const wchar_t * fieldName, const wchar_t * data)
		{
			m_Impl.SetValidationData(w2s(fieldName).c_str(), w2s(data).c_str());
		}

		template<>
		void KeyGeneratorT<char>::SetKeyTemplate(const LicenseTemplateT<char> & templ)
		{
			m_Impl.SetKeyTemplate(&templ.m_Impl);
		}

		template<>
		void KeyGeneratorT<wchar_t>::SetKeyTemplate(const LicenseTemplateT<wchar_t> & templ)
		{
			m_Impl.SetKeyTemplate(&templ.m_Impl);
		}

		template<>
		void KeyGeneratorT<char>::SetKeyTemplate(const LicenseTemplateT<char> * templ)
		{
			m_Impl.SetKeyTemplate(&templ->m_Impl);
		}

		template<>
		void KeyGeneratorT<wchar_t>::SetKeyTemplate(const LicenseTemplateT<wchar_t> * templ)
		{
			m_Impl.SetKeyTemplate(&templ->m_Impl);
		}

		template<>
		const char * KeyGeneratorT<char>::GenerateKey()
		{
			return m_Impl.GenerateKey();
		}

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