#!/bin/python3

import vobject
import datetime
# import uuid


# the dict passed in (output_person_details-person_dict{},
# output_office_details-office_dict{}) should be of the form read from
# a valid vcf vcard by vobject.
def create_vcard(dict):
    line = vobject.vCard()
    line.add('n')
    givenName = dict["name"].split(" ")[0]
    familyName = dict["name"].split(" ")[-1]
    familyName = "Harris"
    givenName = "Jeffrey"
    line.n.value = vobject.vcard.Name(family=familyName, given=givenName)

    print(type(line))
    line.add('fn')
    # line.fn.value =  dict["givenName"] + ' ' + dict["familyName"]
    line.fn.value = dict["name"]

    line.add('org')
    line.org.value = dict['work_office']

    # line.add('nickname')
    # line.nickname.value =  dict.nickname

    # unlimited number of separate values, map to list with foreach areas of
    # practice
    if "Areas_of_Practice" in dict:
        line.add('categories')
        line.categories.type_param = "charset=utf"
        for category in dict['Areas_of_Practice']:
            line.categories.value = category  # multiple line from list in dict, needs a loop FIXME

    # six lines, but the last 4 are city, state, zip and country for thunderbird
    # Reverse index for the last 5 lines, concatenate any remaining in first line.
    if "address" in dict:
        line.add('adr')
        line.adr.type_param = "work"
        print(dict["address"])
        print(len(dict["address"]))
    else:
        print("Bust")
    print(type(line))
    line.prettyPrint()
    # line.adr.value =  ",".join(dict.address[0:len(dict.work_address)-5)]) # line one
    # line.adr.value =  dict.address[-5] # line two
    # line.adr.value =  dict.address[-4] # city
    # line.adr.value =  dict.address[-3] # state
    # line.adr.value =  dict.address[-2] # zip
    # line.adr.value =  dict.address[-1] # country

    # six lines, but the last 4 are city, state, zip and country for thunderbird
    # line.add('adr')
    # line.type_param = "home"
    # line.adr.value =  dict. # line one
    # line.adr.value =  dict. # line two
    # line.adr.value =  dict. # city
    # line.adr.value =  dict. # state
    # line.adr.value =  dict. # zip
    # line.adr.value =  dict. # country

    line.add('tel')
    line.tel.type_param = "work"
    line.tel.value = dict["tel"]

    # line.add('tel')
    # line.tel.type_param = "home"
    # line.tel.value =  dict["tel_home"]

    # line.add('tel')
    # line.tel.type_param = "cell"
    # line.tel.value =  dict["tel_cell"]

    # line.add('tel')
    # line.tel.type_param = "fax"
    # line.tel.value =  dict["tel_fax"]

    # line.add('tel')
    # line.tel.type_param = "pager"
    # line.tel.value =  dict["tel_pager"]

    # line.add('x-mozilla-html')
    # line.value =  False

    line.add('email')
    line.email.type_param = "work"
    line.email.value = dict["email"]

    # line.add('email')
    # line.email.type_param = "home"
    # line.email.value =  dict["email_home"]

    line.add('url')
    line.email.type_param = "work"
    line.email.value = dict["url"]

    # line.add('url')
    # line.url.type_param = "home"
    # line.url.value =  dict["url_home"]

    # line.add('title')
    # line.title.value =  dict["title"]

    # Maybe reuse for date admitted or add an extra field
    # line.add('bday')
    # line.bday.value = '1970-01-01'
    # line.bday.value =  dict["bday"]

    # custom value 1-4
    # use for sra_id
    line.add('custom1')
    line.custom1.value = dict["sra_id"]

    # use for dx_address
    line.add('custom2')
    line.custom2.value = dict["dx_address"]

    # use for type
    line.add('custom3')
    line.custom3.value = dict["type"]

    # line.add('custom4')
    # line.custom4.value =  dict[""]

    # single value each separated with "\n"
    line.add('note')
    if 'areas_of_practice' in dict:
        line.note.value = "\n".join(dict["areas_of_practice"].split(","))
    else:
        line.note.value = "\n".join(dict["facilities"].split(","))

    line.add('geo')
    if 'location' in dict:
        line.geo.value = ";".join(dict["location"])
    else:
        pass

    # line.add('x-aim')
    # line.x-aim.value =  dict.aim

    # # generate uuid from lawsoc id number
    # line.add('x-abuid')
    # if 'person_id' in dict:
    # 	line.x-abuid.value =  uuid.uuid3(uuid.NAMESPACE_DNS, dict.person_id)
    # else:
    # 	line.x-abuid.value =  uuid.uuid3(uuid.NAMESPACE_DNS, dict.solicitor_id)

    #
    line.add('rev')
    # line.value =  2015-11-25T23:17:35+00:00 #use date function
    now = datetime.datetime.now()
    line.rev.value = now.isoformat()

    # # morefunctionsforAB-TB3 fields
    # # tab additional, 5 available slots
    # line.add('email')
    # line.email.type_param = "internet"
    # line.email.value =
    # line.email.value =
    # line.email.value =
    # line.email.value =
    # line.email.value =

    # # other data tab - aniversary, spouse, last modified, 8 extra categories
    # line.add('x-anniversary')
    # line.x-anniversay.value =

    # line.add('x-spouse')
    # line.x-spouse.value =

    line.prettyPrint()
    line.serialize()


person_dict = {
    "accreditations": "[]",
    "admitted_date": "2013/01/15",
    "areas_of_practice": "['Private client - wills and probate']",
    "dx_address": "",
    "email": "penny.bull@rhodda.co.uk",
    "languages": "['English']",
    "name": "Victoria Malvina Methven",
    "person_id": "54264",
    "roles": "Employee",
    "solicitor_id": "476467",
    "sra_id": "430668",
    "tel": "01255 433996",
    "type": "SRA Regulated",
    "work_office": "Mike Healy Solicitors",
    "address": [
        "Unit 3",
        "The Croft",
        "Stockbridge Village",
        "Liverpool",
        "Merseyside",
        "L28 1NR",
        "England"
    ],
    "location": {
        "lat": "53.43529",
        "lon": "-2.867382"
    },
    "url": ""
}

office_dict = {
    "address": [
        "Unit 3",
        "The Croft",
        "Stockbridge Village",
        "Liverpool",
        "Merseyside",
        "L28 1NR",
        "England"
    ],
    "dx_address": "",
    "email": "mike@mikehealeysolicitors.com",
    "facilities": "[]",
    "location": {
        "lat": "53.43529",
        "lon": "-2.867382"
    },
    "name": "Mike Healey Solicitors",
    "solicitor_id": "563684",
    "sra_id": "615289",
    "tel": "0151 480 2220",
    "type": "Recognised sole practitioner law practice",
    "web": ""
}

print(create_vcard(person_dict))
