import pymongo
import markovify
import sys
import random

def log(msg, err=False, tabs=0):
    if (not err):
        print("[*] " + "\t" * tabs + msg)
    else:
        print("[X] " + "\t" * tabs + msg)

def dbg(msg, tabs=0):
    print("[D] " + "\t" * tabs + msg)

if (len(sys.argv) < 3):
    log("Not enough arguments!", err=True)
    sys.exit(1)

# Load the model for the markov chains
file = open("bee-movie.txt", "r")
text = file.read()
file.close()

# Create the model using Markov Chains
model = markovify.Text(text)

# Generate 10 levels
levels = []
vocab = list(range(1, 100))
level = 1
for i in range(10):
    name = model.make_short_sentence(50, tries=100)
    description = ". ".join([model.make_sentence(tries=100) for x in range(4)])
    level_vocab = []

    # Get random vocabulary
    for v in range(4):
        index = random.randint(0, len(vocab))
        level_vocab.append(index)
        del vocab[index]
        
    print("{0}: {1} -> {2}".format(level, name, description))
    levels.append({
        "level": level,
        "name": name,
        "description": description,
        "vocab": level_vocab
    })
    level += 1
    
log("Connecting to database...")    
client = pymongo.MongoClient(sys.argv[1])
log("Getting DB...")
db = client[sys.argv[2]]
log("Inserting...")
res = db["levels"].insert_many(levels)

log("Success", tabs=1)
