﻿using System.Collections.Generic;
using UnityEngine;

namespace JLGames.RocketDriver.Editor.Infra
{
    public class MissingReferences
    {
        public delegate void EachMissingReference(PropertyPaths missing);

        private readonly Dictionary<Component, PropertyPaths> m_MissingMap = new Dictionary<Component, PropertyPaths>();

        public int MissingSize
        {
            get
            {
                if (m_MissingMap.Count == 0) return 0;
                var size = 0;
                foreach (var pathse in m_MissingMap)
                {
                    size += pathse.Value.PathSize;
                }

                return size;
            }
        }

        public bool ContainsComponent(Component comp)
        {
            return null != comp && m_MissingMap.ContainsKey(comp);
        }

        public void AppendMissingPath(Component comp, string path)
        {
            if (null == comp) return;

            if (m_MissingMap.ContainsKey(comp))
            {
                m_MissingMap[comp].AppendPath(path);
                return;
            }

            m_MissingMap[comp] = new PropertyPaths(comp, path);
        }

        public void Foreach(EachMissingReference eachFunc)
        {
            if (m_MissingMap.Count == 0 || eachFunc == null) return;
            foreach (var pathse in m_MissingMap)
            {
                eachFunc.Invoke(pathse.Value);
            }
        }
    }
}