﻿using System.Collections.Generic;
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace JLGames.RocketDriver.Actions.Service
{
    public class ServiceSettings : ScriptableObject
    {
        [SerializeField] private List<ServiceNode> m_SavedList = new List<ServiceNode>();
        [SerializeField] private List<ServiceNode> m_EnabledList = new List<ServiceNode>();

        public ServiceNode[] SavedServices => m_SavedList.ToArray();
        
        public ServiceNode[] EnabledServices => m_EnabledList.ToArray();

#if UNITY_EDITOR
        public void AddToSaveList(ServiceNode asset)
        {
            if (!Add2Save(asset)) return;

            AssetDatabase.AddObjectToAsset(asset, this);
            AssetDatabase.SaveAssets();
        }

        public void RemoveFromSaveList(ServiceNode asset)
        {
            if (!RemoveFromSave(asset)) return;

            AssetDatabase.RemoveObjectFromAsset(asset);
            AssetDatabase.SaveAssets();
        }

        public void InsertToEnableHead(ServiceNode asset, ServiceNode target = null)
        {
            if (!InsertHead(asset, target)) return;

            AssetDatabase.SaveAssets();
        }

        public void InsertToEnableTail(ServiceNode asset, ServiceNode target = null)
        {
            if (!InsertTail(asset, target)) return;

            AssetDatabase.SaveAssets();
        }

        public void RemoveFromEnableList(ServiceNode asset)
        {
            if (null == asset) return;
            var b = m_EnabledList.Remove(asset);
            if (b)
            {
                AssetDatabase.SaveAssets();
            }
        }
#endif

        private bool Add2Save(ServiceNode asset)
        {
            if (null == asset) return false;
            if (m_SavedList.Contains(asset)) return false;
            m_SavedList.Add(asset);
            return true;
        }

        private bool RemoveFromSave(ServiceNode asset)
        {
            if (null == asset) return false;

            var b1 = m_SavedList.Remove(asset);
            var b2 = m_EnabledList.Remove(asset);
            return b1 || b2;
        }

        private bool InsertHead(ServiceNode asset, ServiceNode target = null)
        {
            if (null == asset || m_EnabledList.Contains(asset)) return false;
            if (null == target)
            {
                m_EnabledList.Insert(0, asset);
            }
            else
            {
                var index = m_EnabledList.FindIndex(infoAsset => infoAsset == target);
                m_EnabledList.Insert(index, asset);
            }

            return true;
        }

        private bool InsertTail(ServiceNode asset, ServiceNode target = null)
        {
            if (null == asset || m_EnabledList.Contains(asset)) return false;
            if (null == target)
            {
                m_EnabledList.Add(asset);
            }
            else
            {
                var index = m_EnabledList.FindIndex(infoAsset => infoAsset == target);
                m_EnabledList.Insert(index + 1, asset);
            }

            return true;
        }
    }
}