using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using bbiwarg.Recognition.FingerRecognition;
using bbiwarg.Recognition.Tracking;
using bbiwarg.Utility;
using Emgu.CV;
using Emgu.CV.Structure;

namespace bbiwarg.Recognition.TouchRecognition
{
    class TouchEvent : TrackableObject
    {
        public Vector2D Position { get; private set; }
        public Image<Gray, byte> TouchMask { get; private set; }
        public Finger Finger { get; private set; }

        public TouchEvent(Vector2D position, Image<Gray, byte> touchMask, Finger finger)
        {
            Position = position;
            TouchMask = touchMask;
            Finger = finger;
        }

        public float getTouchValue()
        {
            int touchPixels = TouchMask.CountNonzero()[0];
            int numPixels = TouchMask.Width * TouchMask.Height;
            float touchValue = touchPixels / (float)numPixels;
            return touchValue;
        }

        public override float getSimilarity(TrackableObject compareObject)
        {
            TouchEvent compareTouchEvent = (TouchEvent)compareObject;

            //finger similarity
            float fingerSimilarity = (Finger.TrackID == compareTouchEvent.Finger.TrackID) ? 1 : 0;

            //position similarity
            float distance = Position.getDistanceTo(compareTouchEvent.Position);
            float distanceSimilarity = Math.Max(0, 1 - distance / Constants.ImageDiagonalLength);

            float similarity = fingerSimilarity * distanceSimilarity;

            return similarity;
        }
    }
}
