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

namespace mgaia.tk2d
{
    /// <summary>
    /// Classe utilizada para realizar checagem em sprites collections
    /// </summary>
    /// <owner>Victor</owner>
    public static class SpriteCollectionConsistencyChecker
    {
        /// <summary>
        /// Retorna as sprites collections utilizadas por todas as sprites
        /// </summary>
        /// <owner>Victor</owner>
        public static tk2dSpriteCollectionData[] GetUsedSpriteCollections(Func<tk2dSpriteCollectionData>[] elements)
        {
            List<tk2dSpriteCollectionData> spriteCollections = new List<tk2dSpriteCollectionData>();

            foreach (Func<tk2dSpriteCollectionData> element in elements)
            {
                tk2dSpriteCollectionData usedSpriteCollection = element();

                if (!spriteCollections.Contains(usedSpriteCollection))
                {
                    spriteCollections.Add(usedSpriteCollection);
                }
            }

            return spriteCollections.ToArray();
        }

        /// <summary>
        /// Checa se todos os elementos estão usando sprite collections com configurações de câmera que condizem com o padrão
        /// </summary>
        /// <owner>Victor</owner>
        public static bool CheckSpriteCollectionCameraConfiguration(Func<tk2dSpriteCollectionData>[] elements, float usedOrtographicSize, float targetHeight)
        {
            foreach (tk2dSpriteCollectionData spriteCollection in GetUsedSpriteCollections(elements))
            {
                if (spriteCollection.halfTargetHeight != targetHeight / 2 || spriteCollection.invOrthoSize != usedOrtographicSize)
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Testa se o jogo está utilizando mais de uma sprite collection dentro de uma estrutura de game objects
        /// </summary>
        /// <owner>Victor</owner>
        public static bool CheckSpriteCollectionLimit(Func<tk2dSpriteCollectionData>[] elements, int maxNumber)
        {
            return GetUsedSpriteCollections(elements).Length <= maxNumber;
        }
    }
}