
using System;
using System.IO;
using System.Collections.Generic;
using CommonMemoryStream;

namespace Animrig
{
    public class Preset
    {
        // protected
        // property
        public Dictionary<string,string> list { get; protected set; } = new Dictionary<string, string>();
        // public
        public Preset()
        {
            Clear();
        }
        public void Clear()
        {
            list.Clear();
        }
        public bool LoadFromStream(MemoryStream ms)
        {
            try
            {
                var sl = CommonMS.ReadText(ms);
                list = Misc.ConvertKeyValue(sl);
            }
            catch
            {
                return false;
            }
            return true;
        }
        public void MakeFromStringList(List<string> sl)
        {
            Clear();
            list = Misc.ConvertKeyValue(sl);
        }
        public void SetDefault(StateInput input, float duration = 0.0f, Easing mode = Easing.InOutQuad, float beta = 1.0f)
        {
            input.SetDefaults(duration, mode, beta);
        }
        public void SetPreset(StateInput input, string name, float duration = 0.0f, Easing mode = Easing.InOutQuad, float beta = 1.0f)
        {
            string s = "";
            try
            {
                if (list.ContainsKey(name)) s = list[name];
            }
            catch
            {
                s = "";
            }
            if (s == "") return;

            var sl = Misc.DivideString(',', s);
            foreach (var itemcode in sl)
            {
                var p = itemcode.IndexOf(':');
                if (p < 0) continue;

                var v1 = Misc.Slice(itemcode, 0, p);
                var v2 = Misc.Slice(itemcode, p + 1, 4096);

                var no = Misc.ToIntDef(v1, -1);
                var f = Misc.ToFloatDef(v2, 0.0f);
                if (no == -1) continue;

                input.SetValue(no, f, duration, mode, beta);
            }
        }
        public void SetClear(StateInput input, float duration = 0.0f, Easing mode = Easing.InOutQuad, float beta = 1.0f)
        {
            var count = input.ValueCount;
            for (var i = 0; i < count; i++)
            {
                input.SetValue(i, 0.0f, duration, mode, beta);
            }
        }

    }
}


