using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;



namespace TribbleExtinction
{
    public class World
    {
        static int ForestTile = 0;
        static int GrassTile = 1;
        static int RockyTile = 2;
        static int WaterTile = 3;
        public Tile[,] map;
        //list of doodads

        public int gridHeight, gridWidth, tileSize;

        Texture2D[] tileTex;

        public World(Game1 cGame, int width, int height, int tileSize)
        {
            gridHeight = height/tileSize;
            gridWidth = width / tileSize;
            this.tileSize = tileSize;
            tileTex = new Texture2D[4];
            map = new Tile[gridWidth, gridHeight];

            tileTex[0] = Program.masterGame.Content.Load<Texture2D>("Forest");
            tileTex[1] = Program.masterGame.Content.Load<Texture2D>("grass");
            tileTex[2] = Program.masterGame.Content.Load<Texture2D>("rocky");
            tileTex[3] = Program.masterGame.Content.Load<Texture2D>("water");

            for (int i = 0; i < gridWidth; i++) //horizontal map dimension
            {
                for (int j = 0; j < gridHeight; j++) //vertical map dimension
                {
                    map[i, j] = new Tile();
                }
            }

            //genMap(2);
        }

        private void genMap(int numIter)
        {
            Tile[,] initMap;
            initMap = new Tile[gridWidth, gridHeight];
            RandomGood rand = Program.game.rand;

            for (int i = 0; i < gridWidth; i++) //horizontal map dimension
            {
                for (int j = 0; j < gridHeight; j++) //vertical map dimension
                {
                    initMap[i, j] = new Tile();
                    initMap[i, j].TileType = rand.Next(0, 4);
                    map[i, j].TileType = initMap[i, j].TileType;
                }
            }

            for (int x = 0; x < numIter; x++ )
            {
                //Reconsider based on tendencies
                for (int i = 0; i < gridWidth; i++) //horizontal map dimension
                {
                    for (int j = 0; j < gridHeight; j++) //vertical map dimension
                    {
                        //Now iterate over map to check neighbours
                        for (int k = 0; k < gridWidth; k++) //horizontal map dimension
                        {
                            for (int l = 0; l < gridHeight; l++) //vertical map dimension
                            {
                                if (i != k || j != l)
                                    map[i, j].tendencies[initMap[k, l].TileType] += 1 / (Math.Abs(j - l) + Math.Abs(k - i));
                                else
                                    map[i, j].tendencies[initMap[k, l].TileType] += 2;
                            }
                        }

                        map[i, j].regenTile();
                    }
                }
                //Reset init to new 
                for (int i = 0; i < gridWidth; i++) //horizontal map dimension
                {
                    for (int j = 0; j < gridHeight; j++) //vertical map dimension
                    {
                        initMap[i, j].TileType = map[i, j].TileType;
                    }
                }
            }

        }

        public void render(SpriteBatch spriteBatch, GameTime cTime)
        {
            for (int t = 0; t < 4; t++)
            {
                Texture2D tex = tileTex[t];
                for (int i = 0; i < gridWidth; i++) //horizontal map dimension
                {
                    for (int j = 0; j < gridHeight; j++) //vertical map dimension
                    {
                        int test = map[i, j].TileType;
                        if (test == t)
                        {
                            Rectangle tileArea = new Rectangle((int)(i * tileSize), (int)(j * tileSize), tileSize, tileSize);

                            spriteBatch.Draw(tex, tileArea, Color.White);
                        }
                    }
                }
            }

            //run through doodads to render
        }

    }
}
