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

namespace bbiwarg.Recognition.HandRecognition
{
    class TrackedHand : TrackedObject<Hand>
    {
        private Kalman2DPositionFilter centroidKalman;

        public Vector2D CentroidPrediction { get { return centroidKalman.getPrediction(); } }

        public TrackedHand(int id, Hand detectedHand, int numFramesDetectedUntilTracked, int numFramesLostUntilDeleted)
            : base(id, detectedHand, numFramesDetectedUntilTracked, numFramesLostUntilDeleted)
        {
            centroidKalman = new Kalman2DPositionFilter(Parameters.HandmXX, Parameters.HandmXY, Parameters.HandmYY);
            centroidKalman.setInitialPosition(detectedHand.Centroid);

            logStateChange();
        }

        public override void updateFrame(Hand detectedHand)
        {
            base.updateFrame(detectedHand);

            if (NumFramesInCurrentState == 1)
                logStateChange();

            if (detectedHand != null)
                centroidKalman.getCorrectedPosition(detectedHand.Centroid);
        }

        public override float calculateSimilarity(Hand detectedHand)
        {
            float centroidSimilarity = getPositionSimilarity(CentroidPrediction, detectedHand.Centroid, Parameters.HandTrackerMaxCentroidRelativeMove);

            return centroidSimilarity;
        }

        private void logStateChange()
        {
            String stateAsString = CurrentState.ToString().ToLower();
            Logger.log(String.Format("Hand #{0} {1}", this.ID, stateAsString), LogSubject.HandTracker);
        }
    }
}
