using MainShell.Models.Wafer;
using System.Collections.Generic;
using System.Linq;

namespace MainShell.Process
{
    public class DieTransferRow
    {
        public DieTransferRow()
        {
            Dies = new List<Die>();
            Direction = DieTransferRowDirection.Positive;
        }

        public int RowIndex { get; set; }

        public DieTransferRowDirection Direction { get; set; }

        public bool SkipNgDie { get; set; }

        public List<Die> Dies { get; set; }

        public int AvailableCount
        {
            get
            {
                return GetAvailableDies().Count;
            }
        }

        public IReadOnlyList<Die> GetOrderedDies()
        {
            if (Dies == null)
            {
                return new List<Die>();
            }

            IEnumerable<Die> orderedDies = Direction == DieTransferRowDirection.Positive
                ? Dies.OrderBy(die => die.Column)
                : Dies.OrderByDescending(die => die.Column);
            return orderedDies.ToList();
        }

        public IReadOnlyList<Die> GetAvailableDies()
        {
            IEnumerable<Die> availableDies = GetOrderedDies().Where(die => die != null);
            if (SkipNgDie)
            {
                availableDies = availableDies.Where(die => die.Status != DieStatus.Ng);
            }

            return availableDies.ToList();
        }
    }
}
