using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System;
using static CommunicationEvents;

public class DisplayFacts : MonoBehaviour
{
    public static Dictionary<Type, GameObject> prefabDictionary;

    public static Dictionary<string, GameObject> displayedFacts = new();

    [SerializeField] private GameObject factSpotPrefab;

    public Transform factscreenContent;

    [Header("FactPrefabs")]
    public GameObject prefab_Point;
    public GameObject prefab_Distance;
    public GameObject prefab_Angle;
    public GameObject prefab_Default;
    public GameObject prefab_OnLine;
    public GameObject prefab_Line;
    public GameObject prefab_ParallelLineFact;
    public GameObject prefab_RectangleFact;
    public GameObject prefab_RadiusFact;
    public GameObject prefab_AreaCircle;
    public GameObject prefab_ConeVolume;
    public GameObject prefab_OrthogonalCircleLine;
    public GameObject prefab_TruncatedConeVolume;
    public GameObject prefab_RightAngle;
    public GameObject prefab_CylinderVolume;
    public GameObject prefab_EqualFact;
    public GameObject prefab_UnEqualFact;

    public GameObject prefab_CircleFact;
    public GameObject prefab_OnCircleFact;
    public GameObject prefab_AngleCircleLineFact;

    public GameObject prefab_TestFact;


    //Start is called before the first frame update
    void Start()
    {
        prefabDictionary = new Dictionary<Type, GameObject>() {
            {typeof(PointFact), prefab_Point},
            {typeof(LineFact), prefab_Distance},
            {typeof(RayFact), prefab_Line},
            {typeof(AngleFact), prefab_Angle},
            {typeof(OnLineFact), prefab_OnLine},
            {typeof(ParallelLineFact), prefab_ParallelLineFact},

            {typeof(CircleFact), prefab_CircleFact},
            {typeof(OnCircleFact), prefab_OnCircleFact},
            {typeof(AngleCircleLineFact), prefab_AngleCircleLineFact},
            {typeof(RadiusFact), prefab_RadiusFact},
            {typeof(AreaCircleFact), prefab_AreaCircle},
            {typeof(ConeVolumeFact), prefab_ConeVolume},
            {typeof(OrthogonalCircleLineFact), prefab_OrthogonalCircleLine },
            {typeof(TruncatedConeVolumeFact), prefab_TruncatedConeVolume },
            {typeof(RightAngleFact), prefab_RightAngle },
            {typeof(CylinderVolumeFact), prefab_CylinderVolume},
            {typeof(EqualCirclesFact), prefab_EqualFact },
            {typeof(UnEqualCirclesFact), prefab_UnEqualFact },

            {typeof(TestFact), prefab_TestFact },
        };

        AddFactEvent.AddListener(AddFact);
        RemoveFactEvent.AddListener(RemoveFact);
        AnimateExistingFactEvent.AddListener(AnimateFact);
    }

    public void AddFact(Fact fact) {
        var display = CreateDisplay(transform, fact);
        display.transform.localPosition = Vector3.zero;
        displayedFacts.Add(fact.Id, display);
    }

    public void RemoveFact(Fact fact)
    {
        // destroy factSpot (parent of displayed fact) and the fact display with it
        Destroy(displayedFacts[fact.Id].transform.parent);
        displayedFacts.Remove(fact.Id);
    }

    public void AnimateFact(Fact fact) {
        var factIcon = displayedFacts[fact.Id];
        factIcon.GetComponentInChildren<ImageHintAnimation>().AnimationTrigger();
    }

    private GameObject CreateDisplay(Transform transform, Fact fact)
    {
        var spot = Instantiate(factSpotPrefab, factscreenContent);
        return fact.instantiateDisplay(prefabDictionary[fact.GetType()], spot.transform);
    }
}