using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using bbiwarg.Recognition.TouchRecognition;
using bbiwarg.Utility;
using TUIO;

namespace bbiwarg.Server
{
    class TuioCommunicator
    {
        private TuioServer server;
        private Dictionary<int, TuioCursor> cursors;

        public TuioCommunicator(string host, int port) {
            server = new TuioServer(host, port);
            cursors = new Dictionary<int, TuioCursor>();
        }

        public void touchDown(object sender, PalmTouchEventArgs ptea) {
            TuioCursor cursor = server.addTuioCursor(ptea.Position.X, ptea.Position.Y);
            cursors.Add(ptea.TrackID, cursor);
        }

        public void touchMove(object sender, PalmTouchEventArgs ptea) {
            server.updateTuioCursor(cursors[ptea.TrackID], ptea.Position.X, ptea.Position.Y);
        }

        public void touchUp(object sender, PalmTouchEventArgs ptea) {
            server.removeTuioCursor(cursors[ptea.TrackID]);
            cursors.Remove(ptea.TrackID);
        }

        public void close() {
            server.close();
        }

        public void initFrame() {
            server.initFrame();
        }

        public void commitFrame() {
            server.commitFrame();
        }

        public void reset() {
            foreach (int id in cursors.Keys) {
                server.removeTuioCursor(cursors[id]);
            }
            cursors.Clear();
        }
    }
}
