using System.Collections.Generic;

namespace MainShell.Process
{
    public class WaferRecognitionResultCollector : IWaferRecognitionResultCollector
    {
        private readonly object _syncRoot = new object();
        private readonly List<double> _rowsReal = new List<double>();
        private readonly List<double> _columnsReal = new List<double>();
        private readonly List<double> _angles = new List<double>();
        private readonly List<double> _overlapRows = new List<double>();
        private readonly List<double> _overlapColumns = new List<double>();
        private readonly List<double> _overlapAngles = new List<double>();
        private int _totalFrameCount;
        private int _successFrameCount;
        private int _failedFrameCount;
        private int _lostFrameCount;

        public void Add(WaferRecognitionFrameResult frameResult)
        {
            lock (_syncRoot)
            {
                _totalFrameCount++;

                if (frameResult == null)
                {
                    _failedFrameCount++;
                    return;
                }

                if (!frameResult.IsSuccess)
                {
                    _failedFrameCount++;
                    return;
                }

                _successFrameCount++;
                _rowsReal.AddRange(frameResult.RowsReal);
                _columnsReal.AddRange(frameResult.ColumnsReal);
                _angles.AddRange(frameResult.Angles);
                _overlapRows.AddRange(frameResult.OverlapRows);
                _overlapColumns.AddRange(frameResult.OverlapColumns);
                _overlapAngles.AddRange(frameResult.OverlapAngles);
            }
        }

        public WaferRecognitionSnapshot CreateSnapshot()
        {
            lock (_syncRoot)
            {
                WaferRecognitionSnapshot snapshot = new WaferRecognitionSnapshot();
                snapshot.RowsReal = _rowsReal.ToArray();
                snapshot.ColumnsReal = _columnsReal.ToArray();
                snapshot.Angles = _angles.ToArray();
                snapshot.OverlapRows = _overlapRows.ToArray();
                snapshot.OverlapColumns = _overlapColumns.ToArray();
                snapshot.OverlapAngles = _overlapAngles.ToArray();
                snapshot.TotalFrameCount = _totalFrameCount;
                snapshot.SuccessFrameCount = _successFrameCount;
                snapshot.FailedFrameCount = _failedFrameCount;
                snapshot.LostFrameCount = _lostFrameCount;
                return snapshot;
            }
        }

        public void Reset()
        {
            lock (_syncRoot)
            {
                _rowsReal.Clear();
                _columnsReal.Clear();
                _angles.Clear();
                _overlapRows.Clear();
                _overlapColumns.Clear();
                _overlapAngles.Clear();
                _totalFrameCount = 0;
                _successFrameCount = 0;
                _failedFrameCount = 0;
                _lostFrameCount = 0;
            }
        }
    }
}