def ADD_MAIN():
    ''' Adds a main dish to the database'''
    name = intermediary.get_name()
    meat = intermediary.get_meat()
    VEGGIES = intermediary.get_veggies()
    starch = intermediary.get_starch()
    recipe = intermediary.get_recipe()
    #dish_type = intermediary.get_type()
    dish_type = 'main'
    # Put the name of the recipe and the file into the database
    cur.execute('insert or ignore into recipes (name, type, recipe_file) values (?,?,?)', (name, dish_type, recipe))
    rid = get_recipe_id(name)
    # Try to put the meats and vegetables into the database in case they are not already there
    for veggie in VEGGIES.values():
        if veggie != '':
            add_veggie(veggie)
            id = get_veggie_id(veggie)
            add_recipe_veggie(rid, id)
    cur.execute('insert or ignore into meats (name) values ("%s")' % meat)
    mid = get_meat_id(meat)
    sid = get_starch_id(starch)
    # Put the meat, vegetable, and starch relationships into the database
    add_recipe_meat(rid, mid)
    add_recipe_starch(rid, sid)
    conn.commit()
def ADD():
    name = intermediary.get_name()
    meat = intermediary.get_meat()
    veggies = intermediary.get_veggies()
#    veggies = list(map(str.lower, veggies))
    starch = intermediary.get_starch()
    recipe_string = intermediary.get_recipe()
    cur.execute("insert into recipes values (?,?,?,?,?,?,?,?)", (name, meat, veggies[0], veggies[1], veggies[2], veggies[3], starch, recipe_string))
    conn.commit()
def Edit_recipe(recipe):
    recipe = recipe
    meat = intermediary.get_meat()
    veggies = intermediary.get_veggies()
    veggies = list(map(str.lower, veggies))
    starch = intermediary.get_starch()
    recipe_string = intermediary.get_recipe()
    update = "update recipes set meat='" + meat
    update += "', veggie_one='" + veggies[0]
    update += "', veggie_two='" + veggies[1]
    update += "', veggie_three='" + veggies[2]
    update += "', veggie_four='" + veggies[3]
    update += "', starch='" + starch
    update += "', recipe_file='" + recipe_string
    update += "' where recipe_name='" + recipe + "'"
    cur.execute(update)
    conn.commit()
def EDIT_MAIN(recipe):
    rid = get_recipe_id(recipe)
    id = intermediary.get_id()
    name = get_recipe_name(id)
    # Update the veggies
    VEGGIES = intermediary.get_veggies()
    VIDS = set()
    for veggie in VEGGIES.values():
        if veggie != '':
            add_veggie(veggie)
            VIDS.add(get_veggie_id(veggie))
    ORIGINAL = set(get_recipe_veggies(rid))
    if VIDS != ORIGINAL:
        # this should be a set of the veggies that need to be deleted
        OLD = ORIGINAL - VIDS
        for old in OLD:
            cur.execute('delete from recipe_veggies where recipe_id=%1s and veggie_id=%2s' % (rid, old))
        conn.commit()
        # this should be a set of the new veggies that need to be added
        VIDS = VIDS - ORIGINAL
        for vid in VIDS:
            add_recipe_veggie(rid, vid)
        conn.commit()
    # Update the meat
    meat = get_meat_id(intermediary.get_meat())
    old_meat = get_recipe_meat(rid)
    if meat != old_meat:
        cur.execute('delete from recipe_meats where recipe_id=%u' % rid)
        add_recipe_meat(rid, meat)
    # Update the starch
    starch = get_starch_id(intermediary.get_starch())
    old_starch = get_recipe_starch(rid)
    if starch != old_starch:
        cur.execute('delete from recipe_starches where recipe_id=%u' % rid)
        add_recipe_meat(rid, starch)
    # Update the recipe file
    recipe_file = intermediary.get_recipe()
    if recipe_file != get_recipe_text(rid):
        cur.execute('update recipes set recipe_file="%s" where id=%u' % (recipe_file, rid))
    conn.commit()
    return
def FIND_MAIN(recipe):
    '''finds the meat, veggies, and starch that go with a main dish recipe'''
    rid = get_recipe_id(recipe)
    intermediary.set_name(recipe)
    intermediary.set_type(get_recipe_type(recipe))
    #find the meat
    mid = get_recipe_meat(rid)
    if mid != 'None':
        intermediary.set_meat(get_meat_name(get_recipe_meat(rid)))
    #find the veggies
    VEGGIES = []
    VIDS = get_recipe_veggies(rid)
    for vid in VIDS:
        VEGGIES.append(get_veggie_name(vid))
    intermediary.set_veggies(VEGGIES)
    roar = intermediary.get_veggies()
    #find the starch
    sid = get_recipe_starch(rid)
    if sid != None:
        intermediary.set_starch(get_starch_name(sid))
    #find the recipe
    intermediary.set_recipe(get_recipe_text(recipe))