###
Module dependencies
###

colors = require 'colors'

class Logger

  constructor: (@options) ->

    @messages =
      configuration:
        "header": 'Connect VTEXID Options:'.bold.underline.red
        "ttl": 'TTL: '.yellow + "#{options.ttl} seconds".green
        "redirectUrl": 'Redirect Url: '.yellow + "#{options.redirectUrl}".green
        "replaceWhiteList": 'Replace white-list: '.yellow + "#{options.replaceWhiteList}".green
        "whiteListCustomRoutes": 'Routes to add in white-list: '.yellow + "#{options.addToWhiteList}".green
        "withReturnUrl": 'Return url: '.yellow + "#{options.returnUrl}".green if options.returnUrl
        "withoutReturnUrl": 'Return url: '.yellow + "none".green if not options.returnUrl
        "logoutUrl": 'Logout url: '.yellow + "#{options.logoutUrl}".green
      procedures:
        "whiteList": '>> '.green + 'Setting white-list...'.yellow
        "userLogout": '>> '.green + 'User logging out...'.yellow
        "cache": ">> ".green + "Setting up cache...".yellow
        "redirectUrl": '# '.cyan + "Resolving redirect url...".magenta
        "cookiesParsing": '# '.cyan + "Parsing cookies...".magenta
        "vtexIdCookieNotFound": '# '.cyan + "No vtex-id cookie found. Redirecting to login page...".magenta
        "cacheCheck": '# '.cyan + "Checking cache for vtex-id cookie...".magenta
        "apiRequest": '# '.cyan + "Cookie is not cached. Requesting vtex-id api for user information...".magenta
        "loginPageRedirect": '# '.cyan + "API did not respond with a proper body. Redirecting to login page...".red
        "unauthorized": '# '.cyan + "Could not authenticate the user. Returning 401...".red
      withCustomParameter:
        "whiteListRouteConfig": (route) -> " - #{route}".blue
        "whiteListHit": (url) -> '# '.cyan + 'Route '.magenta + "#{url}".blue + " is in white-list. Bypassing...".magenta
        "redirectUrl": (url) -> " - #{url}".blue
        "cookie": (cookie, value) -> "#{cookie}: ".yellow + "#{value}".blue
        "cachedUser": (user) -> '# '.cyan + "User ".magenta + "#{user}".blue + " is already in cache".magenta
        "apiResponseCode": (code) -> ' >> '.blue + "Vtex-id api responded with code ".yellow + "#{code}".green
        "cachingUser": (user) -> '# '.cyan + "Caching user ".magenta + "#{user}".blue

  log = (message) -> console.log "[connect-vtexid] " + message

  logConfigOptions: ->
    if @options.verbosityLevel >= 1
      console.log @messages.configuration["header"]
      console.log @messages.configuration["ttl"]
      console.log @messages.configuration["redirectUrl"]
      console.log @messages.configuration["logoutUrl"]
      console.log @messages.configuration["replaceWhiteList"] if @options.replaceWhiteList
      console.log @messages.configuration["whiteListCustomRoutes"] if @options.addToWhiteList.length
      console.log @messages.configuration["withReturnUrl"] if @options.returnUrl
      console.log @messages.configuration["withoutReturnUrl"] if not @options.returnUrl

  logWhiteListProcedure: ->
    log @messages.procedures["whiteList"] if @options.verbose and @options.verbosityLevel >= 2

  logWhiteListConfiguration: (whiteList) ->
    if @options.verbose and @options.verbosityLevel >= 2
      for route in whiteList.publicUris
        console.log @messages.withCustomParameter["whiteListRouteConfig"](route)

  logCacheSetupProcedure: ->
    log @messages.procedures["cache"] if @options.verbose and @options.verbosityLevel >= 2

  logWhiteListHit: (url) ->
    log @messages.withCustomParameter["whiteListHit"](url) if @options.verbose and @options.verbosityLevel >= 2

  logUserLogout: ->
    log @messages.procedures["userLogout"] if @options.verbose

  logRedirectUrlSetupProcedure: ->
    log @messages.procedures["redirectUrl"] if @options.verbose and @options.verbosityLevel >= 2

  logRedirectUrl: (url) ->
    log @messages.withCustomParameter["redirectUrl"](url) if @options.verbose and @options.verbosityLevel >= 2

  logCookieParsingProcedure: ->
    log @messages.procedures["cookiesParsing"] if @options.verbose and @options.verbosityLevel >= 2

  logCookies: (cookiesList) ->
    if @options.verbose and @options.verbosityLevel >= 3
      for cookie,value of cookiesList
        console.log @messages.withCustomParameter["cookie"](cookie, value)

  logCookieNotFound: ->
    log @messages.procedures["vtexIdCookieNotFound"] if @options.verbose and @options.verbosityLevel >= 2

  logCacheCheck: ->
    log @messages.procedures["cacheCheck"] if @options.verbose and @options.verbosityLevel >= 2

  logCachedUser: (user) ->
    log @messages.withCustomParameter["cachedUser"](user) if @options.verbose

  logApiRequest: ->
    log @messages.procedures["apiRequest"] if @options.verbose and @options.verbosityLevel >= 2

  logApiResponseCode: (code) ->
    log @messages.withCustomParameter["apiResponseCode"](code) if @options.verbose and @options.verbosityLevel >= 2

  logRedirectToLoginPage: ->
    log @messages.procedures["loginPageRedirect"] if @options.verbose and @options.verbosityLevel >= 2

  logUnauthorizedUser: ->
    log @messages.procedures["unauthorized"] if @options.verbose and @options.verbosityLevel >= 2

  logCachingUser: (user) ->
    log @messages.withCustomParameter["cachedUser"](user) if @options.verbose

module.exports = Logger
