using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using bbiwarg.Images;
using bbiwarg.Output;
using bbiwarg.Recognition.Tracking;
using bbiwarg.Utility;
using bbiwarg.Input.InputHandling;

namespace bbiwarg.Recognition.TouchRecognition
{
    class TouchTracker : Tracker<Touch, TrackedTouch>
    {
        private List<TouchEventArgs> touchEvents;
        
        public TouchTracker() {
            touchEvents = new List<TouchEventArgs>();
        }

        public void trackTouches(FrameData frameData) {
            trackObjects(frameData.DetectedTouches);
            frameData.TrackedTouches = getOptimizedTouches();
            frameData.TouchEvents = flushTouchEvents();
        }

        protected override TrackedTouch createTrackedObject(Touch detectedObject)
        {
            TrackedTouch tt = new TrackedTouch(idPool.getNextUnusedID(), detectedObject, Parameters.TouchTrackerNumFramesDetectedUntilTracked, Parameters.TouchTrackerNumFramesLostUntilDeleted);
            tt.TouchEvent += touchEvent;
            return tt;
        }

        private List<Touch> getOptimizedTouches()
        {
            List<Touch> optimizedTouchs = new List<Touch>();
            foreach (TrackedTouch tp in TrackedObjects)
            {
                if (tp.IsTouchActive)
                    optimizedTouchs.Add(tp.OptimizedTouch);
            }
            return optimizedTouchs;
        }

        private void touchEvent(TrackedTouch sender, TouchEventArgs e)
        {
            touchEvents.Add(e);
        }

        public List<TouchEventArgs> flushTouchEvents() {
            List<TouchEventArgs> flushedTouchEvents = touchEvents;
            touchEvents = new List<TouchEventArgs>();
            return flushedTouchEvents;
        }

    }
}
