using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;


namespace ActionGame.GameResources.Models
{
    public class WorldMapCell
    {
        public Int32 WorldMapX { get; set; }
        public Int32 WorldMapY { get; set; }
        public Int32 SongKey {get;set;}

        public IDictionary<Int32, IDictionary<Int32, TileSet>> TilesLayer1 { get; }
        public IDictionary<Int32, IDictionary<Int32, TileSet>> TilesLayer2 { get; }
        public IList<DoorDefinition> DoorDefinitions {get;}
        public IList<NonPlayerCharacterDefinition> NonPlayerCharacterDefinitions { get; }
        public IList<EnemyDefinitionPlacement> EnemyDefinitionPlacements { get; }

        private TimeSpan lastFrameChange;


        public WorldMapCell()
        {
            TilesLayer1 = new Dictionary<Int32, IDictionary<Int32, TileSet>>();
            TilesLayer2 = new Dictionary<Int32, IDictionary<Int32, TileSet>>();
            DoorDefinitions = new List<DoorDefinition>();
            NonPlayerCharacterDefinitions = new List<NonPlayerCharacterDefinition>();
            EnemyDefinitionPlacements = new List<EnemyDefinitionPlacement>();
        }


        public void AddTile(Int32 x, Int32 y, TileSet tile1, TileSet tile2) 
        {
            if (TilesLayer1.ContainsKey(x) == false) TilesLayer1.Add(x, new Dictionary<Int32, TileSet>());
            if (TilesLayer2.ContainsKey(x) == false) TilesLayer2.Add(x, new Dictionary<Int32, TileSet>());

            TilesLayer1[x].Add(y, tile1);
            TilesLayer2[x].Add(y, tile2);
        }


        public Int32 GetMaxTileX()
        {
            Int32 max = 0;

            max = TilesLayer1.Count;

            return max;
        }


        public Int32 GetMaxTileY()
        {
            Int32 max = 0;

            if(TilesLayer1.Count > 0)
            {
                max = TilesLayer1[0].Count;
            }

            return max;
        }


        public Int32 GetMaxX()
        {
            Int32 max = GetMaxTileX() * TileSet.DRAW_WIDTH;

            return max;
        }


        public Int32 GetMaxY()
        {
            Int32 max = GetMaxTileY() * TileSet.DRAW_HEIGHT;

            return max;
        }


        public void UpdateTiles(GameTime gameTime)
        {
            if (lastFrameChange == default(TimeSpan)) lastFrameChange = gameTime.TotalGameTime;

            if ((gameTime.TotalGameTime - lastFrameChange).TotalSeconds >= 0.5)
            {
                lastFrameChange = gameTime.TotalGameTime;

                IList<TileSet> tileSets = new List<TileSet>();
                Int32 maxX = GetMaxTileX();
                Int32 maxY = GetMaxTileY();

                //gather all unique tiles
                for (Int32 x = 0; x < maxX; x++)
                {
                    for (Int32 y = 0; y < maxY; y++)
                    {
                        if (TilesLayer1[x][y] != null && tileSets.Contains(TilesLayer1[x][y]) == false) tileSets.Add(TilesLayer1[x][y]);
                        if (TilesLayer2[x][y] != null && tileSets.Contains(TilesLayer2[x][y]) == false) tileSets.Add(TilesLayer2[x][y]);
                    }
                }

                //update
                foreach (TileSet tileSet in tileSets)
                {
                    tileSet.Update();
                }
            }
        }


    }
}
