using DressAtelierDataModels.Models;
using DressAtelierContracts.ViewModels;
using DressAtelierContracts.BindingModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;
using System.Runtime.CompilerServices;
using System.Xml.Linq;

namespace DressAtelierFileImplement.Models
{
    public class Dress : IDressModel
    {
        public int ID { get; private set; }
        public string DressName { get; private set; } = string.Empty;

        public double Price { get; private set; }

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

        private Dictionary<int, (IMaterialModel, int)>? _dressComponents = null;
        public Dictionary<int, (IMaterialModel, int)> DressComponents
        {
            get
            {
                if(_dressComponents == null)
                {
                    var source = DataFileSingleton.GetInstance();
                    _dressComponents = Components.ToDictionary(x => x.Key, y => ((source.Components.FirstOrDefault(z => z.ID == y.Key) as IMaterialModel)!, y.Value));
                }
                return _dressComponents;
            }
        }

        public static Dress? Create(DressBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Dress()
            {
                ID = model.ID,
                DressName = model.DressName,
                Price = model.Price,
                Components = model.DressComponents.ToDictionary(x => x.Key, x
                => x.Value.Item2)

            };

        }

        public static Dress? Create(XElement element)
        {
            if (element == null)
            {
                return null;
            }
            return new Dress()
            {
                ID = Convert.ToInt32(element.Attribute("ID")!.Value),
                DressName = element.Element("DressName")!.Value,
                Price = Convert.ToDouble(element.Element("Price")!.Value),
                Components =
           element.Element("DressComponents")!.Elements("DressComponent")
            .ToDictionary(x =>
           Convert.ToInt32(x.Element("Key")?.Value), x =>
           Convert.ToInt32(x.Element("Value")?.Value))
            };
        }


        public void Update(DressBindingModel? model)
        {
            if (model == null)
            {
                return;
            }
            DressName = model.DressName;
            Price = model.Price;
            Components = model.DressComponents.ToDictionary(x => x.Key, x =>
            x.Value.Item2);
            _dressComponents = null;
        }
        public DressViewModel GetViewModel => new()
        {
            ID = ID,
            DressName = DressName,
            Price = Price,
            DressComponents = DressComponents
        };

        public XElement GetXElement => new("Dress",
            new XAttribute("ID", ID),
            new XElement("DressName", DressName),
            new XElement("Price", Price.ToString()),
            new XElement("DressComponents", Components.Select(x =>
            new XElement("DressComponent",new XElement("Key", x.Key),new XElement("Value", x.Value))).ToArray()));
    }
}
