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 ObservableCollection<ObservableCollection<CellContents>> grid;
        CellContents mode;
        #endregion

        #region Public Constants
        public enum CellContents
        {
            EMPTY,
            WALL,
            ENDPOINT,
            NON_ENDPOINT,
            FINISH,
            START
        }
        #endregion

        public Map()
        {
            InitializeComponent();
            grid = new ObservableCollection<ObservableCollection<CellContents>>();
            for (int i = 0; i < 100; i++)
            {
                ObservableCollection<CellContents> temp = new ObservableCollection<CellContents>();
                for (int j = 0; j < 100; j++)
                {
                    temp.Add(CellContents.EMPTY);
                }
                grid.Add(temp);
            }
            DoBinding();
        }

        #region Private Functions

        private void clearMode()
        {
            for (int i = grid.Count - 1; i >= 0; i--)
            {
                for (int j = grid[i].Count - 1; j >= 0; j--)
                {
                    if (grid[i][j] == mode)
                    {
                        grid[i].RemoveAt(j);
                        grid[i].Insert(j, CellContents.EMPTY);
                    }
                }
            }
        }

        private void DoBinding()
        {
            Binding intArray = new Binding();

            intArray.Mode = BindingMode.OneWay;

            intArray.Source = grid;

            map.SetBinding(ItemsControl.ItemsSourceProperty, intArray);
        }

        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--)
                {
                    ObservableCollection<CellContents> temp = new ObservableCollection<CellContents>();
                    for (int j = previousColumns; j > 0; j--)
                    {
                        temp.Add(CellContents.EMPTY);
                    }
                    grid.Add(temp);
                }
            }
            else if (rows < grid.Count)
            {
                for (int i = grid.Count - rows; i > 0; i--)
                {
                    grid.RemoveAt(rows);
                }
            }
            else if (grid.Count > 0)
            {
                if (columns > previousColumns)
                {
                    foreach (ObservableCollection<CellContents> cell in grid)
                    {
                        for (int i = columns - previousColumns; i > 0; i--)
                        {
                            cell.Add(CellContents.EMPTY);
                        }
                    }
                }
                else if (columns < previousColumns)
                {
                    foreach (ObservableCollection<CellContents> cell in grid)
                    {
                        for (int i = previousColumns - columns; i > 0; i--)
                        {
                            cell.RemoveAt(columns);
                        }
                    }
                }
            }
        }

        #endregion

        #region Public Methods

        public void setMode(CellContents mode)
        {
            this.mode = mode;
        }

        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].RemoveAt(j);
                        grid[i].Insert(j, CellContents.WALL);
                    }
                    else
                    {
                        grid[i].RemoveAt(j);
                        grid[i].Insert(j, CellContents.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].RemoveAt(y);
                grid[x].Insert(y, CellContents.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].RemoveAt(y2);
                grid[x2].Insert(y2, CellContents.FINISH);
            }
        }

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

        public void Set(int x, int y, CellContents value)
        {
            if (x < grid.Count)
            {
                if (y < grid[x].Count)
                {
                    grid[x].RemoveAt(y);
                    grid[x].Insert(y, value);
                }
            }
        }

        #endregion

        #region Properties

        public ObservableCollection<ObservableCollection<CellContents>> 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

        #region Action Handlers
        public void cellClick(object sender, RoutedEventArgs e)
        {
            Rectangle src = (Rectangle)sender;
            Color temp = ((SolidColorBrush)src.Fill).Color;
            if (mode != CellContents.START && mode != CellContents.FINISH)
            {
                if (temp.Equals(Colors.White))
                {
                    ((SolidColorBrush)src.Fill).Color = Colors.Black;
                }
                else
                {
                    ((SolidColorBrush)src.Fill).Color = Colors.White;
                }
            }
            else
            {
                clearMode();
                if (mode == CellContents.START)
                {
                    ((SolidColorBrush)src.Fill).Color = Colors.Green;
                }
                else
                {
                    ((SolidColorBrush)src.Fill).Color = Colors.Red;
                }
            }
        }
        #endregion
    }

    public class CellContentConverter : IValueConverter
    {

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                Map.CellContents type = (Map.CellContents)value;
                switch (type)
                {
                    case Map.CellContents.EMPTY:
                        return Colors.White;
                    case Map.CellContents.WALL:
                        return Colors.Black;
                    case Map.CellContents.NON_ENDPOINT:
                        return Colors.DarkBlue;
                    case Map.CellContents.START:
                        return Colors.Green;
                    case Map.CellContents.FINISH:
                        return Colors.Red;
                    default:
                        return Colors.Blue;
                }
            }
            catch (Exception e)
            {
                return Colors.DarkCyan;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Color temp = (Color)value;
            if (temp.Equals(Colors.White))
                return Map.CellContents.EMPTY;
            else if (temp.Equals(Colors.Black))
                return Map.CellContents.WALL;
            else if (temp.Equals(Colors.DarkBlue))
                return Map.CellContents.NON_ENDPOINT;
            else if (temp.Equals(Colors.Green))
                return Map.CellContents.START;
            else if (temp.Equals(Colors.Red))
                return Map.CellContents.FINISH;
            else
                return Map.CellContents.ENDPOINT;
        }

        #endregion
    }
}