 {AppStore, AppActions} = require '../../flux/app'
 {CurrentUserStore, CurrentUserActions} = require '../../flux/current-user'
 Dialog = require '../tutor-dialog'
+
+SUPPORT_LINK_PARAMS = '&cu=1&fs=ContactUs&q='
+
+makeContactMessage = (statusCode, message, request) ->
+  userAgent = window.navigator.userAgent
+  location = window.location.href
+
+  errorInfo = "#{statusCode} with #{message} for #{request.method} on #{request.url}"
+
+  if request.data?
+    errorInfo += " with #{request.data}"
+
+  template = "Hello!\nI ran into a problem on #{userAgent} at #{location}.
+    Here is some additional info: #{errorInfo}."
+
+makeContactURL = (supportLinkBase, statusCode, message, request) ->
+  q = encodeURIComponent(makeContactMessage(statusCode, message, request))
+
+  "#{supportLinkBase}#{SUPPORT_LINK_PARAMS}#{q}"
 
 reloadOnce = ->
   navigation = AppStore.errorNavigation()

    onCancel: hideDialog

  default: (error, message, router) ->
    if router? and not error.supportLink?
      {courseId} = router.getCurrentParams()
      error.supportLink = "#{CurrentUserStore.getHelpLink(courseId)}&cu=1&fs=ContactUs&q=begin"
    dialog:
      title: 'Server Error'
      body: <ServerErrorMessage {...error}/>

    statusCode: React.PropTypes.number.isRequired
    message: React.PropTypes.string.isRequired
    request: React.PropTypes.object.isRequired
    supportLinkBase: React.PropTypes.string
    debug: React.PropTypes.bool

  getDefaultProps: ->
    supportLinkBase: CurrentUserStore.getHelpLink()
    debug: true

  render: ->
    {statusCode, message, request, supportLinkBase, debug} = @props
    message ?= 'No response was received'

    q = makeContactMessage(statusCode, message, request)

    dataMessage =  <span>
      with <pre>{request.data}</pre>
    </span> if request.data?

    debugInfo = [
      <p key='error-note'>Additional error messages returned from the server is:</p>
      <pre key='error-response' className='response'>{message}</pre>
      <div key='error-request' className='request'>
        <kbd>{request.method}</kbd> on {request.url} {dataMessage}
      </div>
