using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEngine;

namespace Testing.Root
{
    // TODO, Comment
    public class Tester : MonoBehaviour
    {
        public bool test;

        protected void Awake()
        {
            if (!test)
            {
                return;
            }

            Debug.LogWarning("Testing...");

            Type[] types = Assembly.GetExecutingAssembly().GetTypes();
            types = types.Where(i => i.Namespace != null && i.IsSubclassOf(typeof(Tests))).ToArray();
            //Debug.Log(string.Join(", ", types.Select(i => i.Name).ToArray()));

            for(int i = 0; i < types.Length; i++)
            {
                object obj = Activator.CreateInstance(types[i]);
                MethodInfo[] methodInfos = types[i].GetMethods();
                MethodInfo setupMethod = types[i].GetMethod("Setup");
                MethodInfo shutdownMethod = types[i].GetMethod("Shutdown");
                methodInfos = methodInfos.Where(m => m.Name != null && m.Name.Contains("Test")).ToArray();
                for(int j = 0; j < methodInfos.Length; j++)
                {
                    if (setupMethod != null)
                    {
                        setupMethod.Invoke(obj, null);
                    }
                    methodInfos[j].Invoke(obj, null);
                    if (shutdownMethod != null)
                    {
                        shutdownMethod.Invoke(obj, null);
                    }
                }
                MethodInfo resultMethod = types[i].GetMethod("GetAssertionStatus");
                Debug.LogWarning(types[i].Name + " assertions " + (string)resultMethod.Invoke(obj, null) + " passed.");
            }
            
        }
    }
}
