fs = require 'fs'
Loggable = (require './Loggable.coffee').Loggable
CompressedUnsignedLongByteIterable =
  (require './iterate/CompressedUnsignedLongByteIterable.coffee').
  CompressedUnsignedLongByteIterable
UnsupportedOperationError =
  (require '../errors/UnsupportedOperationError.coffee').
  UnsupportedOperationError

# This class allows to create blocks of data - logables - and write them to log,
# using WriteIterator.
class LoggableToWrite extends Loggable
  # @private
  type: undefined
  # @private
  data: undefined
  # @private
  structureId: undefined

  # Constructor.
  #
  # @param type the type of loggable.
  # @param data the ByteIterable with data.
  @create$int$ByteIterable: (type, data, o) ->
    return LoggableToWrite.create$int$ByteIterable$int type, data,
            Loggable.NO_STRUCTURE_ID, o

  # Constructor.
  #
  # @param type the type of loggable.
  # @param data the ByteIterable with data.
  # @param structureId.
  @create$int$ByteIterable$int: (type, data, structureId, o) ->
    if !o? then o = new LoggableToWrite
    o.type = type
    o.data = data
    o.structureId = structureId
    return o

  getAddress: () ->
    throw new UnsupportedOperationError("Loggable for writing has" +
    " no address until it is written to log");

  getType: () =>
    return @type

  # Get length.
  #
  # @return the length of the whole loggable, including header and data.
  length: () =>
    return @data.length +
          CompressedUnsignedLongByteIterable.getCompressedSize$int(@type) +
          CompressedUnsignedLongByteIterable.getCompressedSize$int(@structureId) +
          CompressedUnsignedLongByteIterable.getCompressedSize$int(@data.length)

  getData: () ->
    return @data

  getDataLength: () ->
    return @data.length

  getStructureId: () ->
    return @structureId

exports.LoggableToWrite = LoggableToWrite
