﻿// MIT License. Copyright (c) 2016 Maxim Kuzmin. Contacts: https://github.com/maxim-kuzmin

// Author: Maxim Kuzmin

using Makc2017.Data.Entity.Objects;
using Makc2017.Modules.DummyManyToMany.Filters;
using System.Linq;

namespace Makc2017.Modules.DummyManyToMany.Data.Entity.Extensions.OfQueryList
{
    /// <summary>
    /// Модули. Модуль "DummyManyToMany". Данные. Entity Framework. Расширения. Расширение запроса списка.
    /// </summary>
    public static class ModuleDummyManyToManyDataEntityExtensionOfQueryList
    {
        #region Public methods

        /// <summary>
        /// Применить фильтрацию.
        /// </summary>
        /// <param name="query">Запрос.</param>
        /// <param name="filter">Фильтр.</param>
        /// <returns>Запрос с учётом фильтрации.</returns>
        public static IQueryable<DataEntityObjectDummyManyToMany> ApplyFiltering(
            this IQueryable<DataEntityObjectDummyManyToMany> query,
            ModuleDummyManyToManyFilterList filter
            )
        {
            if (!string.IsNullOrWhiteSpace(filter.Name))
            {
                query = query.Where(x => x.Name.Contains(filter.Name));
            }

            if (filter.Ids != null && filter.Ids.Any())
            {
                query = query.Where(x => filter.Ids.Contains(x.Id));
            }

            return query;
        }

        /// <summary>
        /// Применить сортировку.
        /// </summary>
        /// <param name="query">Запрос.</param>
        /// <param name="filter">Фильтр.</param>
        /// <returns>Запрос с учётом сортировки.</returns>
        public static IQueryable<DataEntityObjectDummyManyToMany> ApplySorting(
            this IQueryable<DataEntityObjectDummyManyToMany> query,
            ModuleDummyManyToManyFilterList filter
            )
        {
            var sortField = filter.SortField.ToLower();
            var sortDirection = filter.SortDirection.ToLower();

            switch (sortField)
            {
                case "id":
                    switch (sortDirection)
                    {
                        case "asc":
                            query = query.OrderBy(x => x.Id);
                            break;
                        case "desc":
                            query = query.OrderByDescending(x => x.Id);
                            break;
                    }
                    break;
                case "name":
                    switch (sortDirection)
                    {
                        case "asc":
                            query = query.OrderBy(x => x.Name);
                            break;
                        case "desc":
                            query = query.OrderByDescending(x => x.Name);
                            break;
                    }
                    break;
            }

            if (!string.IsNullOrWhiteSpace(sortField) && sortField != "id")
            {
                query = ((IOrderedQueryable<DataEntityObjectDummyManyToMany>)query).ThenBy(x => x.Id);
            }

            return query;
        }

        #endregion Public methods
    }
}