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

namespace XGame.Framework.Asset
{
    public class AlphaTextureManifest : ScriptableObject, IDisposable
    {
        [SerializeField]
        private AtlasAlphaTextureInfo[] alphaTextureInfos;
        /// <summary>
        /// key:spriteName
        /// value:alphaTextureName
        /// </summary>
        private Dictionary<string, string> spriteMap;

        public void Init()
        {
            if (alphaTextureInfos != null)
            {
                spriteMap = new Dictionary<string, string>();
                foreach(var info in alphaTextureInfos)
                {
                    if (info.sprites != null)
                    {
                        foreach(var spr in info.sprites)
                        {
                            if (!spriteMap.ContainsKey(spr))
                            {
                                spriteMap.Add(spr, info.textureName);
                            }
                            else
                            {
                                AssetsLog.Error($"Atlas Alpha Manifest Error. Sprite repeat. name:{spr} tex:{info.textureName}");
                            }
                        }
                    }
                }
            }
        }
        public bool TryGetAlphaTextureName(string spriteName, out string textureName)
        {
            if (spriteMap != null && spriteMap.TryGetValue(spriteName, out textureName))
            {
                return true;
            }
            textureName = string.Empty;
            return false;
        }

        void IDisposable.Dispose()
        {
            if (spriteMap != null)
            {
                spriteMap.Clear();
                spriteMap = null;
            }
        }
    }
}

