from cromulent.model import *
from cromulent.vocab import *

from utils.aat_labels import aat_labels
from utils.aat_label_fetcher import get_or_fetch

from utils.data_parsing import find_values
from utils.crom_helpers import props, toJSON, toString, printString, printAttr,\
                               type_maker, datetime_maker, date_maker

make_type = type_maker(
    vocab='aat:', getter=get_or_fetch, labels=aat_labels, Class=Type)


def cromify(record):

    o = LinguisticObject(ident=record.pi_record_no) # or no ident?
    o.label = 'Record Text'
    o.classified_as = make_type(300027146) # or 300026685

    star_record_no = Identifier()
    star_record_no.label = 'star_record_no' # STAR record number
    star_record_no.value = record.star_record_no
    o.identified_by = star_record_no

    pi_record_no = Identifier()
    pi_record_no.label = 'pi_record_no' # Provenance Index record number
    pi_record_no.value = record.pi_record_no
    o.identified_by = pi_record_no

    row_number = Identifier()
    row_number.label = 'row_number' # row number
    row_number.value = record.row_number
    o.identified_by = row_number

    # Does this connection needs to be made on page side to reduce redundancy?
    page_id = '%s-%s' % (record.stock_book_no, record.page_number)
    page = LinguisticObject(ident=page_id)
    #page.value = record.page_number
    #page.classified_as = make_type(300200294) # pagination
    o.referred_to_by = page

    # Page Dimensions
    # http://linked.art/model/document/index.html#page-dimensions
    # (not used)

    # Creation
    # http://linked.art/model/document/index.html#creation-and-publication
    # (not used)

    # linguistic objects and their AAT IDs:
    fields_and_ids = [
        ('description', 300027200),
        ('subject', 300404126),
        ('genre', 300056462),
        ('object_type', 300191790),
        ('materials', 300010358),
        ('dimensions', 300266036),
        ('working_note', 300027200),
        ('verbatim_notes', 300027200),
    ]
    for field, aat_id in fields_and_ids:
        text = getattr(record, field)
        if text:
            lo = LinguisticObject()
            lo.label = text
            lo.classified_as = make_type(aat_id) # notes
            o.part = lo

    mmo = ManMadeObject(ident=record.object_id)
    o.refers_to = mmo
    if record.sale_event_id:
        sale = Acquisition(ident=record.sale_event_id)
        o.refers_to = sale
    if record.purchase_event_id:
        purchase = Acquisition(ident=record.purchase_event_id)
        o.refers_to = purchase
    elif record.inventory_event_id:
        inventory = Activity(ident=record.inventory_event_id)
        o.refers_to = inventory

    #printString(o)
    return o
