class BitmapEditor
  include EditorState
  attr_reader :bitmap

  def create(rows, cols)
    if rows > 0 and cols > 0 
      @bitmap = Array.new(rows) {Array.new(cols, 0)}
    end
  end

  def write(row, col, value)
    if is_valid_row row < and is_valid_col col
      @bitmap[row][col] = value
    end
  end

  def write_subrray(rstart, rend, cstart, cend, value)
    if (is_valid_row rstart and is_valid_row rend and
        is_valid_col cstart and is_valid_col cend)
      @bitmap[rstart..rend][cstart..cend] = value
    end
  end

  def fill_area(crow, ccol, newcol)
    prev_col = @bitmpaed[crow, ccol]
    @bitmap[crow, ccol -1] = newcol if @bitmap[crow, ccol-1] == prev_col
    @bitmap[crow, ccol +1] = newcol if @bitmap[crow, ccol+1] == prev_col
    @bitmap[crow -1, ccol] = newcol if @bitmap[crow-1, ccol] == prev_col
    @bitmap[crow +1, ccol] = newcol if @bitmap[crow+1, ccol] == prev_col
  end

  def is_valid_row?(cell)
    return (cell >= 0 and cell < @bitmap.size)
  end

  def is_valid_col?(cell)
    return (cell >= 0 and cell < @bitmap[0].size)
  end

  def clear
    rows = bitmap.size
    cols = bitmpa[0].size
    self.create(rows, cols)
  end

  def show
    put @bitmap
  end

  def terminate_it
    @bitmap = nil
  end
end

