using EFlib;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using Model;

namespace EFManager
{
    public partial class ManagerData
    { 
        public class ManagerChampion : IChampionsManager
        {
            private readonly ManagerData parent;
            public ManagerChampion(ManagerData parent)
                => this.parent = parent;

            public async Task<Champion?> AddItem(Champion? item)
            {
                if (item == null)
                    return null;

                await parent.DbContext.Champions.AddAsync(item.toEF());
                await parent.DbContext.SaveChangesAsync();
                return item;
            }

            public async Task<bool> DeleteItem(Champion? item)
            {
                if (item == null)
                    return false;

                parent.DbContext.Champions.Remove(item.toEF());
                await parent.DbContext.SaveChangesAsync();
                return true;
            }

            public async Task<IEnumerable<Champion?>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false)
            {
                if (orderingPropertyName != null)
                {
                    if (descending)
                    {
                        return await Task.FromResult(parent.DbContext.Champions.OrderByDescending(
                            c => typeof(EFChampion).GetProperty(orderingPropertyName))
                            .Skip(index * count)
                            .Take(count)
                            .Select(ce => ce.toModel())
                            );
                    }
                    else
                    {
                        return await Task.FromResult(parent.DbContext.Champions.OrderBy(
                            c => typeof(EFChampion).GetProperty(orderingPropertyName))
                            .Skip(index * count)
                            .Take(count)
                            .Select(ce => ce.toModel())
                            );
                    }
                }
                else
                {
                    return await Task.FromResult(parent.DbContext.Champions
                        .Skip(index * count)
                        .Take(count)
                        .Select(ce => ce.toModel())
                        );
                }
            }

            public Task<IEnumerable<Champion?>> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, bool descending = false)
            {
                throw new NotImplementedException();

            }

            public Task<IEnumerable<Champion?>> GetItemsByClass(ChampionClass championClass, int index, int count, string? orderingPropertyName = null, bool descending = false)
            {
                throw new NotImplementedException();

            }

            public Task<IEnumerable<Champion?>> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false)
            {
                throw new NotImplementedException();

            }

            public Task<IEnumerable<Champion?>> GetItemsByRunePage(RunePage? runePage, int index, int count, string? orderingPropertyName = null, bool descending = false)
            {
                throw new NotImplementedException();

            }

            public Task<IEnumerable<Champion?>> GetItemsBySkill(Skill? skill, int index, int count, string? orderingPropertyName = null, bool descending = false)
            {
                throw new NotImplementedException();
            }

            public Task<IEnumerable<Champion?>> GetItemsBySkill(string skill, int index, int count, string? orderingPropertyName = null, bool descending = false)
            {
                throw new NotImplementedException();
            }

            public async Task<int> GetNbItems()
            {
                return parent.DbContext.Champions.Count();
            }

            public async Task<int> GetNbItemsByCharacteristic(string charName)
            {
                throw new NotImplementedException();
            }

            public Task<int> GetNbItemsByClass(ChampionClass championClass)
            {
                throw new NotImplementedException();
            }

            public Task<int> GetNbItemsByName(string substring)
            {
                throw new NotImplementedException();
            }

            public Task<int> GetNbItemsByRunePage(RunePage? runePage)
            {
                throw new NotImplementedException();
            }

            public Task<int> GetNbItemsBySkill(Skill? skill)
            {
                throw new NotImplementedException();
            }

            public async Task<int> GetNbItemsBySkill(string skill)
            {
                return parent.DbContext.Champions.Where(champ => skill != null && champ.Skills.Any(Skill => Skill.Name.Equals(skill)))
                    .Count();
            }

            public async Task<Champion?> UpdateItem(Champion? oldItem, Champion? newItem)
            {
                var toUpdate = parent.DbContext.Champions.Find(oldItem.Name);
             
                toUpdate = newItem.toEF();
                parent.DbContext.SaveChanges();

                return newItem;
            }
        }
    }
}