using PizzeriaContracts.BindingModels;
using PizzeriaContracts.ViewModels;
using PizzeriaDataModels;
using System.Xml.Linq;

namespace PizzeriaFileImplement.models
{
    public class Order : IOrderModel
    {
        public int PizzaId { get; private set; }

        public string PizzaName
        {
            get
            {
                var source = DataFileSingleton.GetInstance();
                return source.Pizzas.FirstOrDefault(x => x.Id == PizzaId).PizzaName;
            }
        }

        public int Count { get; private set; }

        public double Sum { get; private set; }

        public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;

        public DateTime DateCreate { get; private set; } = DateTime.Now;

        public DateTime? DateImplement { get; private set; }

        public int Id { get; private set; }

        public static Order? Create(OrderBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Order()
            {
                Id = model.Id,
                PizzaId = model.PizzaId,
                Count = model.Count,
                Sum = model.Sum,
                Status = model.Status,
                DateCreate = model.DateCreate,
                DateImplement = model.DateImplement
            };
        }

        public static Order? Create(XElement element)
        {
            if (element == null)
            {
                return null;
            }

            return new Order()
            {
                Id = Convert.ToInt32(element.Attribute("Id")!.Value),
                PizzaId = Convert.ToInt32(element.Element("PizzaId")!.Value),
                Count = Convert.ToInt32(element.Element("Count")!.Value),
                Sum = Convert.ToInt32(element.Element("Sum")!.Value),
                Status = Enum.Parse<OrderStatus>(element.Element("Status")!.Value),
                DateCreate = DateTime.Parse(element.Element("DateCreate")!.Value),
                DateImplement = !string.IsNullOrEmpty(element.Element("DateImplement")!.Value.ToString()) ?
                    DateTime.Parse(element.Element("DateImplement")!.Value) : null,
            };
        }

        public void Update(OrderBindingModel? model)
        {
            if (model == null)
            {
                return;
            }
            PizzaId = model.PizzaId;
            Count = model.Count;
            Sum = model.Sum;
            Status = model.Status;
            DateCreate = model.DateCreate;
            DateImplement = model.DateImplement;
        }

        public OrderViewModel GetViewModel => new()
        {
            Id = Id,
            PizzaId = PizzaId,
            PizzaName = PizzaName,
            Count = Count,
            Sum = Sum,
            Status = Status,
            DateCreate = DateCreate,
            DateImplement = DateImplement
        };
        public XElement GetXElement => new("Order",
            new XAttribute("Id", Id),
            new XElement("PizzaId", PizzaId),
            new XElement("Count", Count),
            new XElement("Sum", Sum),
            new XElement("Status", Status),
            new XElement("DateCreate", DateCreate),
            new XElement("DateImplement", DateImplement));
    }
}
