import sys
import logging
import requests
import errno
import json

from mementoembed import MementoSurrogate
from archivenow import archivenow

if __name__ == '__main__':

    logger = logging.getLogger(__name__)

    logging.basicConfig(
        format='%(asctime)s [%(levelname)s] %(name)s: %(message)s',
        level=logging.INFO,
        filename='fetch_surrogate.log'
    )

    try:
        uri = sys.argv[1]
    except IndexError as e:
        print("You must supply a URI as the argument to this command")
        sys.exit(errno.EINVAL)

    logger.info("Starting process for URI {}".format(uri))

    # HEAD the URI
    resp = requests.head(uri, allow_redirects=True)

    logger.info("HEAD on URI {}, HTTP status: {}".format(uri, resp.status_code))

    try:
        mdt = resp.headers['memento-datetime']
        logger.info("Detected a memento, submitted URI {} will be treated as a URI-M")
        urim = uri

    except KeyError as e:
        # if it is a URI-R, create a URI-M with archivenow.push
        logger.info("No Memento-Datetime detected for URI {}, archiving now...".format(uri))
        # urims = archivenow.push(uri, "all")
        # not all archives respond after URI-Ms are created, so we use ia
        urims = archivenow.push(uri, "ia")
        urim = urims[0]

    # TODO: handle meta-tag refreshes
    
    logger.info("Using URI-M {} for surrogate extraction".format(urim))

    # do a GET on it and pass it into Surrogate
    resp = requests.get(urim)
    mdt = resp.headers['memento-datetime']

    logger.info("creating surrogate information")

    s = MementoSurrogate(urim)

    output = {
        "URI-M": urim,
        "Memento-Datetime": s.memento_datetime,
        "Text Snippet": s.text_snippet,
        "Striking Image": s.striking_image,
        "Title": s.title,
        "Member of Archive": s.archive_name,
        "Archive URI": s.archive_uri,
        "Archive Collection": s.collection_name,
        "Archive Collection URI": s.archive_collection_uri,
        "Archive Favicon": s.archive_favicon,
        "Original Favicon": s.original_favicon,
        "Original Link Status": s.original_link_status,
        "Generated Datetime": s.surrogate_creation_time
    }

    print(json.dumps(output))

    logger.info("Finished processing.")