fs = require 'fs'
Reader = require('../io/Reader.coffee').Reader
Writer = require('../io/Writer.coffee').Writer
LogCache = require('./LogCache.coffee').LogCache
IOUtil = require('../util/IOUtil.coffee').IOUtil

# This class contains all the information about log.
class LogConfig
  # @private
  @DEFAULT_FILE_SIZE: 1024 # in kilobytes
  # @private
  dir: undefined
  # @private
  fileSize: undefined
  # @private
  lockTimeout: undefined
  # @private
  memoryUsage: undefined
  # @private
  memoryUsagePercentage: undefined
  # @private
  reader: undefined
  # @private
  writer: undefined
  # @private
  isDurableWrite: undefined
  # @private
  sharedCache: undefined
  # @private
  nonBlockingCache: undefined
  # @private
  cachePageSize: undefined
  # @private
  cacheOpenFilesCount: undefined
  # @private
  writeBufferSize: undefined
  # @private
  cleanDirectoryExpected: undefined

  @create: () ->
    o = new LogConfig
    return o

  getDir: () ->
    return @dir

  setDir: (dir) ->
    @dir = dir

  getFileSize: () ->
    if @fileSize == 0
      @fileSize = @DEFAULT_FILE_SIZE
    return @fileSize

  setFileSize: (fileSize) ->
    @fileSize = fileSize

  getLockTimeout: () =>
    return @lockTimeout

  setLockTimeout: (lockTimeout) ->
    @lockTimeout = lockTimeout

  getMemoryUsage: () ->
    return @memoryUsage

  setMemoryUsage: (memUsage) ->
    @memoryUsage = memUsage

  getMemoryUsagePercentage: () ->
    if !@memoryUsagePercentage?
      @memoryUsagePercentage = 50
    return @memoryUsagePercentage

  setMemoryUsagePercentage: (memoryUsagePercentage) ->
    @memoryUsagePercentage = memoryUsagePercentage

  getReader: () ->
    if !@reader?
      @reader = Reader.create$String$int$int$int @checkDirectory$String(@dir),
              @getFileSize(), @getCachePageSize(), @getCacheOpenFilesCount()
    return @reader

  setReader: (reader) ->
    @reader = reader

  getWriter: () ->
    if !@writer?
      @writer = Writer.create$String$int @checkDirectory$String(@dir), @getFileSize()
    return @writer

  setWriter: (writer) ->
    @writer = writer

  isDurableWrite: () ->
    return @isDurableWrite

  setDurableWrite: (durableWrite) ->
    @isDurableWrite = durableWrite

  isSharedCache: () ->
    return @sharedCache

  setSharedCache: (sharedCache) ->
    @sharedCache = sharedCache

  isNonBlockingCache: () ->
    @nonBlockingCache

  setNonBlockingCache: (nonBlockingCache) ->
    @nonBlockingCache = nonBlockingCache

  getCachePageSize: () ->
    if !@cachePageSize?
      @cachePageSize = LogCache.MINIMUM_PAGE_SIZE
    return @cachePageSize

  setCachePageSize: (cachePageSize) ->
    @cachePageSize = cachePageSize

  getCacheOpenFilesCount: () ->
    if !@cacheOpenFilesCount?
      @cacheOpenFilesCount = LogCache.DEFAULT_OPEN_FILES_COUNT
    return @cacheOpenFilesCount

  setCacheOpenFilesCount: (cacheOpenFilesCount) ->
    @cacheOpenFilesCount = cacheOpenFilesCount

  getWriteBufferSize: () ->
    if (@writeBufferSize == 0)
      @writeBufferSize = 1
    return @writeBufferSize

  setWriteBufferSize: (pagesCount) ->
    @writeBufferSize = pagesCount

  isCleanDirectoryExpected: () ->
    return @cleanDirectoryExpected

  setCleanDirectoryExpected: (cleanDirectoryExpected) ->
    @cleanDirectoryExpected = cleanDirectoryExpected

  # @private
  # Checks whether the directory is valid.
  #
  # @param directory the full path to the directory
  # @return updated path
  checkDirectory$String: (directory) ->
    if directory[directory.length - 1] != '/'
      directory += '/'
    IOUtil.makeDirectory$String directory
    return directory

exports.LogConfig = LogConfig