using PlumbingRepairContracts.BindingModels;
using PlumbingRepairContracts.ViewModels;
using PlumbingRepairDataModels.Models;
using System.Xml.Linq;

namespace PlumbingRepairFileImplement.Models
{
    public class Work : IWorkModel
    {
        public int Id { get; private set; }
        public string WorkName { get; private set; } = string.Empty;
        public double Price { get; private set; }

        public Dictionary<int, int> Components { get; private set; } = new();

        public Dictionary<int, (IComponentModel, int)> _workComponents = null;

        public Dictionary<int, (IComponentModel, int)> WorkComponents
        {
            get
            {
                if (_workComponents == null)
                {
                    var source = DataFileSingleton.GetInstance();
                    _workComponents = Components.ToDictionary(x => x.Key, y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value));
                }
                return _workComponents;
            }
        }

        public static Work? Create(WorkBindingModel model)
        {
            if (model == null)
            {
                return null;
            }
            return new Work()
            {
                Id = model.Id,
                WorkName = model.WorkName,
                Price = model.Price,
                Components = model.WorkComponents.ToDictionary(x => x.Key, x => x.Value.Item2)

            };
        }

        public static Work? Create(XElement element)
        {
            if (element == null)
            {
                return null;
            }
            return new Work()
            {
                Id = Convert.ToInt32(element.Attribute("Id")!.Value),
                WorkName = element.Element("WorkName")!.Value,
                Price = Convert.ToDouble(element.Element("Price")!.Value),
                Components = element.Element("WorkComponents")!.Elements("WorkComponent").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
            };
        }

        public void Update(WorkBindingModel model)
        {
            if (model == null)
            {
                return;
            }
            WorkName = model.WorkName;
            Price = model.Price;
            Components = model.WorkComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
            _workComponents = null;
        }

        public WorkViewModel GetViewModel => new()
        {
            Id = Id,
            WorkName = WorkName,
            Price = Price,
            WorkComponents = WorkComponents
        };

        public XElement GetXElement => new("Work",
            new XAttribute("Id", Id),
            new XElement("WorkName", WorkName),
            new XElement("Price", Price.ToString()),
            new XElement("WorkComponents", Components.Select(x =>
                new XElement("WorkComponent",
                new XElement("Key", x.Key),
                new XElement("Value", x.Value))).ToArray()));
    }
}
