using DressAtelierContracts.BindingModels;
using DressAtelierContracts.SearchModels;
using DressAtelierContracts.StorageContracts;
using DressAtelierContracts.ViewModels;
using DressAtelierFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace DressAtelierFileImplement.Implements
{
    public class OrderStorage : IOrderStorage
    {
        private readonly DataFileSingleton _source;

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

        public List<OrderViewModel> GetFullList()
        {
            return _source.Orders.Select(x => ReceiveDressName(x)).ToList();     
        }
        public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
        {
            if (!model.ID.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ClientID.HasValue && !model.Status.HasValue)
            {
                return new();
            }
            if (model.ClientID.HasValue)
            {
                return _source.Orders.Where(x => x.ClientID == model.ClientID).Select(x => ReceiveDressName(x)).ToList();
            }
            if (model.ID.HasValue)
            {
                return _source.Orders.Where(x => x.ID == model.ID).Select(x => ReceiveDressName(x)).ToList();
            }

            if (model.DateFrom.HasValue && model.DateTo.HasValue)
            {
                return _source.Orders.Where(x => model.DateFrom >= x.DateCreate && model.DateTo <= x.DateCreate).Select(x => ReceiveDressName(x)).ToList();
            }
            return _source.Orders.Where(x => x.Status.Equals(model.Status)).ToList().Select(x => ReceiveDressName(x)).ToList();
        }

        public OrderViewModel? GetElement(OrderSearchModel model)
        {
            if (!model.ID.HasValue)
            {
                return null;
            }
            return ReceiveDressName(_source.Orders.FirstOrDefault(x => x.ID == model.ID));
        }

        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 ReceiveDressName(newOrder);
        }

        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 ReceiveDressName(order);   
        }

        public OrderViewModel? Delete(OrderBindingModel model)
        {
            var order = _source.Orders.FirstOrDefault(x => x.ID == model.ID);
            if(order == null)
            {
                return null;
            }
            _source.Orders.Remove(order);
            _source.SaveOrders();
            return ReceiveDressName(order);
            
        }

        private OrderViewModel? ReceiveDressName(Order model)
        {
            if(model == null)
            {
                return null;
            }
            var order = model.GetViewModel;
            var dress = _source.Dresses.FirstOrDefault(un => un.ID == order.ID);
            order.DressName = dress == null ? "" : dress.DressName;
            return order;


        }
    }
}
