using System.Reflection;
using Awperative;


namespace AwperativeTest2;


public class ConsoleRegistry : Component
{
    protected override void Create() {
        Priority = int.MinValue;
    }
    
    protected override void Update() {
        Console.Clear();

        List<Component> Targets = Scene.Components.ToList();
        for(int i = 0; i < Targets.Count; i++) {
            Targets.InsertRange(i + 1, Targets[i].Components);

            string output = "| ";
            for (int d = 0; d < Targets[i].Parents.Length; d++) {
                output += "    ";
            }
            
            //write name and code
            Console.WriteLine(output + Targets[i].GetType().Name + " - " + Targets[i].GetHashCode());

            List<string> VariableNames = [];
            List<Object?> ShownVariables = [];

            foreach (FieldInfo field in Targets[i].GetType().GetFields()) {
                foreach (Attribute attribute in field.GetCustomAttributes(typeof(Show), false)) {
                    if (attribute is Show show) {
                        VariableNames.Add(field.Name);
                        ShownVariables.Add(field.GetValue(Targets[i]));
                    }
                }
            }
            
            foreach (PropertyInfo property in Targets[i].GetType().GetProperties()) {
                foreach (Attribute attribute in property.GetCustomAttributes(typeof(Show), false)) {
                    if (attribute is Show show) {
                        VariableNames.Add(property.Name);
                        ShownVariables.Add(property.GetValue(Targets[i]));
                    }
                }
            }
            

            for(int s = 0; s < ShownVariables.Count; s++) {
                Console.WriteLine(output + "    " + VariableNames[s] + "  -  " + ShownVariables[s]);
            }
            
            Console.WriteLine("|");
        }
    }
}