using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SushiBarFileImplement.Implements
{
    public class OrderStorage : IOrderStorage
    {
        private readonly DataFileSingleton source;

        public OrderStorage()
        {
            source = DataFileSingleton.GetInstance();
        }

        public List<OrderViewModel> GetFullList() => source.Orders.Select(x => AttachSushiName(x.GetViewModel)).ToList();

        public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
        {
            if (model.DateFrom.HasValue)
            {
                return source.Orders.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
                    .Select(x => GetViewModel(x)).ToList();
            }
            if (model.ClientId.HasValue && !model.Id.HasValue)
            {
                return source.Orders.Where(x => x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList();
            }

            if (model.Id.HasValue)
            {
                return source.Orders.Where(x => x.Id.Equals(model.Id)).Select(x => GetViewModel(x)).ToList();
            }
            return new();
        }

        public OrderViewModel? GetElement(OrderSearchModel model)
        {
            if (!model.Id.HasValue)
            {
                return new();
            }
            return AttachSushiName(source.Orders.FirstOrDefault(x => x.Id == model.Id)?.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 AttachSushiName(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 AttachSushiName(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 AttachSushiName(order.GetViewModel);
            }
            return null;
        }

        private OrderViewModel? AttachSushiName(OrderViewModel? model)
        {
            if (model == null)
            {
                return null;
            }
            var sushi = source.Sushis.FirstOrDefault(x => x.Id == model.SushiId);
            if (sushi != null)
            {
                model.SushiName = sushi.SushiName;
            }
            return model;
        }

        private OrderViewModel GetViewModel(Order order)
        {
            var viewModel = order.GetViewModel;

            var sushi = source.Sushis.FirstOrDefault(x => x.Id == order.SushiId);
            var client = source.Clients.FirstOrDefault(x => x.Id == order.ClientId);

            if (sushi != null)
            {
                viewModel.SushiName = sushi.SushiName;
            }
            if (client != null)
            {
                viewModel.ClientFIO = client.ClientFIO;
            }
            return viewModel;
        }
    }
}
