import jsonpickle
import random
import string
from model.contact import Contact
import os.path
import getopt
import sys


try:
    opts, args = getopt.getopt(sys.argv[1:], "n:f:", ["number of contacts", "file"])
except getopt.GetoptError as err:
    getopt.usage()
    sys.exit(2)

n = 5
f = "data/contacts.json"

for o, a in opts:
    if o == "-n":
        n = int(a)
    elif o == "-f":
        f = a


def random_string(prefix, maxlen):
    symbols = string.ascii_letters + string.digits + " "*10
    return prefix + "".join([random.choice(symbols) for i in range(random.randrange(maxlen))])


testdata = [Contact(first_name="", middle_name="", last_name="", nickname="", title="", company="", address="",
                    home_tel="", mobile_tel="", work_tel="", fax="", email="", homepage="", birthday="",
                    anniversary="", secondary_address="", secondary_tel="", notes="")] + [
    Contact(first_name=random_string('first_name', 10), middle_name=random_string('middle_name', 10), last_name=random_string('last_name', 10),
            nickname=random_string('nickname', 10), title=random_string('random_string', 10), company=random_string('company', 10),
            address=random_string('address', 10), home_tel=random_string('home_tel', 10), mobile_tel=random_string('mobile_tel', 10),
            work_tel=random_string('work_tel', 10), fax=random_string('fax', 10), email=random_string('email', 10),
            homepage=random_string('homepage', 10), birthday=random_string('birthday', 10), anniversary=random_string('anniversary', 10),
            secondary_address=random_string('secondary_address', 10), secondary_tel=random_string('secondary_tel', 10), notes=random_string('notes', 10))
    for i in range(5)
]


file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", f)

with open(file , "w") as out:
    jsonpickle.set_encoder_options("json", indent=2)
    out.write(jsonpickle.encode(testdata))
[((11, 17, 11, 84), 'getopt.getopt', 'getopt.getopt', ({(11, 31, 11, 43): 'sys.argv[1:]', (11, 45, 11, 51): '"""n:f:"""', (11, 53, 11, 83): "['number of contacts', 'file']"}, {}), "(sys.argv[1:], 'n:f:', ['number of contacts', 'file'])", False, 'import getopt\n'), ((47, 4, 47, 52), 'jsonpickle.set_encoder_options', 'jsonpickle.set_encoder_options', (), '', False, 'import jsonpickle\n'), ((13, 4, 13, 18), 'getopt.usage', 'getopt.usage', ({}, {}), '()', False, 'import getopt\n'), ((14, 4, 14, 15), 'sys.exit', 'sys.exit', ({(14, 13, 14, 14): '(2)'}, {}), '(2)', False, 'import sys\n'), ((31, 12, 33, 85), 'model.contact.Contact', 'Contact', (), '', False, 'from model.contact import Contact\n'), ((48, 14, 48, 41), 'jsonpickle.encode', 'jsonpickle.encode', ({(48, 32, 48, 40): 'testdata'}, {}), '(testdata)', False, 'import jsonpickle\n'), ((28, 29, 28, 51), 'random.choice', 'random.choice', ({(28, 43, 28, 50): 'symbols'}, {}), '(symbols)', False, 'import random\n'), ((28, 67, 28, 91), 'random.randrange', 'random.randrange', ({(28, 84, 28, 90): 'maxlen'}, {}), '(maxlen)', False, 'import random\n')]