﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace JLGames.RocketDriver.Actions.AssetIndex
{
    [Serializable]
    public class DirFilesIndex : ScriptableObject
    {
        [SerializeField] private bool m_IncludeSelf = false;
        [SerializeField] private bool m_IncludeSubDir = false;
        [SerializeField] private string m_BasePath;
        [SerializeField] private List<NamePathInfo> m_NameToPath = new List<NamePathInfo>();

#if UNITY_EDITOR
        public bool IncludeSelf => m_IncludeSelf;
        public bool IncludeSubDir => m_IncludeSubDir;
        public string BasePath => m_BasePath;
#endif

        public override string ToString()
        {
            return $"{{IncludeSelf={m_IncludeSelf}, IncludeSubDir={m_IncludeSubDir}, BasePath=\"{m_BasePath}\"}}";
        }

        /// <summary>
        /// Size of indexed.
        /// 索引数量
        /// </summary>
        public int Size => m_NameToPath.Count;

        /// <summary>
        /// Fetch data by subscript
        /// 通过下标取数据
        /// </summary>
        /// <param name="index"></param>
        public NamePathInfo this[int index] => m_NameToPath[index];

        /// <summary>
        /// Get path by name
        /// 通过名称取路径
        /// </summary>
        /// <param name="nameKey"></param>
        /// <returns></returns>
        public string GetPath(string nameKey)
        {
            if (string.IsNullOrEmpty(nameKey)) return null;
            return m_NameToPath.Find(info => info.Name == nameKey)?.Path;
        }

        /// <summary>
        /// Foreach
        /// 遍历
        /// </summary>
        /// <param name="eachAction"></param>
        public void Foreach(NamePathInfo.Each eachAction)
        {
            if (null == eachAction || null == m_NameToPath || m_NameToPath.Count == 0) return;
            m_NameToPath.ForEach(info => { eachAction.Invoke(info.Name, info.Path); });
        }
    }
}