using ActionGame.GameArtifacts.Models.Characters;
using ActionGame.GameResources.Models;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ActionGame.UserInterface.Control
{
    public class MiniMapViewer : IControl
    {
        public IList<IControl> Controls { get; }

        public SpriteFont Font { get; set; }

        public Boolean IsSelected { get; set; }
        public Boolean IsSelectable { get; }
        public Rectangle Location { get; set; }
        public WorldMap WorldMap { get; set; }
        public Player Player { get; set; }

        public Texture2D CellTexture { get; set; }
        public Texture2D ActiveCellTexture { get; set; }
        public Texture2D BackgroundTexture { get; set; }



        public MiniMapViewer()
        {
            this.Controls = new List<IControl>();
            this.IsSelectable = false;
            this.WorldMap = null;
        }



        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            spriteBatch.Draw(this.BackgroundTexture, this.Location, Color.White);

            Int32 w = (Location.Height-2) / (GetHighestSide()+1);
            
            if(WorldMap.ShowMiniMap)
            {
                foreach(KeyValuePair<Int32,IDictionary<Int32,WorldMapCell>> column in WorldMap.Cells)
                {
                    foreach(KeyValuePair<Int32,WorldMapCell> row in WorldMap.Cells[column.Key]) 
                    {
                        WorldMapCell cell = WorldMap.Cells[column.Key][row.Key];

                        Int32 x = this.Location.X + (cell.WorldMapX * w) + 1;
                        Int32 y = this.Location.Y + (cell.WorldMapY * w) + 1;

                        Texture2D texture = this.CellTexture;

                        if(this.Player.WorldPosition.WorldCellX == column.Key)
                        {
                            if(this.Player.WorldPosition.WorldCellY == row.Key)
                            {
                                texture = this.ActiveCellTexture;
                            }
                        }

                        Rectangle rect = new Rectangle(x, y, w, w);
                        spriteBatch.Draw(texture, rect, Color.White);

                    }
                }
            }
        }


        private Int32 GetHighestSide()
        {
            Int32 z = 0;

            foreach (KeyValuePair<Int32, IDictionary<Int32, WorldMapCell>> column in WorldMap.Cells)
            {
                if (column.Key > z) z = column.Key;

                foreach (KeyValuePair<Int32, WorldMapCell> row in WorldMap.Cells[column.Key])
                {
                    if (row.Key > z) z = row.Key;
                }
            }

            return z;
        }


        public void Update(GameTime gameTime)
        {

        }


        public void Press(GameTime gameTime)
        {

        }





    }
}
