using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using Ventuz.OSC;
using OSC.NET;
//using System.Runtime.InteropServices;

using bbiwarg.Utility;

namespace TUIO
{
    class TuioServer
    {
        const int MAX_PACKET_SIZE = 65535 - 8;

        private OSCTransmitter transmitter = null;
        private String host = "127.0.0.1";
        private int port = 3333;
        private long sessionID = 0;
        private TuioTime currentFrameTime;

        private List<TuioCursor> cursorList;
        private List<TuioCursor> updatedCursorList;

        public TuioServer() { init(); }

        public TuioServer(int port)
        {
            this.port = port;
            init();
        }

        public TuioServer(String host, int port)
        {
            this.host = host;
            this.port = port;
            init();
        }

        private void init()
        {
            TuioTime.initSession();
            cursorList = new List<TuioCursor>();
            transmitter = new OSCTransmitter(host, port);
        }


        public void close()
        {
            transmitter.Close();
        }

        public TuioCursor addTuioCursor(float x, float y)
        {
            TuioCursor tcur = new TuioCursor(sessionID, cursorList.Count, x, y);
            cursorList.Add(tcur);
            updatedCursorList.Add(tcur);

            sessionID++;
            return tcur;
        }

        public void updateTuioCursor(TuioCursor tcur, float xp, float yp)
        {
            tcur.update(currentFrameTime, xp, yp);
            if (!updatedCursorList.Contains(tcur))
                updatedCursorList.Add(tcur);
        }

        public void updateTuioCursor(long s_id, float xp, float yp)
        {
            TuioCursor tcur;
            for (int i = 0; i < cursorList.Count; i++)
            {
                tcur = cursorList[i];
                if (tcur.getSessionID() == s_id)
                {
                    updateTuioCursor(tcur, xp, yp);
                    return;
                }
            }
        }

        public void removeTuioCursor(TuioCursor tcur)
        {
            cursorList.Remove(tcur);
        }


        public void initFrame()
        {
            currentFrameTime = TuioTime.getSessionTime();
            updatedCursorList = new List<TuioCursor>();
        }

        public void commitFrame()
        {
            sendMessage(updatedCursorList);
        }

        public void sendFullMessages()
        {
            sendMessage(cursorList);
        }

        private void sendMessage(List<TuioCursor> list)
        {
            OSCBundle packet = new OSCBundle();
            addAliveMessagesToBundle(packet);

            OSCMessage currentMessage;
            TuioCursor cursor;
            for (int i = 0; i < list.Count; i++)
            {
                cursor = cursorList[i];
                currentMessage = new OSCMessage("/tuio/2Dcur");
                currentMessage.Append("set");
                currentMessage.Append((Int32)cursor.getSessionID());
                currentMessage.Append(cursor.getX());
                currentMessage.Append(cursor.getY());
                currentMessage.Append(cursor.getXSpeed());
                currentMessage.Append(cursor.getYSpeed());
                currentMessage.Append(cursor.getMotionAccel());

                /*if (Marshal.SizeOf(packet) + Marshal.SizeOf(currentOscElement) >= MAX_PACKET_SIZE)
                {
                    packet.AddElement(new OscElement("/tuio/2Dcur", new Object[] { "fseq", -1 }));
                    udpwriter.Send(packet);

                    packet = new OscBundle();
                    addAliveMessagesToBundle(packet);
                }*/
                packet.Append(currentMessage);
            }
            currentMessage = new OSCMessage("/tuio/2Dcur");
            currentMessage.Append("fseq");
            currentMessage.Append(-1);
            packet.Append(currentMessage);
            transmitter.Send(packet);
        }

        private void addAliveMessagesToBundle(OSCBundle packet)
        {
            OSCMessage mssg = new OSCMessage("/tuio/2Dcur");
            mssg.Append("alive");
            for (int i = 0; i < cursorList.Count; i++)
            {
                mssg.Append((Int32)cursorList[i].getSessionID());
            }
            packet.Append(mssg);
        }

        public List<TuioCursor> getTuioCursor()
        {
            return cursorList;
        }

        public TuioCursor getTuioCursor(long s_id)
        {
            for (int i = 0; i < cursorList.Count; i++)
            {
                if (cursorList[i].getSessionID() == s_id)
                    return cursorList[i];
            }
            return null;
        }
    }
}
