using SushiBarDataModels.Models;
using SushiBarContracts.BindingModels;
using SushiBarContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using SushiBarFileImplement;

namespace SushiBarFileImplement.Models
{
    public class Shop : IShopModel
    {
        public int Id { get; private set; }
        public string ShopName { get; private set; } = string.Empty;
        public string Adress { get; private set; } = string.Empty;
        public DateTime OpeningDate { get; private set; }
        public Dictionary<int, int> Sushis { get; private set; } = new();
        private Dictionary<int, (ISushiModel, int)>? _shopSushis = null;

        public Dictionary<int, (ISushiModel, int)> ShopSushis
        {
            get
            {
                if (_shopSushis == null)
                {
                    var source = DataFileSingleton.GetInstance();
                    _shopSushis = Sushis.ToDictionary(x => x.Key, y => ((source.Sushis.FirstOrDefault(z => z.Id == y.Key) as ISushiModel)!, y.Value));
                }
                return _shopSushis;
            }
        }

        public int SushiMaxCount { get; private set; }

        public static Shop? Create(ShopBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Shop()
            {
                Id = model.Id,
                ShopName = model.ShopName,
                Adress = model.Adress,
                OpeningDate = model.OpeningDate,
                Sushis = model.ShopSushis.ToDictionary(x => x.Key, x => x.Value.Item2),
                SushiMaxCount = model.SushiMaxCount
            };
        }

        public static Shop? Create(XElement element)
        {
            if (element == null)
            {
                return null;
            }
            return new()
            {
                Id = Convert.ToInt32(element.Attribute("Id")!.Value),
                ShopName = element.Element("ShopName")!.Value,
                Adress = element.Element("Adress")!.Value,
                OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value),
                Sushis = element.Element("ShopSushis")!.Elements("ShopSushi")!.ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value),
                x => Convert.ToInt32(x.Element("Value")?.Value)),
                SushiMaxCount = Convert.ToInt32(element.Element("SushiMaxCount")!.Value)
            };
        }

        public void Update(ShopBindingModel? model)
        {
            if (model == null)
            {
                return;
            }
            ShopName = model.ShopName;
            Adress = model.Adress;
            OpeningDate = model.OpeningDate;
            SushiMaxCount = model.SushiMaxCount;
            Sushis = model.ShopSushis.ToDictionary(x => x.Key, x => x.Value.Item2);
            _shopSushis = null;
        }

        public ShopViewModel GetViewModel => new()
        {
            Id = Id,
            ShopName = ShopName,
            Adress = Adress,
            OpeningDate = OpeningDate,
            ShopSushis = ShopSushis,
            SushiMaxCount = SushiMaxCount
        };

        public XElement GetXElement => new("Shop",
            new XAttribute("Id", Id),
            new XElement("ShopName", ShopName),
            new XElement("Adress", Adress),
            new XElement("OpeningDate", OpeningDate.ToString()),
            new XElement("ShopSushis", Sushis.Select(
                x => new XElement("ShopSushi", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()),
            new XElement("SushiMaxCount", SushiMaxCount.ToString())
            );

        public void SushisUpdate()
        {
            _shopSushis = null;
        }
    }
}