using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace StrategicCombat.Maps
{
    /// <summary>
    /// This class represents a chess map.
    /// </summary>
    public class Map
    {

        #region Variables

        public int tileSize;
        public int numberOfxTiles;
        public int numberOfyTiles;

        public int xOffset = 377;
        public int yOffset = 135;

        public int[,] tiles;

        // Tile texture
        public Texture2D whiteTexture;
               
        #endregion

        #region Constructors

        /// <summary>
        /// Create a new map.
        /// </summary>
        /// <param name="content"></param>
        public Map(ContentManager content, int tileSize, int numberOfxTiles, int numberOfyTiles)
        {
            // Load textures
            this.whiteTexture = content.Load<Texture2D>("Textures/whiteTile");

            // Set up the map
            this.tileSize = tileSize;
            this.numberOfxTiles = numberOfxTiles;
            this.numberOfyTiles = numberOfyTiles;
            this.tiles = new int[numberOfxTiles, numberOfyTiles];
        }

        #endregion

        #region Methods

        /// <summary>
        /// Draw the map, must be called inside spritebatch.begin() and spritebatch.end()
        /// </summary>
        public void Draw(SpriteBatch spriteBatch)
        {
            
            // Boolean to draw different colors.
            bool blackColor = false;

            // Draw a black frame
            spriteBatch.Draw(this.whiteTexture, new Vector2(xOffset - 2, yOffset - 2), new Rectangle(0, 0, 525 + 4, 450 + 4), Color.Black, 0, Vector2.Zero, 1, SpriteEffects.None, 0);

            for (int row = 0; row < this.numberOfxTiles; row++)
            {
                // Change color for each row tile
                if (blackColor == false)
                    blackColor = true;
                else if (blackColor == true)
                    blackColor = false;

                for (int column = 0; column < this.numberOfyTiles; column++)
                {

                    // Draw rectangles.
                    if (blackColor == false)
                    {
                        spriteBatch.Draw(this.whiteTexture, new Rectangle(this.xOffset + row * this.tileSize,
                            this.yOffset + column * this.tileSize, this.tileSize, this.tileSize), Color.White);
                    }
                    else
                    {
                        spriteBatch.Draw(this.whiteTexture, new Rectangle(this.xOffset + row * this.tileSize,
                            this.yOffset + column * this.tileSize, this.tileSize, this.tileSize), Color.DarkGray);
                    }

                    // Change color for each column tile
                    if (blackColor == false)
                        blackColor = true;
                    else if (blackColor == true)
                        blackColor = false;
                }
            }

        } // End of the Draw method.

        /// <summary>
        /// Get a rectangle on the map based on a position.
        /// </summary>
        /// <param name="position">A x position and a y position as a Vector2.</param>
        /// <returns>A rectangle on the map</returns>
        public Rectangle GetMapRectangle(Vector2 position)
        {

            // Return the rectangle
            return new Rectangle(this.xOffset + (int)position.X * this.tileSize, this.yOffset + (int)position.Y * this.tileSize, this.tileSize, this.tileSize);

        } // End of the GetMapRectangle method.

        #endregion

    } // End of class

} // End of namespace
