using PizzeriaContracts.BindingModels;
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StorageContracts;
using PizzeriaContracts.ViewModels;
using PizzeriaFileImplement.Models;

namespace PizzeriaFileImplement.Implements
{
    public class OrderStorage : IOrderStorage
    {
        private readonly DataFileSingleton source;
        public OrderStorage()
        {
            source = DataFileSingleton.GetInstance();
        }
        public List<OrderViewModel> GetFullList() => source.Orders.Select(x => AttachOthersNames(x.GetViewModel)).ToList();
        public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
        {
            if ((!model.DateFrom.HasValue || !model.DateTo.HasValue) && !model.ClientId.HasValue && !model.Status.HasValue)
            {
                return new();
            }
            return source.Orders.Where(x => (model.DateFrom.HasValue && model.DateTo.HasValue && x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) ||
                    (model.ClientId.HasValue && x.ClientId == model.ClientId) ||
                    (model.Status.HasValue && x.Status == model.Status)).Select(x => AttachOthersNames(x.GetViewModel)).ToList();
        }
        public OrderViewModel? GetElement(OrderSearchModel model)
        {
            if (!model.Id.HasValue && (!model.ImplementerId.HasValue || !model.Status.HasValue))
            {
                return new();
            }
            return AttachOthersNames(source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) ||
                    (model.ClientId.HasValue && x.Id == model.ClientId) ||
                    (model.ImplementerId.HasValue && x.Id == model.ImplementerId && x.Status == model.Status))?
                .GetViewModel);
        }
        public OrderViewModel? Insert(OrderBindingModel model)
        {
            model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1;
            var newOrder = Order.Create(model);
            if (newOrder == null)
            {
                return null;
            }
            source.Orders.Add(newOrder);
            source.SaveOrders();
            return AttachOthersNames(newOrder.GetViewModel);
        }
        public OrderViewModel? Update(OrderBindingModel model)
        {
            var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
            if (order == null)
            {
                return null;
            }
            order.Update(model);
            source.SaveOrders();
            return AttachOthersNames(order.GetViewModel);
        }
        public OrderViewModel? Delete(OrderBindingModel model)
        {
            var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
            if (order != null)
            {
                source.Orders.Remove(order);
                source.SaveOrders();
                return AttachOthersNames(order.GetViewModel);
            }
            return null;
        }
        private OrderViewModel AttachOthersNames(OrderViewModel model)
        {
            if (model == null)
            {
                return null;
            }
            var implementerFIO = source.Implementers.FirstOrDefault(x => model.ImplementerId == x.Id)?.ImplementerFIO;
            var clientFIO = source.Clients.FirstOrDefault(x => model.ClientId == x.Id)?.ClientFIO;
            var pizza = source.Pizzas.FirstOrDefault(x => x.Id == model.PizzaId);
            if (clientFIO != null)
            {
                model.ClientFIO = clientFIO;
            }
            if (implementerFIO != null)
            {
                model.ImplementerFIO = implementerFIO;
            }
            if (pizza != null)
            {
                model.PizzaName = pizza.PizzaName;
            }
            return model;
        }
    }

}
