# holder for the master list
masterRecipeList = []
# increment isn't likely to change; it's the most ids that can be requested from the recipe endpoint
incr = 200

# this url will return a list of all recipe ids
url = gw2lib.apiBase + gw2lib.recipesSubsect
recipeIDlist = json.load(urlopen(url))

# the last ID isn't really important, just nice to know. lastListIndex is needed to build the list of url requests
lastListIndex = len(recipeIDlist)
lastRecipeID = recipeIDlist[-1]
print "final recipe ID:", lastRecipeID, ", final list index:", lastListIndex

# get the list of request urls and filename ranges, output from makeReqUrlList()
[requestURLs, filenamesStartEnd] = gw2lib.makeReqUrlList(recipeIDlist, url + gw2lib.idsReq, incr)

# i keeps track of the index associated with each set of incr ids stored in requestURLs
i = 0
for req in requestURLs:
    # for each request url, get the json data for those incr recipes
    recipeList = json.load(urlopen(req))
    # add them to the  master list
    masterRecipeList += recipeList
    # make the filename string from filenamesStartEnd[i]
    filenameString = (
        gw2lib.recipeListFolder + "ids" + str(filenamesStartEnd[i][0]) + "_" + str(filenamesStartEnd[i][1]) + ".json"
    )
    # save each set of incr ids to a file (not particularly necessary unless the recipe list gets really big)
    with open(filenameString, "w") as recipeFile:
        json.dump(recipeList, recipeFile)
    # get a list of the ids from the current masterItemList.json, and sort them
    currentIDs = sorted([x['id'] for x in masterItemList ])

    # compare using sets and get a list of the ids that exist in the list from the api but not in the list from
    # masterItemList.json
    newIDs = list(set(itemIDlist)^set(currentIDs))

    # if there are any new ids:
    if len(newIDs) > 0:
        # not necessary, but nice to see the new ids
        print newIDs,'\n',len(newIDs)

        # use makeReqUrlList to get a list containing strings, each of which is a url to request up to 200 item ids
        # from the api. filename ranges isn't really necessary here.
        [urlReqList, filenameRanges] = gw2lib.makeReqUrlList(newIDs)
        newItemList = []

        # again, not necessary, but nice to see all the request urls. plus, you can just click then and check out
        # some of the new items.
        for x in urlReqList:
            print x

        # for each request url, make a call to the api and add the resulting json data to newItemList
        for urlReq in urlReqList:
            newItemList += json.load(urlopen(urlReq))

        # add newItemList to the master list
        masterItemList += newItemList

        # sort the master item list by id using gw2lib.compareByID()
    for ingr in recChk['ingredients']:
        # add the name and a placeholder buy price for each ingredient
        ingr['name'] = masterItemList[str(ingr['item_id'])]['name'] #gw2lib.findByID(ingr['item_id'], masterItemList)['name']
        ingr['costInfo'] = None
        # and add the id to the ids list
        ingrID = ingr['item_id']
        if masterItemList[str(ingrID)]['TPable']:
            tradeIDs.append(ingrID)
        else:
            allIDsDict[ingrID] = {'source':'something', 'cost':'someNumber'}
        # call chkRec to get the same info for each ingredient
        ingr['ingredients'] = chkRec(ingrID, ingr['count'])
    # append the dict to the tradeChecks list
    recipeChecks.append(recChk)

urlReqList = gw2lib.makeReqUrlList(tradeIDs, gw2lib.apiBase+gw2lib.commercePricesSubsect+gw2lib.idsReq)[0]

tradePrices = []
# get all the trade objects and put them in tradePrices
for req in urlReqList:
    tradePrices += json.load(urlopen(req))

# add an entry for each item on tp into allIDsDict, which is a dict containing price info
for tp in tradePrices:
    iid = tp['id']
    allIDsDict[iid] = { 'source':'trading post', 'buyPrice':tp['buys']['unit_price'], 'sellPrice':tp['sells']['unit_price'] }

# go through all the recipes
for recipe in recipeChecks:
    # add the costInfo dict by copying it from  allIDsDict
    recipe['costInfo'] = allIDsDict[recipe['sourceID']].copy()