using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace PrecastConcretePlantFileImplement.Models
{
    public class Shop : IShopModel
    {
        public string Name { get; private set; } = string.Empty;
        public string Address { get; private set; } = string.Empty;

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

        public Dictionary<int, (IReinforcedModel, int)> _shopReinforcedies = null;
        public Dictionary<int, (IReinforcedModel, int)> ShopReinforcedies
        {
            get
            {
                if (_shopReinforcedies == null)
                {
                    var source = DataFileSingleton.GetInstance();
                    _shopReinforcedies = Reinforcedies.ToDictionary(x => x.Key, y => ((source.Reinforceds.FirstOrDefault(z => z.Id == y.Key) as IReinforcedModel)!, y.Value));
                }
                return _shopReinforcedies;
            }
        }

        public int Id { get; private set; }

        public int ReinforcedMaxCount { get; private set; }

        public static Shop? Create(ShopBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Shop()
            {
                Id = model.Id,
                Name = model.Name,
                Address = model.Address,
                ReinforcedMaxCount = model.ReinforcedMaxCount,
                DateOpening = model.DateOpening,
                Reinforcedies = model.ShopReinforcedies.ToDictionary(x => x.Key, x => x.Value.Item2)
            };
        }
        public static Shop? Create(XElement element)
        {
            if (element == null)
            {
                return null;
            }
            return new()
            {
                Id = Convert.ToInt32(element.Attribute("Id")!.Value),
                Name = element.Element("Name")!.Value,
                Address = element.Element("Address")!.Value,
                DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value),
                ReinforcedMaxCount = Convert.ToInt32(element.Element("ReinforcedMaxCount")!.Value),
                Reinforcedies = element.Element("ShopReinforcedies")!.Elements("Reinforced").ToDictionary(
                    x => Convert.ToInt32(x.Element("Key")?.Value),
                    x => Convert.ToInt32(x.Element("Value")?.Value))
            };
        }
        public void Update(ShopBindingModel? model)
        {
            if (model == null)
            {
                return;
            }
            Name = model.Name;
            Address = model.Address;
            DateOpening = model.DateOpening;
            ReinforcedMaxCount = model.ReinforcedMaxCount;
            Reinforcedies = model.ShopReinforcedies.ToDictionary(x => x.Key, x => x.Value.Item2);
            _shopReinforcedies = null;
        }
        public ShopViewModel GetViewModel => new()
        {
            Id = Id,
            Name = Name,
            Address = Address,
            ShopReinforcedies = ShopReinforcedies,
            DateOpening = DateOpening,
            ReinforcedMaxCount = ReinforcedMaxCount,
        };
        public XElement GetXElement => new("Shop",
             new XAttribute("Id", Id),
             new XElement("Name", Name),
             new XElement("Address", Address),
             new XElement("DateOpening", DateOpening),
             new XElement("ReinforcedMaxCount", ReinforcedMaxCount),
             new XElement("ShopReinforcedies", Reinforcedies
                 .Select(x => new XElement("Reinforced",
                     new XElement("Key", x.Key),
                     new XElement("Value", x.Value))
                 ).ToArray()));
    }
}
