#if UNITY_5_3_OR_NEWER
using System;
using System.Collections.Generic;
using UnityEngine;

namespace RoboidControl.Unity {

    public class SiteServer : MonoBehaviour {
        public RoboidControl.SiteServer site;

        public Queue<RoboidControl.Thing> thingQueue = new();

        protected virtual void Awake() {
            Console.SetOut(new UnityLogWriter());

            site = new RoboidControl.SiteServer(7681);
            RoboidControl.Thing.OnNewThing += HandleNewThing;
        }

        void OnApplicationQuit() {
            site.Close();
        }

        public void HandleNewThing(RoboidControl.Thing thing) {
            Debug.Log($"Handle New thing event for {thing}");
            site.Add(thing, false);
            thingQueue.Enqueue(thing);
        }

        protected virtual void Update() {
            site.Update((ulong)(Time.time * 1000));
            while (thingQueue.TryDequeue(out RoboidControl.Thing thing))
                thing.CreateComponent();
        }
    }

}
#endif