using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDataModels.Enums;
using ConfectioneryDataModels.Models;
using System.Xml.Linq;

namespace ConfectioneryFileImplement.Models
{
    public class Order : IOrderModel
    {
        public int Id { get; private set; }

        public int PastryId { get; private set; }

        public int Count { get; private set; }

        public double Sum { get; private set; }

        public OrderStatus Status { get; private set; }

        public DateTime DateCreate { get; private set; }

        public DateTime? DateImplement { get; private set; }

        public static Order? Create(OrderBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Order()
            {
                PastryId = model.PastryId,
                Count = model.Count,
                Sum = model.Sum,
                Status = model.Status,
                DateCreate = model.DateCreate,
                DateImplement = model.DateImplement,
                Id = model.Id,
            };
        }
        public static Order? Create(XElement element)
        {
            if (element == null)
            {
                return null;
            }
            var dateImplement = element.Element("DateImplement")!.Value;
            return new()
            {
                Id            = Convert.ToInt32(element.Attribute("Id")!.Value),
                Sum           = Convert.ToDouble(element.Element("Sum")!.Value),
                Count         = Convert.ToInt32(element.Element("Count")!.Value),
                Status        = (OrderStatus)Convert.ToInt32(element.Element("Status")!.Value),
                PastryId      = Convert.ToInt32(element.Element("PastryId")!.Value),
                DateCreate    = Convert.ToDateTime(element.Element("DateCreate")!.Value),
                DateImplement = string.IsNullOrEmpty(dateImplement) ? null : Convert.ToDateTime(dateImplement),
            };
        }

        public void Update(OrderBindingModel? model)
        {
            if (model == null)
            {
                return;
            }
            PastryId = model.PastryId;
            Count = model.Count;
            Sum = model.Sum;
            Status = model.Status;
            DateCreate = model.DateCreate;
            DateImplement = model.DateImplement;
            Id = model.Id;
        }
        public OrderViewModel GetViewModel => new()
        {
            PastryName = DataFileSingleton.GetInstance().Pastries.FirstOrDefault(x => x.Id == PastryId)?.PastryName ?? string.Empty,
            PastryId = PastryId,
            Count = Count,
            Sum = Sum,
            Status = Status,
            DateCreate = DateCreate,
            DateImplement = DateImplement,
            Id = Id,
        };
        public XElement GetXElement => new("Order",
            new XAttribute("Id", Id),
            new XElement("PastryId", PastryId),
            new XElement("Count", Count),
            new XElement("Sum", Sum.ToString()),
            new XElement("Status", (int)Status),
            new XElement("DateCreate", DateCreate),
            new XElement("DateImplement", DateImplement)
        );
    }
}