using ShipyardContracts.BindingModels;
using ShipyardContracts.ViewModels;
using ShipyardDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ShipyardFileImplement.Models
{
    [DataContract]
    public class Ship : IShipModel
    {
        [DataMember]
        public int Id { get; private set; }

        [DataMember]
        public string ShipName { get; private set; } = string.Empty;

        [DataMember]
        public double Price { get; private set; }
        public Dictionary<int, int> Details { get; private set; } = new();
        private Dictionary<int, (IDetailModel, int)>? _shipDetails = null;

        [DataMember]
        public Dictionary<int, (IDetailModel, int)> ShipDetails
        {
            get
            {
                if (_shipDetails == null)
                {
                    var source = DataFileSingleton.GetInstance();
                    _shipDetails = Details.ToDictionary(x => x.Key, y => ((source.Details.FirstOrDefault(z => z.Id == y.Key) as IDetailModel)!, y.Value));
                }
                return _shipDetails;
            }
        }
        public static Ship? Create(ShipBindingModel model)
        {
            if (model == null)
            {
                return null;
            }
            return new Ship()
            {
                Id = model.Id,
                ShipName = model.ShipName,
                Price = model.Price,
                Details = model.ShipDetails.ToDictionary(x => x.Key, x => x.Value.Item2)
            };
        }
        public static Ship? Create(XElement element)
        {
            if (element == null)
            {
                return null;
            }
            return new Ship()
            {
                Id = Convert.ToInt32(element.Attribute("Id")!.Value),
                ShipName = element.Element("ShipName")!.Value,
                Price = Convert.ToDouble(element.Element("Price")!.Value),
                Details = element.Element("ShipDetails")!.Elements("ShipDetail").ToDictionary(x =>Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
            };
        }
        public void Update(ShipBindingModel model)
        {
            if (model == null)
            {
                return;
            }
            ShipName = model.ShipName;
            Price = model.Price;
            Details = model.ShipDetails.ToDictionary(x => x.Key, x =>
                x.Value.Item2);
            _shipDetails = null;
        }
        public ShipViewModel GetViewModel => new()
        {
            Id = Id,
            ShipName = ShipName,
            Price = Price,
            ShipDetails = ShipDetails
        };
        public XElement GetXElement => new("Ship",
        new XAttribute("Id", Id),
        new XElement("ShipName", ShipName),
        new XElement("Price", Price.ToString()),
        new XElement("ShipDetails", ShipDetails.Select(x => new XElement("ShipDetails",
            new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()));
    }
}

