"""namespace config, 'cause the c++ side doesn't do it too nicely"""

from __main__ import _pythonscriptmodule
from __main__ import _naali
import rexviewer as r #the old module is still used , while porting away from it
#from _naali import *

#XXX do we actually want these style changes,
#or is it better to just call the slots directly
# module methods
runjs = _pythonscriptmodule.RunJavascriptString

def getScene(name):
    return _pythonscriptmodule.GetScene(name)
    
def getDefaultScene():
    #XXX should use framework GetDefaultWorldScene
    return getScene("World")
    
def createEntity(comptypes = []):
    s = getDefaultScene()
    #newid = s.NextFreeId()
    ent = s.CreateEntityRaw(0, comptypes) #0 apparently means it assigns NextFreeId
    return ent #Entity(ent)

def createMeshEntity(meshname, raycastprio=1):
    ent = r.createEntity(meshname, raycastprio)
    return getEntity(ent.id)
    #XXX ugly workaround
    #oldpyent = r.getEntity(ent.id)
    #oldpyent.createComponent("EC_Mesh") #sets placeable too

    #XXX wasn't possible yet. lead into research about adding QPointer support to PythonQt internals etc
    #ent = naali.createEntity(["EC_Placeable", "EC_Mesh"])
    #ent.mesh.SetPlaceable(ent.placeable) #wants a boost shared_ptr, which we don't have :/
    #ent.mesh.SetMesh(meshname)

def removeEntity(entity):
    r.removeEntity(entity.Id)

def createInputContext(name, priority = 100):
    return _pythonscriptmodule.CreateInputContext(name, priority)

# module variables
renderer = _pythonscriptmodule.GetRenderer()
worldlogic = _pythonscriptmodule.GetWorldLogic()
inputcontext = _pythonscriptmodule.GetInputContext()
mediaplayerservice = _pythonscriptmodule.GetMediaPlayerService()
frame = _naali.GetFrame()
console = _naali.Console()
input = _naali.GetInput()
audio = _naali.Audio()
ui = _naali.UiService() #the UI core object does not implement the old uiservice stuff yet
framework = _naali

#not used in this branch, which tries to avoid extra Entity wrapper alltogether
# class Entity:
#     compnames = {
#         'placeable': 'EC_Placeable',
#         'mesh': 'EC_Mesh',
#         'camera': 'EC_OgreCamera',
#         'animationcontroller': 'EC_AnimationController',
#         'highlight': 'EC_Highlight',
#         'touchable': 'EC_Touchable',
#         'soundruler': 'EC_SoundRuler',
#         'netpos': 'EC_NetworkPosition',
#        }   
#
#     def __init__(self, qent):
#         self.id = qent.Id

#         """The Scene::Entity exposed by Qt mechanism. Public, ok to use directly as well."""
#         self.qent = qent


#     def getComponent(self, typename, idname=None):
#         """To allow getting components for which there is no shortcut in py Entity helper"""

#         if idname is None: #the right qt slot is found based on the amount of params given
#             return self.qent.GetComponentRaw(typename)
#         else:
#             return self.qent.GetComponentRaw(typename, idname)

#     def getDynamicComponent(self, idname):
#         return self.getComponent("EC_DynamicComponent", idname)
        
#     def __getattr__(self, name):
#         if name in Entity.compnames:
#             fullname = Entity.compnames[name]
#             comp = self.qent.GetComponentRaw(fullname)
#             if comp is None: 
#                 raise AttributeError, "The entity does not have a %s component" % fullname
#             return comp
#         raise AttributeError

#     def createComponent(self, typename):
#         oldpyent = r.getEntity(self.id)
#         oldpyent.createComponent(typename)

#     def createComponent(self, typename):
#         oldpyent = r.getEntity(self.id)
#         oldpyent.createComponent(typename)

def createComponent(ent, typename):
    #XXX ugly workaround
    oldpyent = r.getEntity(ent.Id)
    oldpyent.createComponent(typename)

# Returns EntityAction pointer by the name
def action(self, name):
    return self.qent.Action(name)
    
def getEntity(entid):
    qent = getScene("World").GetEntityRaw(entid)
    if qent is None:
        raise ValueError, "No entity with id %d" % entid
    #return Entity(qent)
    return qent

