﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Domain.Objects;

namespace Domain.Dtos
{
    public class MovementDto : EntityBaseDto
    {
        public DateTime Date { get; set; }
        public decimal Quantity { get; set; }
        public ICollection<TagDto> Tags { get; set; }
    }

    public static class MovementDtoExtensions
    {
        public static IQueryable<MovementDto> Map(this IQueryable<Movement> movements)
        {
            return movements.Select(m => new MovementDto
            {
                Id = m.Id,
                CreationDate = m.CreationDate,
                ModificationDate = m.ModificationDate,
                Date = m.Date,
                Quantity = m.Quantity,
                Tags = m.TagInMovements.Select(t => t.Tag).AsQueryable().Select(TagDtoExtensions.SelectExpression).ToList()
            });
        }

        public static IOrderedQueryable<MovementDto> Map(this IOrderedQueryable<Movement> movements)
        {
            return (IOrderedQueryable<MovementDto>) ((IQueryable<Movement>) movements).Map();
        }

        public static MovementDto Map(this Movement movement)
        {
            return new MovementDto
            {
                Id = movement.Id,
                CreationDate = movement.CreationDate,
                ModificationDate = movement.ModificationDate,
                Date = movement.Date,
                Quantity = movement.Quantity,
                Tags = movement.TagInMovements.Select(t => t.Tag).AsQueryable().Select(TagDtoExtensions.SelectExpression).ToList()
            };
        }
    }
}
