using System.Collections.ObjectModel;

namespace Models.Game
{

    /// <summary>
    /// The Map class is the representation of the game map with the board and the operations table.
    /// </summary>
    public class Map
    {
        /// <summary>
        /// It is the list of cells on the map.
        /// </summary>
        public ReadOnlyObservableCollection<Cell> Boards { get; private set; }
        ObservableCollection<Cell> board = new ObservableCollection<Cell>();
        
        /// <summary>
        /// It is the backgrond image of the map
        /// </summary>
        public string Background { get; private set; }

        /// <summary>
        /// It is the grid of the possible operation in the game
        /// </summary>
        public ReadOnlyObservableCollection<OperationCell> OperationGrid { get; private set; }
        ObservableCollection<OperationCell> operationGrid = new ObservableCollection<OperationCell>();

        /// <summary>
        /// It is a list of a list containing user's rope paths in the current game
        /// </summary>
        public List<List<Cell>> RopePaths { get; private set; }

        /// <summary>
        /// It is a list of a list containing user's zones in the current game
        /// </summary>
        public List<List<Cell>> Zones { get; private set; }

        /// <summary>
        /// Initializes a new instance of the Map class.
        /// </summary>
        /// <param name="background">The background of the map.</param>
        public Map(string background)
        {
            Boards = new ReadOnlyObservableCollection<Cell>(board);
            InitializeBoards(board);
            Background = background;
            OperationGrid = new ReadOnlyObservableCollection<OperationCell>(operationGrid);
            InitializeOperationGrid(operationGrid);
            RopePaths = new List<List<Cell>>();
            Zones = new List<List<Cell>>();
        }

        /// <summary>
        /// Initializes the boards of the map.
        /// </summary>
        /// <returns>Return the boards</returns>
        private void InitializeBoards(ObservableCollection<Cell> board)
        {
            for (int i = 0; i < 49; i++) // 6x6 board
            {
                board.Add(new Cell(i / 7, i % 7));
            }
            board[1].Valid = true;
            board[3].Valid = true;
            board[4].Valid = true;
            board[5].Valid = true;
            board[9].Valid = true;
            board[12].Valid = true;
            board[15].Valid = true;
            board[16].Valid = true;
            board[17].Valid = true;
            board[21].Valid = true;
            board[24].Valid = true;
            board[25].Valid = true;
            board[28].Valid = true;
            board[30].Valid = true;
            board[31].Valid = true;
            board[32].Valid = true;
            board[40].Valid = true;
            board[41].Valid = true;
            board[48].Valid = true;

            board[3].IsDangerous = true;
            board[15].IsDangerous = true;
            board[16].IsDangerous = true;
        }
        
        /// <summary>
        /// Initializes the operation grid of the map.
        /// </summary>
        /// <returns>Return the operation grid</returns>
        private void InitializeOperationGrid(ObservableCollection<OperationCell>operationGrid)
        {
            for (int i = 0; i < 5; i++) // 5 operations
            {
                for (int j = 0; j < 4; j++) // 4 cells per operation
                {
                    operationGrid.Add(new OperationCell(i, j));
                }
            }
        }

        public bool CheckOperationPossible(int x)
        {
            return OperationGrid[x * 4 + 3].IsChecked;
        }
    }
}