//#pragma once
#ifndef AnimationrigPreset_H
#define AnimationrigPreset_H

#include <CommonMemoryStream.h>
#include <AnimationrigMisc.h>
#include <Animationrig.h>
#include <AnimationrigState.h>

#include <vector>
#include <string>
#include <map>

using namespace std;
using namespace CommonMemoryStream;

namespace Animrig {
	class Preset {
	public:
		map<wstring, wstring> list;

		Preset() {
			clear();
		}
		virtual ~Preset() {}

		void clear() {
			list.clear();
		}

		void makeFromStringList(const vector<wstring> &sl)
		{
			Misc::convertKeyValue(list, sl);
		}
		bool loadFromStream(MemoryStream *ms)
		{
			clear();

			try
			{
				vector<wstring> sl;
				CommonMS::readText(ms,sl);
				Misc::convertKeyValue(list,sl);
			}
			catch(...)
			{
				return false;
			}
			return true;
		}
		void setDefault(StateInput *input, float duration = 0.0f, Easing mode = Easing::InOutQuad, float beta = 1.0f)
		{
			input->setDefaults(duration, mode, beta);
		}
		void setPreset(StateInput *input, const wstring &name, float duration = 0.0f, Easing mode = Easing::InOutQuad, float beta = 1.0f)
		{
			wstring s = L"";
			try
			{
				if (list.count(name) > 0) s = list[name];
			}
			catch(...)
			{
				s = L"";
			}
			if (s == L"") return;

			auto sl = Misc::divideString(',', s);
			for (auto &itemcode : *sl)
			{
				auto p = (int)itemcode.find(':');
				if (p < 0) continue;

				auto v1 = Misc::slice(itemcode, 0, p);
				auto v2 = Misc::slice(itemcode, p + 1, 4096);

				auto no = Misc::toIntDef(v1, -1);
				auto f = Misc::toFloatDef(v2, 0.0f);
				if (no == -1) continue;

				input->setValue(no, f, duration, mode, beta);
			}
			delete sl;
		}
		void setClear(StateInput *input, float duration = 0.0f, Easing mode = Easing::InOutQuad, float beta = 1.0f)
		{
			auto count = input->valueCount();
			for (auto i = 0; i < count; i++)
			{
				input->setValue(i, 0.0f, duration, mode, beta);
			}
		}
	protected:
	};
}

#endif