 config = require '../config'
 Content = require './content'
 Embed = require './embed'
+Errors = require './errors'
 Fetcher = require './fetcher'
 path = require 'path'
 Processor = require './processor'
+URL = require 'url'
 
 # Constants
 PIPELINE_PATH = path.join __dirname, '..', 'pipeline'

#
# Pre-reqs: (none)
#
# TODO: Zoom.it back-compat of ?url query string support.
#
@getHomepage = (req, res, _) ->
    res.render 'home'

#
# Pre-reqs:
# - req.params.id
#
@getEmbed = (req, res, _) ->
    id = parseInt req.params.id, 10
    if not id? or isNaN id
        return res.send 404
    res.send embed.generate id, _


#
# Pre-reqs:
# - req.params.id
#
@getViewer = (req, res, _) ->
    id = parseInt req.params.id, 10
    if not id? or isNaN id
        return res.send 404
    content = Content.getById id, _
    if not content?
        return res.send 404
    res.render 'view', {content}


#
# Pre-reqs:
# - req.params.url
#
@submitURL = (req, res, _) ->
    # FIXME: This sometimes returns API JSON; this route should always
    # redirect to the view page.
    handleURL res, req.params.url, _


## HELPERS:

# Helper for the two different routes for URLs:
handleURL = (res, url, _) ->
    if not url?
        res.json 400, error:
            message: 'Please give us the full URL,
                including `http://` or `https://`.'
        return false

    content = Content.getByURL url, _
    if content?
        res.redirect content.shareUrl
        return false

    content = Content.fromURL url, _
    # Redirect to metadata
    res.redirect content.self

    # Async operations:

    # Fetch source
    source = fetcher.fetch content, _

    # Create DZI
    destination = processor.process source, _


#
# Pre-reqs:
# - `id` param
#
@getContentById = (req, res, _) ->
    id = req.param 'id'
    if not id
        errorAPI res, 404, Errors.MISSING_ID_OR_URL
        return

    content = Content.getById id, _
    if not content?
        errorAPI res, 404, Errors.NO_CONTENT_WITH_ID.replace '{ID}', id
        return

    res.json 200, content

#
# Pre-reqs:
# - `url` param
#
@getContentByURL = (req, res, _) ->
    url = req.param 'url'
    if not url
        errorAPI res, 404, Errors.MISSING_ID_OR_URL
        return
    if not validateURL url
        errorAPI res, 400, Errors.MALFORMED_URL
        return

    # Get or create this content, enqueueing it for conversion if it's new.
    # But support us rejecting new content, e.g. if we're overloaded.
    # TODO: Implement config for rejecting new content.
    #
    # TODO: This logic should probably be abstracted in the Content class,
    # since it's core business logic, used by (and currently duplicated by)
    # both our API and our website. But in order to do that, we'd want it to
    # throw semantic error codes, so that error handling can remain custom.
    #
    content = Content.getByURL url, _
    if not content
        if false
            errorAPI res, 503, Errors.SERVICE_UNAVAILABLE
            return
        else
            content or= Content.fromURL url, _
            enqueueForConversion content

    # HACK: Hardcoding knowledge of URL from app._coffee.
    redirectAPI res, 301, "/v1/content/#{content.id}", content


## UI:
