using ComputerShopContracts.BindingModels;
using ComputerShopContracts.ViewModels;
using ComputerShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ComputerShopFileImplement.Models
{
    public class Shop : IShopModel
    {
        public int Id { get; private set; }

        public string Name { get; private set; } = string.Empty;

        public string Adress { get; private set; } = string.Empty;

        public int MaxCountComputers { get; private set; }

        public DateTime OpeningDate { get; private set; }

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

        private Dictionary<int, (IComputerModel, int)>? _shopComputers = null;

        public Dictionary<int, (IComputerModel, int)> ShopComputers
        {
            get
            {
                if (_shopComputers == null)
                {
                    var source = DataFileSingleton.GetInstance();
                    _shopComputers = Computers.ToDictionary(
                        x => x.Key,
                        y => ((source.Computers.FirstOrDefault(z => z.Id == y.Key) as IComputerModel)!, y.Value)
                    );
                }
                return _shopComputers;
            }
        }

        public static Shop? Create(ShopBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Shop()
            {
                Id = model.Id,
                Name = model.Name,
                Adress = model.Adress,
                MaxCountComputers = model.MaxCountComputers,
                OpeningDate = model.OpeningDate,
                Computers = model.ShopComputers.ToDictionary(x => x.Key, x => x.Value.Item2)
            };
        }

        public static Shop? Create(XElement element)
        {
            if (element == null)
            {
                return null;
            }
            return new Shop()
            {
                Id = Convert.ToInt32(element.Attribute("Id")!.Value),
                Name = element.Element("Name")!.Value,
                Adress = element.Element("Address")!.Value,
                MaxCountComputers = Convert.ToInt32(element.Element("MaxCountComputers")!.Value),
                OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value),
                Computers = element.Element("ShopComputers")!.Elements("ShopComputer").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;
            Adress = model.Adress;
            OpeningDate = model.OpeningDate;
            MaxCountComputers = model.MaxCountComputers;
            if (model.ShopComputers.Count > 0)
            {
                Computers = model.ShopComputers.ToDictionary(x => x.Key, x => x.Value.Item2);
                _shopComputers = null;
            }
        }
        public ShopViewModel GetViewModel => new()
        {
            Id = Id,
            Name = Name,
            Adress = Adress,
            OpeningDate = OpeningDate,
            ShopComputers = ShopComputers,
            MaxCountComputers = MaxCountComputers
        };

        public XElement GetXElement => new(
            "Shop",
             new XAttribute("Id", Id),
             new XElement("Name", Name),
             new XElement("Address", Adress),
             new XElement("MaxCountComputers", MaxCountComputers),
             new XElement("OpeningDate", OpeningDate.ToString()),
             new XElement("ShopComputers", Computers.Select(x =>
             new XElement("ShopComputer",
             new XElement("Key", x.Key),
             new XElement("Value", x.Value)))
            .ToArray()));
    }
}