from panda3d.core import getModelPath, Filename, ConfigVariableFilename, DSearchPath, ExecutionEnvironment, PandaSystem

# This file defines a number of standard "packages" that correspond to
# a Panda3D distribution.  These packages are built by passing this
# file to the ppackage utility, either as a packaged application, or
# as the module direct.p3d.ppackage.

# The packages in this file define the "Core API".  This is the second
# installed piece of the three-part plugin system (the plugin, the
# core API, Panda3D).

# These packages are downloaded directly by the plugin, from the host
# specified by the value of PANDA_PACKAGE_HOST_URL compiled into the
# plugin.  Thus, these packages are inextricably tied to the
# particular plugin they have been built with.  They do not have to be
# present on a server that hosts a version of Panda3D for download,
# just on the server that hosts the plugin itself.  These packages do
# not need to be updated with each new version of Panda3D.

# Also see panda3d.pdef.

class coreapi(solo):
    # The special "coreapi" package.  As a "solo", this is just a
    # single .dll (or dylib, or whatever).
    setVer(PandaSystem.getP3dCoreapiVersionString())
    file('p3d_plugin.dll')

class images(package):
    # The default startup images are stored as their own package.
    names = ['download', 'failed', 'play_click', 'play_ready', 'play_rollover',
             'auth_click', 'auth_ready', 'auth_rollover']
    configDict = {}

    # Construct a search path to look for the images.
    search = DSearchPath()

    # First on the path: an explicit $PLUGIN_IMAGES env var.
    if ExecutionEnvironment.hasEnvironmentVariable('PLUGIN_IMAGES'):
        search.appendDirectory(Filename.expandFrom('$PLUGIN_IMAGES'))

    # Next on the path: the models/plugin_images directory within the
    # current directory.
    search.appendDirectory('models/plugin_images')

    # Finally on the path: models/plugin_images within the model
    # search path.
    for dir in getModelPath().getDirectories():
        search.appendDirectory(Filename(dir, 'plugin_images'))

    for name in names:
        # Look for a png image first.
        basename = '%s.png' % (name)
        filename = Filename(basename)
        found = filename.resolveFilename(search)
        if not found:
            # Then try a jpeg image.
            basename = '%s.jpg' % (name)
            filename = Filename(basename)
            found = filename.resolveFilename(search)

        if found:
            # Add the image file to the package
            file(filename, newName = basename, extract = True)

            # And set the config variable to reference it.
            token = '%s_img' % (name)
            configDict[token] = basename
        else:
            print("Could not locate %s" % (filename))

    # Also make a few special cases.  We use the same default image
    # for many of the states.
    download = configDict.get('download_img', None)
    if download:
        configDict['ready_img'] = download
        configDict['unauth_img'] = download
        configDict['launch_img'] = download
        configDict['active_img'] = download

    config(**configDict)

class certlist(package):
    # This package holds any certificates that should be considered
    # pre-approved by the plugin vendor.  If you build and host your
    # own version of the Panda3D plugin, you can add your own
    # certificates to this list.  The certificates in this package
    # will be assumed to have been approved by the user when he/she
    # installed the plugin.

    # They should be PEM-encoded and their filenames must end in the
    # ".pem" or ".crt" extension, and they should be added with the
    # extract = True flag so they will be extracted to disk.
    pass


class p3dcert(package):
    # This special application, used to pop up a dialog to prompt the
    # user to accept or deny unknown applications, is its own package.
    config(display_name = "Authorization Dialog")

    if platform.startswith('osx'):
        # On Mac, we package up a P3DCert.app bundle.  This includes
        # specifications in the plist file to avoid creating a dock
        # icon and stuff.

        # Find p3dcert.plist in the direct source tree.
        import direct
        plist = Filename(direct.__path__[0], 'plugin/p3dcert.plist')
        makeBundle('P3DCert.app', plist, executable = 'p3dcert')

    else:
        # Anywhere else, we just ship the executable file p3dcert.exe.
        file('p3dcert.exe', required = True)


class p3dembed(package):
    # This contains the p3dembed executable, that is used by
    # pdeploy to generate a self-extracting .p3d executable.

    config(platform_specific = True)
    file('p3dembed.exe', required = True)

    if platform.startswith('win'):
        file('p3dembedw.exe', required = True)
