using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using bbiwarg.Images;
using bbiwarg.Utility;

namespace bbiwarg.Detectors.Fingers
{
    class Finger
    {
        public Vector2D Tip { get { return SliceTrail.Start.Mid; } }
        public Vector2D Hand { get { return SliceTrail.End.Mid; } }
        public LineSegment2D LineSegment { get { return SliceTrail.LineSegment; } }
        public FingerSliceTrail SliceTrail { get; private set; }

        public Finger(FingerSliceTrail sliceTrail)
        {
            //check direction
            if (sliceTrail.Start.Length > sliceTrail.End.Length)
                sliceTrail.Slices.Reverse();

            SliceTrail = sliceTrail;
        }

        public Point[] getPolygon()
        {
            List<Point> pointsA = new List<Point>();
            List<Point> pointsB = new List<Point>();
            //int numSlices = SliceTrail.NumSlices;
            //for (int i = 0; i < numSlices; i++)
            foreach (FingerSlice slice in SliceTrail.Slices)
            {
                pointsA.Add(slice.Start);
                pointsB.Add(slice.End);
            }

            pointsB.Reverse();
            pointsA.AddRange(pointsB);
            return pointsA.ToArray();
        }

        //TODO: redo (not use similarity but actual distances and angle instead)
        public float getSimilarity(Finger compareFinger)
        {
            LineSegment2D compareLineSegment = compareFinger.LineSegment;
            Line2D compareLine = compareLineSegment.Line;

            //thresholds
            float thresholdMaxAngle = (float)(30 * Math.PI / 180); // 30°
            float thresholdMaxParallelDistance = 40;
            float thresholdMaxVerticalDistance = 40;

            //check angle
            float angle = LineSegment.Line.getAngleBetween(compareLine);
            float angleSimilarity = Math.Max(1 - angle / thresholdMaxAngle, 0);

            //check parallel distance
            float parallelDistance = LineSegment.getParallelDistanceTo(compareLineSegment);
            float parallelDistanceSimilarity = Math.Max(1 - parallelDistance / thresholdMaxParallelDistance, 0);

            //check vertical distance
            float verticalDistance = LineSegment.getVerticalDistanceTo(compareLineSegment);
            float verticalDistanceSimilarity = Math.Max(1 - verticalDistance / thresholdMaxVerticalDistance, 0);

            return (angleSimilarity + parallelDistanceSimilarity + verticalDistanceSimilarity) / 3;
        }
    }
}