#helper funcs to hide api indirections/inconsistenties that were hard to fix,
#-- these allow to remove the old corresponding hand written c funcs in pythonscriptmodule.cpp
#.. and now am working towards removal of PyEntity.cpp too
def _getAsPyEntity(qentget, errmsg):
    qent = qentget()
    if qent is not None:
        #print qent.Id
        #pyent = Entity(qent)
        #print pyent, pyent.id
        #return pyent
        return qent
    else:
        raise ValueError, errmsg

def getUserAvatar():
    return _getAsPyEntity(worldlogic.GetUserAvatarEntityRaw, "No avatar. No scene, not logged in?")
        
def getCamera():
    return _getAsPyEntity(worldlogic.GetCameraEntityRaw, "No default camera. No scene?")

@@ -640,36 +640,8 @@
 e()%0A
-    #newid = s.NextFreeId()%0A
     
@@ -738,21 +738,8 @@
  ent
- #Entity(ent)
 %0A%0Ade
@@ -794,16 +794,17 @@
  ent = r
+ 
 .createE
@@ -831,16 +831,77 @@
 astprio)
+ #XXX: this returns a PyEntity still, get rid of this somehow
 %0A    ret
@@ -1330,24 +1330,63 @@
 (meshname)%0A%0A
+#XXX check how to do with SceneManager%0A
 def removeEn
@@ -1991,1911 +1991,8 @@
 li%0A%0A
-#not used in this branch, which tries to avoid extra Entity wrapper alltogether%0A# class Entity:%0A#     compnames = %7B%0A#         'placeable': 'EC_Placeable',%0A#         'mesh': 'EC_Mesh',%0A#         'camera': 'EC_OgreCamera',%0A#         'animationcontroller': 'EC_AnimationController',%0A#         'highlight': 'EC_Highlight',%0A#         'touchable': 'EC_Touchable',%0A#         'soundruler': 'EC_SoundRuler',%0A#         'netpos': 'EC_NetworkPosition',%0A#        %7D   %0A#%0A#     def __init__(self, qent):%0A#         self.id = qent.Id%0A%0A#         %22%22%22The Scene::Entity exposed by Qt mechanism. Public, ok to use directly as well.%22%22%22%0A#         self.qent = qent%0A%0A%0A#     def getComponent(self, typename, idname=None):%0A#         %22%22%22To allow getting components for which there is no shortcut in py Entity helper%22%22%22%0A%0A#         if idname is None: #the right qt slot is found based on the amount of params given%0A#             return self.qent.GetComponentRaw(typename)%0A#         else:%0A#             return self.qent.GetComponentRaw(typename, idname)%0A%0A#     def getDynamicComponent(self, idname):%0A#         return self.getComponent(%22EC_DynamicComponent%22, idname)%0A        %0A#     def __getattr__(self, name):%0A#         if name in Entity.compnames:%0A#             fullname = Entity.compnames%5Bname%5D%0A#             comp = self.qent.GetComponentRaw(fullname)%0A#             if comp is None: %0A#                 raise AttributeError, %22The entity does not have a %25s component%22 %25 fullname%0A#             return comp%0A#         raise AttributeError%0A%0A#     def createComponent(self, typename):%0A#         oldpyent = r.getEntity(self.id)%0A#         oldpyent.createComponent(typename)%0A%0A#     def createComponent(self, typename):%0A#         oldpyent = r.getEntity(self.id)%0A#         oldpyent.createComponent(typename)%0A%0Adef createComponent(ent, typename):%0A    #XXX ugly workaround%0A    oldpyent = r.getEntity(ent.Id)%0A    oldpyent.createComponent(typename)%0A%0A
 # Re
@@ -2246,33 +2246,8 @@
 tid%0A
-    #return Entity(qent)%0A
     
@@ -2436,67 +2436,8 @@
 cpp%0A
-#.. and now am working towards removal of PyEntity.cpp too%0A
 def 
@@ -2542,91 +2542,8 @@
 .Id%0A
-        #pyent = Entity(qent)%0A        #print pyent, pyent.id%0A        #return pyent%0A
     

remove commented out Entity wrapper from naali.py and almost all PyEntity using things