import jsonpickle
import random
import string
import os.path
import getopt
import sys

from model.group import Group


# create parametrize generator (need import getopt and sys)
try:
    opts, args = getopt.getopt(sys.argv[1:], "n:f:", ["numbers of groups", "file"])
except getopt.GetoptError as err:
    getopt.usage()
    sys.exit(2)

# analys for parametrs
n = 5
f = "data/groups.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 + string.punctuation+" "*10
    return prefix + "".join(random.choice(symbols) for i in range(random.randrange(maxlen)))

testdata = [Group(name="", header="", footer="")] + [
    Group(name=random_string("name", 10), header=random_string("header", 20), footer=random_string("footer", 20))
    for i in range(n)]

# save data after generation
file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", f)

# open file for record
with open(file, "w") as out:
    jsonpickle.set_encoder_options("json", indent=2)
    out.write(jsonpickle.encode(testdata))
