﻿namespace JLGames.RocketDriver.Games.PanelManager
{
    public class PanelAnimSettings : IPanelAnimSettings
    {
        private const char Sep = ':';

        private string m_OpenKey;
        private string m_OpenState;
        private string m_CloseKey;
        private string m_CloseState;

        public string OpenKey => m_OpenKey;
        public string OpenState => m_OpenState;
        public string CloseKey => m_CloseKey;
        public string CloseState => m_CloseState;

        public bool IncludeOpenAnim => !string.IsNullOrEmpty(m_OpenKey);
        public bool IncludeOpenState => !string.IsNullOrEmpty(m_OpenState);
        public bool IncludeCloseAnim => !string.IsNullOrEmpty(m_CloseKey);
        public bool IncludeCloseState => !string.IsNullOrEmpty(m_CloseState);

        public PanelAnimSettings(string showAnim, string closeAnim)
        {
            ParseAnimStr(showAnim, out m_OpenKey, out m_OpenState);
            ParseAnimStr(closeAnim, out m_CloseKey, out m_CloseState);
        }

        public PanelAnimSettings(string openKey, string openState, string closeKey, string closeState)
        {
            m_OpenKey = openKey;
            m_OpenState = openState;
            m_CloseKey = closeKey;
            m_CloseState = closeState;
        }

        private void ParseAnimStr(string animStr, out string key, out string state)
        {
            if (string.IsNullOrEmpty(animStr))
            {
                key = null;
                state = null;
                return;
            }

            if (!animStr.Contains($"{Sep}"))
            {
                key = animStr;
                state = null;
                return;
            }

            var index = animStr.LastIndexOf(Sep);
            key = animStr.Substring(0, index);
            state = animStr.Substring(index + 1);
        }
    }
}