﻿using System.Collections.Generic;
using System.Linq;

namespace AlaCarte.Domain.Model
{
    public class AssignmentPresentation
    {
        public AssignmentPresentation(Assignment assignment, IEnumerable<PresentationStep> steps)
        {
            Assignment = assignment;
            Steps = steps;
        }

        public Assignment Assignment { get; set; }
        public IEnumerable<PresentationStep> Steps { get; private set; }

        public PresentationStep PreviousStep
        {
            get
            {
                if (Assignment.CurrentStatus == null)
                {
                    return null;
                }
                else
                {
                    List<PresentationStep> presentation = Steps.OrderByDescending(ps => ps.Order).Where(ps => ps.Order <= Assignment.CurrentStatus.Sequence).Take(2).ToList();
                    return presentation.Count == 2 ? presentation[1] : null;
                }
            }
        }
        public PresentationStep CurrentStep
        {
            get
            {
                if (Assignment.CurrentStatus == null)
                {
                    return null;
                }
                else
                {
                    return Steps.Where(ps => ps.Order == Assignment.CurrentStatus.Sequence).Single();
                }
            }
        }
        public PresentationStep NextStep
        {
            get
            {
                if (Assignment.CurrentStatus == null)
                {
                    return null;
                }
                else
                {
                    List<PresentationStep> presentation = Steps.Where(ps => ps.Order > Assignment.CurrentStatus.Sequence).OrderBy(ps => ps.Order).Take(1).ToList();
                    return presentation.Count == 1 ? presentation[0] : null;
                }
            }
        }
    }
}
