using System.IO;
using eagle;
using NUnit.Framework;
using UnityEditor;
using UnityEngine;

namespace eagle0.Tests {
    [TestFixture]
    public class FireworksEffectTests {
        private const string FireworksPrefabPath = "Assets/Eagle/Effects/Fireworks.prefab";
        private const string ClipRectParticleShaderPath =
                "Assets/Eagle/Shaders/ClipRectParticleUnlit.shader";
        private const string FireworksEffectScriptPath = "Assets/Eagle/FireworksEffect.cs";
        private const string ClipRectParticleShaderName = "Eagle/ClipRectParticleUnlit";

        [Test]
        public void FireworksPrefabUsesSerializedClipRectParticleShader() {
            var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(FireworksPrefabPath);
            Assert.NotNull(prefab, $"{FireworksPrefabPath} must exist.");

            var effect = prefab.GetComponent<FireworksEffect>();
            Assert.NotNull(effect, $"{FireworksPrefabPath} must include FireworksEffect.");

            var expectedShader = AssetDatabase.LoadAssetAtPath<Shader>(ClipRectParticleShaderPath);
            Assert.NotNull(expectedShader, $"{ClipRectParticleShaderPath} must exist.");

            Assert.AreSame(
                    expectedShader,
                    effect.clipRectParticleShader,
                    "Fireworks should serialize the project-owned clipped particle shader.");
            Assert.AreEqual(ClipRectParticleShaderName, effect.clipRectParticleShader.name);
        }

        [Test]
        public void FireworksRuntimeCodeDoesNotFindParticleShadersByName() {
            StringAssert.DoesNotContain(
                    "Shader.Find",
                    File.ReadAllText(FireworksEffectScriptPath),
                    "FireworksEffect should use its serialized clipRectParticleShader.");
        }
    }
}
