using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;

[RequireComponent(typeof(ARTrackedImageManager))]

public class ImageTracking : MonoBehaviour
{
	[SerializeField]
	private GameObject[] placeablePrefabs;

	private Dictionary<string, GameObject> spawnedPrefabs = new Dictionary<string, GameObject>();
	private ARTrackedImageManager trackedImageManager;

	private void Awake()
	{
		trackedImageManager = FindObjectOfType<ARTrackedImageManager>();

		foreach(GameObject prefab in placeablePrefabs)
		{
			GameObject newPrefab = Instantiate(prefab, Vector3.zero, Quaternion.identity);
			newPrefab.name = prefab.name;
			spawnedPrefabs.Add(prefab.name, newPrefab);
		}
	}

	private void OnEnable()
	{
		trackedImageManager.trackedImagesChanged += ImageChnaged;
	}

	private void OnDisable()
	{
		trackedImageManager.trackedImagesChanged -= ImageChnaged;
	}

	private void ImageChnaged(ARTrackedImagesChangedEventArgs eventArgs)
	{
		foreach(ARTrackedImage trackedImage in eventArgs.added)
		{
			UpdateImage(trackedImage);
		}
		foreach(ARTrackedImage trackedImage in eventArgs.updated)
		{
			if (trackedImage.trackingState == TrackingState.Tracking)
                //if (trackedImage.trackingState != TrackingState.None)
            {
                UpdateImage(trackedImage);
            }

            else
            {
            	Destroy(spawnedPrefabs[trackedImage.name]);
            	spawnedPrefabs[trackedImage.name].SetActive(false);
            }

			
		}
		foreach(ARTrackedImage trackedImage in eventArgs.removed)
		{
			spawnedPrefabs[trackedImage.name].SetActive(false);
		}
	}

	private void UpdateImage(ARTrackedImage trackedImage)
	{
		string name = trackedImage.referenceImage.name;
		Vector3 position = trackedImage.transform.position;
		Quaternion rotation = trackedImage.transform.rotation;

		GameObject prefab = spawnedPrefabs[name];
		prefab.transform.position = position;
		prefab.transform.rotation = rotation;
		prefab.SetActive(true);

		foreach(GameObject go in spawnedPrefabs.Values)
		{
			if(go.name != name)
			{
				go.SetActive(false);
			}
		}
	}


}
