using DocumentFormat.OpenXml.Bibliography;
using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.SearchModels;
using JewelryStoreContracts.StoragesContracts;
using JewelryStoreContracts.ViewModels;
using JewelryStoreFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JewelryStoreFileImplement.Implements
{
    public class OrderStorage : IOrderStorage
    {
        private readonly DataFileSingleton _source;
        public OrderStorage()
        {
            _source = DataFileSingleton.GetInstance();
        }
        public List<OrderViewModel> GetFullList()
        {
            return _source.Orders.Select(x => SAddJewelName(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.ImplementerId.HasValue && model.Statuses != null)
            {
                return _source.Orders .FirstOrDefault(x => x.ImplementerId == model.ImplementerId &&model.Statuses.Contains(x.Status))?.GetViewModel;
            }
            if (model.ImplementerId.HasValue)
            {
                return _source.Orders.FirstOrDefault(x => x.ImplementerId == model.ImplementerId)?.GetViewModel;
            }

            if (!model.Id.HasValue)
            {
                return null;
            }
            return SAddJewelName(_source.Orders.FirstOrDefault(x => (model.Id.HasValue && 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 SAddJewelName(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 SAddJewelName(order.GetViewModel);
        }
        public OrderViewModel? Delete(OrderBindingModel model)
        {
            var element = _source.Orders.FirstOrDefault(x => x.Id == model.Id);
            if (element != null)
            {
                _source.Orders.Remove(element);
                _source.SaveOrders();
                return SAddJewelName(element.GetViewModel);
            }
            return null;
        }

        public OrderViewModel SAddJewelName(OrderViewModel fullModel)
        {
            string? jewelName = _source.Jewels.FirstOrDefault(x => x.Id == fullModel?.JewelId)?.JewelName;
            if (jewelName != null) fullModel.JewelName = jewelName;
            return fullModel;
        }

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

            var jewel = _source.Jewels.FirstOrDefault(x => x.Id == order.JewelId);
            var client = _source.Clients.FirstOrDefault(x => x.Id == order.ClientId);
            if (jewel != null)
            {
                viewModel.JewelName = jewel.JewelName;
            }
            if (client != null)
            {
                viewModel.ClientFIO = client.ClientFIO;
            }
            return viewModel;
        }
    }
}
