/*
Copyright 2019 Dr. Thomas Benjamin Senior, Michael Zoeller 

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

namespace EyeSkills
{
    [System.Serializable]
    public class Event
    {
        public Action callback;
        public string description;
        public string name;
        public List<string> keys; //Ben : Why are you serializing all the behaviours?
    };

    public class ControlManager : MonoBehaviour
    {
        public Dictionary<string, Event> behaviours = new Dictionary<string, Event>();

        // singleton like access pattern
        public static ControlManager instance = null;

        void Awake()
        {
            if (instance == null)
                instance = this;
            else if (instance != this)
                Destroy(gameObject);
            DontDestroyOnLoad(this);
        }

        public void RegisterBehaviour(string id, Action callback, string name = "", string description = "")
        {
            Event b = new Event();
            b.description = description;
            b.name = name;
            b.callback = callback;
            b.keys = new List<string>();
            behaviours.Add(id, b);

            string json = "\"{\"id\":\"" + id + "\", \"name\":\"" + name + "\", \"description\":\"" + description + "\"}\"";
            Debug.Log(json);

            //Ben: I assume this is just for testing. It might make sense if there's to be a web interface for establishing mappings, 
            //but that's something else which needs it's own Pub/Sub methods.
            //NetworkManager.instance.Send("register", json);
        }

        public void Register(string id, string key)
        {
            behaviours[id].keys.Add(key);
        }

        /// <summary>
        /// Registers input event types. In order to build a GUI mapping behaviours to inputs, we need to know which inputs there might be.
        /// Here, input devices can register themselves and the events they are able to produce. 
        /// </summary>
        /// <param name="id">Identifier.</param>
        public void RegisterInputEventType(string deviceID, string eventType)
        {
        }

        /// <summary>
        /// Distributes the input event. Any connected input devices should call this method with their updates.
        /// It will distribute that event to any behaviour call-backs connected to that source.
        /// </summary>
        /// <param name="">.</param>
        public void distributeInputEvent(string deviceID, string eventType, string eventData)
        {
        }

        /// <summary>
        /// Adds the mapping from input to behaviour.  
        /// </summary>
        /// <param name="inputEventTypeID">Input event type identifier.</param>
        /// <param name="behaviourID">Behaviour identifier.</param>
        public void addMappingFromBehaviourToInput(string behaviourID, string deviceID, string deviceEventType)
        {
        }

        public void delMappingFromInputToBehaviour(string behaviourID, string deviceID, string deviceEventType)
        {
        }
    }
}