# -*- coding: utf-8 -*-
"""
    PUSH API for scrapi prototype
"""
from __future__ import unicode_literals

import copy
import json
import logging
import requests
import datetime

# from scrapi import settings
from scrapi.linter import lint
from scrapi.linter.document import RawDocument, NormalizedDocument


logging.basicConfig(level=logging.ERROR)
logger = logging.getLogger(__name__)


def tutorial():
    return {
        "title": "string representing title of the resource",
        "contributors": "a list of dictionaries containing prefix, middle, family, suffix, and ORCID of contributors.",
        "id": "a dictionary of unique IDs given to the resource based on the particular publication we’re accessing. Should include an entry for a URL that links right to the original resource, a DOI, and a service specific ID",
        "url": "A url pointing to the resources\'' real location",
        "doi": "The digital object identifier of the resource, if it has one",
        "serviceID": "A service specific identifier for the resource",
        "description": "an abstract or general description of the resource",
        "tags": "a list of tags or keywords identified in the resource itself, normalized to be all lower case",
        "source": "string identifying where the resource came from",
        "timestamp": "string indicating when the resource was accessed by scrAPI using the format YYYY-MM-DD h : m : s in iso format",
        "dateCreated": "string indicating when the resource was first created or published using the format YYYY-MM-DD in iso format",
        "dateUpdated": "string indicating when the resource was last updated in the home repository using the format YYYY-MM-DD in iso format",
    }

def process_api_input(input_data):
    ''' Takes a list of documents as raw input from API route
    returns a list of linted normalzied documents
    '''
    raw_documents = consume(input_data)
    
    return lint_results(input_data)


def consume(input_data):
    ''' takes a list of input from the api route,
    returns a list of raw documents 
    '''
    event_list = json.loads(input_data)

    raw_doc_list = []
    for event in event_list:
        raw_doc_list.append(RawDocument({
            'doc': json.dumps(event),
            'source': event.get('source'),
            'docID': event.get('id')['serviceID'],
            'filetype': 'json'
        }))

    return raw_doc_list


def normalize(raw_doc):
    raw = raw_doc.get('doc')
    normalized_dict = json.loads(raw)

    return NormalizedDocument(normalized_dict)


def lint_results(input_data):
    print(lint(lambda: consume(input_data), normalize))


@@ -202,16 +202,50 @@
 ettings%0A
+from scrapi.util import timestamp%0A
 from scr
@@ -1956,44 +1956,1037 @@
     
-%0A    return lint_results(input_data)
+print(lint_results(input_data))%0A%0A    consumed_docs = task_consume(raw_documents)%0A%0A    import pdb; pdb.set_trace()%0A%0Adef task_consume(raw_documents):%0A    ''' takes in the raw_doc_list and emulates the %0A    normal scrapi consume task, adding appropriate%0A    timestamps and returning a tuple consisting%0A    of the raw doc list and the dict of timestamps%0A    '''%0A%0A    timestamps = %7B%0A        'consumeTaskCreated': timestamp(),%0A        'consumeStarted': timestamp(),%0A        'consumeFinished': timestamp()%0A    %7D%0A%0A    return raw_documents, timestamps%0A%0A%0Adef task_normalize(raw_doc):%0A    ''' emulates the normalize function in the celery%0A    tasks, adds timestamps to the raw doc and returns%0A    a single normalized document with the correct%0A    timestamps%0A    '''%0A%0A    raw_doc%5B'timestamps'%5D%5B'normalizeStarted'%5D = timestamp()%0A%0A    normalizeStarted = timestamp()%0A%0A    normalized = normalize(raw_doc)%0A%0A    normalized%5B'timestamps'%5D = raw_doc%5B'timestamps'%5D%0A    normalized%5B'timestamps'%5D%5B'normalizeFinished'%5D = timestamp()%0A%0A    return normalzied
 %0A%0A%0Ad

add appropriate timestamps to api harvested input