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

namespace Passer.Control.Unity {

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

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

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

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

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

        public void HandleNewThing(Core.Thing thing) {
            site.Add(thing, false);
            thingQueue.Enqueue(thing);
        }

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

}
#endif