package skeb

import scala.gui.Event
import scala.collection.immutable.{Map, TreeMap}

/** A sheet made of a two-dimensional matrix of cells. When initialised, all
  * cells are instantiated as empty.
  * @param width: The number of columns of the cell matrix.
  * @param height: The number of rows of the cell matrix. */
class Sheet (val width: Int, val height: Int) {
  
  /** A function that parses a user-inputed string to transform it into a cell
    * expression. */
  val parse = new Parser(this)
  
  /** The two-dimensional matrix containing all cells. */
  private val cells: Array[Array[skeb.Cell]] = {
    val array = new Array[Array[skeb.Cell]](width)
    for (val cindex <- 0 until width) {
      val colArray = new Array[skeb.Cell](height)
      for (val rindex <- 0 until height)
        colArray.update(rindex, new skeb.Cell {
          val sheet = Sheet.this
          val column = cindex
          val row = rindex
        })
      array.update(cindex, colArray)
    }
    array
  }
  
  /** Returns one specific cell in the sheet, defined by its coordinates.
    * @param cindex The index of the column for the cell.
    * @param rindex The index of the row for the cell.
    * @returns The cell at the given coordinates. */
  def apply (cindex: Int, rindex: Int): skeb.Cell =
    if (cindex >= 0 && cindex < width && rindex >= 0 && rindex < height)
      cells(cindex)(rindex)
    else throw
      new IndexOutOfBoundsException("cindex="+cindex+", rindex="+rindex)
  
  /** An event that is thrown when the value of a cell in the sheet changes. */
  case class CellValueChanged (cell: skeb.Cell) extends Event
  
}
