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

namespace bbiwarg.Graphics
{
    class TouchEventVisualizer
    {
        public OutputImage OutputImage { get; private set; }

        private int width, height;

        private Dictionary<int, List<Vector2D>> positions;
        private Dictionary<int, Kalman2DPositionFilter> kalmanFilters;

        public TouchEventVisualizer(int width, int height)
        {
            this.width = width;
            this.height = height;

            this.positions = new Dictionary<int, List<Vector2D>>();
            this.kalmanFilters = new Dictionary<int, Kalman2DPositionFilter>();
        }

        public void touchDown(object sender, PalmTouchEventArgs ptea)
        {
            positions.Add(ptea.TrackID, new List<Vector2D>());
            positions[ptea.TrackID].Add(ptea.Position);

            Kalman2DPositionFilter kalmanFilter = new Kalman2DPositionFilter(Constants.TouchmXX, Constants.TouchmXY, Constants.TouchmYY, Constants.TouchProcessNoise);
            kalmanFilter.setInitialPosition(ptea.Position);
            kalmanFilters.Add(ptea.TrackID, kalmanFilter);
        }


        public void touchMove(object sender, PalmTouchEventArgs ptea) {
            positions[ptea.TrackID].Add(kalmanFilters[ptea.TrackID].getCorrectedPosition(ptea.Position));
        }

        public void touchUp(object sender, PalmTouchEventArgs ptea) {
            positions.Remove(ptea.TrackID);
            kalmanFilters.Remove(ptea.TrackID);
        }

        public void updateImage() {
            OutputImage = new OutputImage(width, height);

            //border
            OutputImage.drawRectangle(0, 0, width - 1, height - 1, Constants.TouchEventVisualizerGridColor);

            //draw grid
            int numRows = Constants.PalmGridNumRows;
            int numColumns = Constants.PalmGridNumColumns;
            int widthPerColumn = width / numColumns;
            int heightPerRow = height / numRows;

            for (int i = 0; i <= numColumns; i++)
            {
                OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(i * widthPerColumn, 0), new Vector2D(i * widthPerColumn, height - 1)), Constants.TouchEventVisualizerGridColor);
            }

            for (int i = 0; i <= numRows; i++)
            {
                OutputImage.drawLineSegment(new LineSegment2D(new Vector2D(0,i*heightPerRow), new Vector2D(width-1,i*heightPerRow)), Constants.TouchEventVisualizerGridColor);
            }

            Vector2D maxPixel = new Vector2D(width - 1, height - 1);
            foreach (int id in positions.Keys) {
                int numPositions = positions[id].Count;
                for (int i = 1; i < numPositions; i++) {
                    OutputImage.drawLineSegment(new LineSegment2D(positions[id][i - 1].scale(maxPixel), positions[id][i].scale(maxPixel)), Constants.TouchEventVisualizerLineColor);
                }
                Vector2D lastPos = positions[id][numPositions - 1].scale(maxPixel);
                OutputImage.fillCircle(lastPos.IntX, lastPos.IntY, 3, Constants.TouchEventVisualizerPointColor);
            }
        }

        public void reset()
        {
            OutputImage = new OutputImage(width, height);
            positions.Clear();
            kalmanFilters.Clear();
        }
    }
}
