using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;

namespace WTD_Editor
{
    public class TileMap
    {
        // attributes
        public int width;
        public int height;
        public int[][] tilemap;
        public int[][] monster_map;
        public int[][] tower_map;
        public int tileset;
        public bool use_custom_tileset;
        public string custom_tileset_path;
        public Byte[] custom_tileset_gif;

        // default constructor for XML serialization
        public TileMap()
        {
            this.width = 32;
            this.height = 32;
            this.tileset = 0;
            this.use_custom_tileset = false;
            custom_tileset_path = null;
            tilemap = new int[width][];
            monster_map = new int[width][];
            tower_map = new int[width][];
            for (int i = 0; i < width; i++)
            {
                tilemap[i] = new int[height];
                monster_map[i] = new int[height];
                tower_map[i] = new int[height];
            }
            fillWithTile(0);
        }

        // constructor
        public TileMap(int width, int height)
        {
            this.width = width;
            this.height = height;
            this.tileset = 0;
            tilemap = new int[width][];
            monster_map = new int[width][];
            tower_map = new int[width][];
            for (int i = 0; i < width; i++)
            {
                tilemap[i] = new int[height];
                monster_map[i] = new int[height];
                tower_map[i] = new int[height];
            }
            fillWithTile(0);
        }

        // copy constructor
        public TileMap(TileMap tmap)
        {
            width = tmap.width;
            height = tmap.height;
            tileset = tmap.tileset;
            tilemap = new int[width][];
            monster_map = new int[width][];
            tower_map = new int[width][];
            for (int i = 0; i < width; i++)
            {
                tilemap[i] = new int[height];
                monster_map[i] = new int[height];
                tower_map[i] = new int[height];
            }
            for (int j = 0; j < height; j++)
                for (int i = 0; i < width; i++)
                {
                    tilemap[i][j] = tmap.tilemap[i][j];
                    monster_map[i][j] = tmap.monster_map[i][j];
                    tower_map[i][j] = tmap.tower_map[i][j];
                }
        }


        // change the size of the tilemap
        public void changeSizeTo(int width, int height)
        {
            int[][] old_tilemap = tilemap;
            int[][] old_monster_map = monster_map;
            int[][] old_tower_map = tower_map;
            tilemap = new int[width][];
            monster_map = new int[width][];
            tower_map = new int[width][];
            for (int i = 0; i < width; i++)
            {
                tilemap[i] = new int[height];
                monster_map[i] = new int[height];
                tower_map[i] = new int[height];
            }
            int old_width = this.width;
            int old_height = this.height;
            this.width = width;
            this.height = height;

            for (int i = 0; i < height; i++)
                for (int j = 0; j < width; j++)
                {
                    if (j < old_width && i < old_height)
                    {
                        tilemap[j][i] = old_tilemap[j][i];
                        monster_map[j][i] = old_monster_map[j][i];
                        tower_map[j][i] = old_tower_map[j][i];
                    }
                    else
                    {
                        Map.TILESETS[tileset].putRandomBaseTile(this, new Point(j, i), 0);
                        monster_map[j][i] = 0;
                        tower_map[j][i] = 0;
                    }
                }
        }

        // fill the tilemap with a given base tile type
        public void fillWithTile(int tile)
        {
            // clear map
            for (int i = 0; i < height; i++)
                for (int j = 0; j < width; j++)
                    tilemap[j][i] = 0;

            // fill with the given tiletype
            for (int i = 0; i < height; i++)
                for (int j = 0; j < width; j++)
                    Map.TILESETS[tileset].putRandomBaseTile(this, new Point(j, i), tile);
        }

    }
}
