﻿using System;
using System.Collections.Generic;

namespace GOL_2016.model
{
    public class BoardCreator
    {
        public IDictionary<ICellKey, ICell> CreateBoard()
        {
            int boardSize = 40;
            IDictionary<ICellKey, ICell> board = new Dictionary<ICellKey, ICell>();
            for (int i = 0; i < boardSize; i++)
            {
                for (int j = 0; j < boardSize; j++)
                {
                    ICellKey key = new CellKey(i, j);
                    bool isAlive = IsCellAlive(i, j);
                    ICell cell = new Cell(key, isAlive);

                    board.Add(cell.Key, cell);
                }
            }
            CreateCellCrossReferences(board);
            return board;
        }

        void CreateCellCrossReferences(IDictionary<ICellKey, ICell> board)
        {
            foreach (var cell in board.Values)
            {
                cell.FillReferences(board);
            }
        }

        bool IsCellAlive(int x, int y)
        {
            return Random();
            return s_LiveCells.Contains(new CellKey(x, y));
        }

        bool Random()
        {
            Random rand = new Random(Guid.NewGuid().GetHashCode());
            return Convert.ToBoolean(rand.Next(0,2));
        }
        static IList<ICellKey> s_LiveCells = new List<ICellKey> {
            ////new CellKey(1, 1),
            ////new CellKey(1, 2),
            ////new CellKey(4, 7)

            // Blinker
            //new CellKey(1, 2),
            //new CellKey(2, 2),
            //new CellKey(3, 2)

            // Glider
            new CellKey(1, 1),
            new CellKey(2, 2),
            new CellKey(3, 2),
            new CellKey(1, 3),
            new CellKey(2, 3)

        };
    }
}
