﻿using System.Collections.Generic;
using Augumenta;
using UnityEngine;
/**
 * FingerCursorHandler clones, then moves and rotates the cloned game object
 * so that it follows the selected hand pose. Used mostly for fingers cursors.
 */
public class FingerCursorHandler : PoseTransformer
{
	// GameObject to use as a child object
	[Tooltip("GameObject used for showing detected fingers")]
	public GameObject FingerCursor = null;
	// map of current cloned objects
	private Dictionary<uint, GameObject> clonesMap = new Dictionary<uint, GameObject> ();

	public override void OnPose(PoseEvent e, bool isNew, Vector3 p, Quaternion q)
	{
		if(FingerCursor == null) {
			return;
		}
		if(clonesMap.ContainsKey(e.id)) {
			// child object with event id already exists
			// update only the child game object
			base.TransformPose(clonesMap[e.id], e, p, q);
		} else {
			// new child event detected, create a new
			// child game object and start tracking it
			GameObject cloneobj = Instantiate(FingerCursor);
			cloneobj.SetActive(true);  // make the cloned object active
			base.TransformPose(cloneobj, e, p, q);
			clonesMap.Add(e.id, cloneobj);
		}
	}
	public override void OnPoseLost(PoseEvent e)
	{
		if(FingerCursor == null) {
			return;
		}
		if(clonesMap.ContainsKey(e.id)) {
			Destroy(clonesMap[e.id]);
			clonesMap.Remove(e.id);
		}
	}
}