﻿namespace CarDealer.Services.Implementations
{
    using System.Linq;
    using System.Collections.Generic;

    using Data;
    using Models;
    using Models.Customers;
    using Web.Infrastructure.Mapping;

    public class CustomerService : ICustomerService
    {
        private readonly CarDealerDbContext context;

        public CustomerService(CarDealerDbContext context)
        {
            this.context = context;
        }

        public IEnumerable<CustomerListingModel> All(OrderType order)
        {
            var customers = this.context.Customers
                .To<CustomerListingModel>();

            if (order == OrderType.Descending)
            {
                customers = customers
                    .OrderByDescending(c => c.BirthDate)
                    .ThenBy(c => c.IsYoungDriver);
            }
            else
            {
                customers = customers
                    .OrderBy(c => c.BirthDate)
                    .ThenBy(c => c.IsYoungDriver);
            }

            return customers.ToList();
        }
    }
}
