using BarContracts.BindingModels;
using BarContracts.ViewModels;
using BarDataModels.Enums;
using BarDataModels.Models;
using System.Xml.Linq;

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

        public int CocktailId { get; private set; }
        public int ClientId { 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 is null)
                return null;

            return new Order()
            {
                Id = Model.Id,
                CocktailId = Model.CocktailId,
                ClientId = Model.ClientId,
                Count = Model.Count,
                Sum = Model.Sum,
                Status = Model.Status,
                DateCreate = Model.DateCreate,
                DateImplement = Model.DateImplement
            };
        }

        public static Order? Create(XElement Element)
        {
            if (Element is null)
                return null;

            return new Order()
            {
                Id = Convert.ToInt32(Element.Attribute("Id")!.Value),
                CocktailId = Convert.ToInt32(Element.Element("CocktailId")!.Value),
                ClientId = Convert.ToInt32(Element.Element("ClientId")!.Value),
                Count = Convert.ToInt32(Element.Element("Count")!.Value),
                Sum = Convert.ToDouble(Element.Element("Sum")!.Value),
                Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), Element.Element("Status")!.Value),
                DateCreate = Convert.ToDateTime(Element.Element("DateCreate")!.Value),
                DateImplement = string.IsNullOrEmpty(Element.Element("DateImplement")!.Value) ? null : Convert.ToDateTime(Element.Element("DateImplement")!.Value)
            };
        }

        public void Update(OrderBindingModel? Model)
        {
            if (Model is null)
                return;

            Status = Model.Status;
            DateImplement = Model.DateImplement;
        }

        public OrderViewModel GetViewModel => new()
        {
            Id = Id,
            CocktailId = CocktailId,
            ClientId = ClientId,
            Count = Count,
            Sum = Sum,
            Status = Status,
            DateCreate = DateCreate,
            DateImplement = DateImplement
        };

        public XElement GetXElement => new(
            "Order",
            new XAttribute("Id", Id),
            new XElement("CocktailId", CocktailId),
             new XElement("ClientId", ClientId),
            new XElement("Count", Count.ToString()),
            new XElement("Sum", Sum.ToString()),
            new XElement("Status", Status.ToString()),
            new XElement("DateCreate", DateCreate.ToString()),
            new XElement("DateImplement", DateImplement.ToString())
        );
    }
}
