using AutomobilePlantContracts.BindingModels;
using AutomobilePlantContracts.ViewModels;
using AutomobilePlantDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace AutomobilePlantFileImplement.Models
{
    public class Car : ICarModel
    {
        public string CarName { get; private set; } = string.Empty;

        public double Price { get; private set; }

        public int Id { get; private set; }

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

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


        public static Car? Create(CarBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Car()
            {
                Id = model.Id,
                CarName = model.CarName,
                Price = model.Price,
                Components = model.CarComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
            };
        }

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

        public void Update(CarBindingModel? model)
        {
            if (model == null)
            {
                return;
            }
            CarName = model.CarName;
            Price = model.Price;
            Components = model.CarComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
            _carComponents = null;
        }

        public CarViewModel GetViewModel => new()
        {
            Id = Id,
            CarName = CarName,
            Price = Price,
            CarComponents = CarComponents
        };

        public XElement GetXElement => new(
            "Car",
             new XAttribute("Id", Id),
             new XElement("CarName", CarName),
             new XElement("Price", Price.ToString()),
             new XElement("CarComponents", Components.Select(x =>
             new XElement("CarComponent",
             new XElement("Key", x.Key),
             new XElement("Value", x.Value)))
            .ToArray()));
    }
}