"""Tutorial 2 - Python and agxOSGViewer

Demonstrates how to.
  * create a python script which can be loaded both with python3 and agxOSGViewer.
  * How to use arguments when python3 is used.

The file consists of two scenes. buildScene1 and buildScene2.
With python3 you can switch between the two scenes with the keys '1' and '2'
With agxOSGViewer the def 'buildScene' will be called, which in turn will call 'buildScene1',:
hence 'buildScene2' is not available from agxOSGViewer

agxOSGViewer will always call 'buildScene()' when it loads a .agxPy or .py file.
python3 on the other hand knows nothing about graphics, so it has to be created explicitly
(by creating an agxOSG::ExampleApplication).
"""

# AGX Dynamics imports
import agx
import agxPython
import agxCollide
import agxOSG
import agxIO

# Python imports
import sys

from tutorial_utils import createHelpText


def initCamera(app):
    eye = agx.Vec3(2.44613, -6.74518, 3.8733)
    center = agx.Vec3(2.16801, -5.89586, 3.42464)
    up = agx.Vec3(-0.139624, 0.426388, 0.893699)

    app.setCameraHome(eye, center, up)


# Entry point if script is launched using agxOSGViewer
def buildScene1():
    # sim - A pointer to an instance of a agxSDK::Simulation
    # app - A pointer to an instance of a agxOSG::ExampleApplication
    # root - A pointer to an instance of agxOSG::Group
    sim = agxPython.getContext().environment.getSimulation()
    app = agxPython.getContext().environment.getApplication()
    root = agxPython.getContext().environment.getSceneRoot()

    # Add text to screen
    createHelpText(sim, app)

    print("buildScene1 - boxes")

    # Create floor geometry
    floor = agxCollide.Geometry(agxCollide.Box(agx.Vec3(1, 1, 0.1)))
    floor.setPosition(0, 0, -0.1)
    sim.add(floor)
    agxOSG.createVisual(floor, root)

    # If it was started with python3 we could get number of objects
    # through the app.numberOfObjects value set in the init() method below
    try:
        n = app.numberOfObjects
    except AttributeError:
        n = 1

    # Create boxes
    for i in range(0, n + 1):
        b = agx.RigidBody()
        b.add(agxCollide.Geometry(agxCollide.Box(agx.Vec3(0.2, 0.2, 0.2))))
        b.setPosition(0, 0, 0.2 + i * 0.2 * 2 + 1)
        sim.add(b)
        agxOSG.createVisual(b, root)

    initCamera(app)


########################################
# 	Create another scene with bodies and geometries
# 	sim - A pointer to an instance of a agxSDK..Simulation
# 	app - A pointer to an instance of a agxOSG..ExampleApplication
#   root - A pointer to an instance of a agxOSG::Group
########################################
def buildScene2():
    # Access some important variables for building a simulation
    sim = agxPython.getContext().environment.getSimulation()
    app = agxPython.getContext().environment.getApplication()
    root = agxPython.getContext().environment.getSceneRoot()

    print("buildScene2 -spheres")

    floor = agxCollide.Geometry(agxCollide.Box(agx.Vec3(1, 1, 0.1)))
    floor.setPosition(0, 0, -0.1)
    sim.add(floor)
    agxOSG.createVisual(floor, root)

    # If it was started with python3 we could get number of objects
    # through the app.numberOfObjects value set in the init() method below
    try:
        n = app.numberOfObjects
    except AttributeError:
        n = 1

    # Create spheres
    for i in range(0, n + 1):
        b = agx.RigidBody()
        b.add(agxCollide.Geometry(agxCollide.Sphere(0.2)))
        b.setPosition(0, 0, 0.2 + i * 0.2 * 2 + 1)
        sim.add(b)
        agxOSG.createVisual(b, root)

    initCamera(app)


########################################
# 	agxOSGViewer's 'main' function. This function is called by agxOSGViewer when it is given a .agxPy file on the command-line.
########################################
def buildScene():
    # We just call the buildScene1 to create a scene
    app = agxPython.getContext().environment.getApplication()
    app.addScene(app.getArguments().getArgumentName(1), "buildScene1", ord("1"), True)
    app.addScene(app.getArguments().getArgumentName(1), "buildScene2", ord("2"), True)
    buildScene1()


# Main function to be loaded when script is executed using python3
def main(args):
    # Create an application with graphics etc.
    app = agxOSG.ExampleApplication()

    # Create a command line parser. sys.executable will point to python executable
    # in this case, because getArgumentName(0) needs to match the C argv[0] which
    # is the name of the program running
    argParser = agxIO.ArgumentParser([sys.executable] + args)

    app.addScene(argParser.getArgumentName(1), "buildScene", ord("1"), True)

    # Call the init method of ExampleApplication
    # It will setup the viewer, windows etc.
    if app.init(argParser):
        app.run()
    else:
        print("An error occurred while initializing ExampleApplication.")


# Entry point when this script is loaded with python
if agxPython.getContext() is None:
    init = agx.AutoInit()
    main(sys.argv)
