#fileLoader
#loads in and creates objects from a file definition generated by rfid readers

# import the needed modules
import os, importlib, imp
import classes



# the FileLoader is an 'invisible' object that handles reading information from external files, and loading them into the
# game in a format that the game will understand
class FileLoader():
    # a FileLoader has no initial attributes to set up
    def __init__(self):
        pass



    # create Avatar objects representing mechs based on information in an external text file.
    def loadFromFile(self, fileLocation, teamName, console):
        '''
        creates avatar objects based on file definitions
        '''
        # create a list to store finished Avatars
        avatarList = []

        # open the external file, and read the information from it
        file = open(fileLocation, 'r')
        fileData = file.readlines()

        # strip out blank lines from the file's information
        for entry in fileData:
            if entry == '# \n' or entry == '#\n':
                fileData.remove(entry)

        # locate lines that we don't want to actually include as data (comments, blanks, etc)
        indexList = []
        for entry in fileData:
            if entry.startswith('#') or entry.startswith(' ') or entry.startswith('\n'):
                index =  fileData.index(entry)
                indexList.append(index)

        # strip non-data lines from the information gleaned from the file
        i = 0
        for num in indexList:
            locator = num + i
            if locator < 0:
                locator = 0
            del fileData[locator]
            i -= 1

        # if there are more than 6 mechs defined for the team, ignore every mech after the 6th one.
        if len(fileData) > 6:
            fileData = fileData[:6]

        # build a new Avatar for each entry in the information from the file
        for entry in fileData:
            newAvatar = classes.Avatar(entry, teamName, console)
            avatarList.append(newAvatar)

        # we got what we needed from the file, so we close it
        file.close()

        # return the Avatars created to represent the data in the file
        return avatarList



    # loads the weapons into the world, so that mechs can have them equipped, based on data in an external text file
    def loadWeaponModules(self, directory):
        '''
        loads all of the weapons defined by the weapon modules into the world
        '''

        # make a list of all the weapon definitions in the weapon directory
        fileList = os.listdir(directory)

        # create a place to store Weapon objects that are created
        weaponDict = {}

        # for each weapon definition file, strip the file extension
        for weapon in fileList:
            if weapon.endswith('.py'):
                weapon = weapon[:-3]
            elif weapon.endswith('.pyc'):
                weapon = weapon[:-4]

            # pull the weapon definition file in, and run it, creating a new weapon in the game world
            weaponFile = imp.find_module(weapon, [directory])
            weaponModule = imp.load_module(weapon, weaponFile[0], weaponFile[1], ('.py', 'r', imp.PY_SOURCE) )

            # add the weapon to our list of created Weapon objects
            weaponDict[weapon] = weaponModule

        # return the list of created weapon objects
        return weaponDict


    def loadPixelArrays(self, directory):
        '''
        loads all of the functions used for creating pixelArrays for player images
        '''

        # make a list of all the pixelArray definitions in the pixelArray directory
        fileList = os.listdir(directory)

        # create a place to store pixelArray objects that are created
        pixelArrayDict = {}

        # for each pixelArray definition, strip the file extension
        for pixelArray in fileList:
            if pixelArray.endswith('.py'):
                pixelArray = pixelArray[:-3]
            elif pixelArray.endswith('.pyc'):
                pixelArray = pixelArray[:-4]

            # pull the pixelArray function in and run it, creating a new image
            pixelArrayFile = imp.find_module(pixelArray, [directory])
            pixelArrayModule = imp.load_module(pixelArray, pixelArrayFile[0], pixelArrayFile[1], ('.py', 'r', imp.PY_SOURCE) )

            # add the pixelArray to our list of created pixelArrays
            pixelArrayDict[pixelArray] = pixelArrayModule

        # return the list of created pixelArrays
        return pixelArrayDict


