using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace VisualPathFinder
{
    /// <summary>
    /// Interaction logic for Grid.xaml
    /// </summary>

    public partial class Map : System.Windows.Controls.UserControl
    {
        #region Private Variables
        private List<List<Tile>> grid;
        #endregion

        public Map()
        {
            InitializeComponent();
            grid = new List<List<Tile>>();
            changeSize(50, 50);
        }

        #region Private Functions

        private void changeSize(int rows, int columns)
        {
            int previousColumns = grid.Count > 0 ? grid[0].Count : 0;

            if (rows > grid.Count)
            {
                for (int i = rows - grid.Count; i > 0; i--)
                {
                    List<Tile> temp = new List<Tile>();
                    for (int j = previousColumns; j > 0; j--)
                    {
                        Tile tile = new Tile();
                        tile.State = Tile.CellType.EMPTY;
                        temp.Add(tile);
                    }
                    grid.Add(temp);
                }
            }
            else if (rows < grid.Count)
            {
                for (int i = grid.Count - rows; i > 0; i--)
                {
                    grid.RemoveAt(rows);
                }
            }
            if (grid.Count > 0)
            {
                if (columns > previousColumns)
                {
                    foreach (List<Tile> cell in grid)
                    {
                        for (int i = columns - previousColumns; i > 0; i--)
                        {
                            Tile tile = new Tile();
                            tile.State = Tile.CellType.EMPTY;
                            cell.Add(tile);
                        }
                    }
                }
                else if (columns < previousColumns)
                {
                    foreach (List<Tile> cell in grid)
                    {
                        for (int i = previousColumns - columns; i > 0; i--)
                        {
                            cell.RemoveAt(columns);
                        }
                    }
                }
            }

            content.Children.Clear();
            content.RowDefinitions.Clear();
            content.ColumnDefinitions.Clear();
            for (int i = 0; i < columns; i++)
            {
                content.ColumnDefinitions.Add(new ColumnDefinition());
            }
            for (int i = 0; i < rows; i++)
            {
                content.RowDefinitions.Add(new RowDefinition());
            }
            for (int i = 0; i < columns; i++)
            {
                for (int j = 0; j < rows; j++)
                {
                    System.Windows.Controls.Grid.SetRow(grid[j][i], j);
                    System.Windows.Controls.Grid.SetColumn(grid[j][i], i);
                    content.Children.Add(grid[j][i]);
                }
            }
        }

        #endregion

        #region Public Methods

        public void clearPath()
        {
            foreach (List<Tile> list in grid)
            {
                foreach (Tile tile in list)
                {
                    if (tile.State == Tile.CellType.NON_ENDPOINT || 
                        tile.State == Tile.CellType.ENDPOINT || 
                        tile.State == Tile.CellType.HIGHLIGHT)
                    {
                        tile.State = Tile.CellType.EMPTY;
                    }
                }
            }
        }

        public void clearAll()
        {
            foreach (List<Tile> list in grid)
            {
                foreach (Tile tile in list)
                {
                    if (tile.State != Tile.CellType.EMPTY)
                    {
                        tile.State = Tile.CellType.EMPTY;
                    }
                }
            }
        }

        public void fillRandom()
        {
            Random temp = new Random();
            int percent = temp.Next();
            for (int i = grid.Count - 1; i >= 0; i--)
            {
                for (int j = grid[i].Count - 1; j >= 0; j--)
                {
                    if (temp.Next() > percent)
                    {
                        grid[i][j].State = Tile.CellType.WALL;
                    }
                    else
                    {
                        grid[i][j].State = Tile.CellType.EMPTY;
                    }
                }
            }

            if (grid.Count > 0 && grid[0].Count > 0)
            {
                int x = (int)temp.Next(0, grid.Count);
                int y = (int)temp.Next(0,grid[0].Count);

                grid[x][y].State = Tile.CellType.START;

                int x2;
                int y2;
                do
                {
                    x2 = (int)temp.Next(0, grid.Count);
                    y2 = (int)temp.Next(0, grid[0].Count);
                } while (x == x2 && y == y2);

                grid[x2][y2].State = Tile.CellType.FINISH;
            }
        }

        public Tile.CellType At(int x, int y)
        {
            if (x >= 0 && x < grid.Count)
            {
                if (y >= 0 && y < grid[x].Count)
                {
                    return grid[x][y].State;
                }
            }
            return Tile.CellType.WALL;
        }

        public void Set(int x, int y, Tile.CellType value)
        {
            if (x < grid.Count)
            {
                if (y < grid[x].Count)
                {
                    grid[x][y].State = value;
                }
            }
        }

        #endregion

        #region Properties

        public List<List<Tile>> Grid
        {
            get
            {
                return grid;
            }
        }

        public int Rows
        {
            get
            {
                return grid.Count;
            }
            set
            {
                changeSize(value, grid.Count > 0 ? grid[0].Count : 0);
            }
        }
        public int Columns
        {
            get
            {
                if (grid.Count > 0)
                {
                    return grid[0].Count;
                }
                else
                {
                    return 0;
                }
            }
            set
            {
                changeSize(grid.Count, value);
            }
        }
        #endregion
    }
}