from paraview import servermanager
from paraview.servermanager import CreateRenderView
from vtkRenderingPython import (
    vtkRenderWindowInteractor, vtkInteractorStyleSwitch)

from simphony_paraview.core.api import loaded_in_paraview
from simphony_paraview.core.fixes import CreateRepresentation


def show(cuds, testing=None):
    """ Show the cuds objects using the default visualisation.

     Parameters
     ----------
     cuds : {ABCLattice, ABCMesh, ABCParticles}
         A top level cuds object (e.g. a mesh). The method will detect
         the type of object and create the appropriate visualisation.
     testing : callable(obj, event)
         A callable object that accepts an the interactor object and a
         time event. The callable will be executed after 1000 msec.
         This is commonly used for testing. Default value is None

    """
    with loaded_in_paraview(cuds) as source:

        # XXX Special workaround to avoid segfault on exit as
        # as seen in http://www.paraview.org/Bug/view.php?id=13124

        view = CreateRenderView()
        representation = CreateRepresentation(source, view)

        interactor = vtkRenderWindowInteractor()
        interactor.SetInteractorStyle(vtkInteractorStyleSwitch())
        interactor.SetRenderWindow(view.GetRenderWindow())
        interactor.Initialize()

        if testing is not None:
            timerid = interactor.CreateOneShotTimer(1000)
            handler = Handler(testing, timerid)
            interactor.AddObserver('TimerEvent', handler)
        try:
            view.ResetCamera()
            view.StillRender()
            interactor.Start()
        finally:
            interactor.RemoveAllObservers()


class Handler(object):

    def __init__(self, callback, timerid):
        self.callback = callback
        self.timerid = timerid

    def __call__(self, obj, event):
        self.callback(obj, event)
        obj.DestroyTimer(self.timerid)

@@ -1,20 +1,62 @@
+from paraview.simple import Glyph, Sphere%0A
 from paraview import
@@ -211,16 +211,76 @@
 eSwitch)
+%0Afrom simphony.cuds import ABCMesh, ABCLattice, ABCParticles
 %0A%0Afrom s
@@ -312,16 +312,22 @@
  import 
+(%0A    
 loaded_i
@@ -336,16 +336,45 @@
 paraview
+, typical_distance, set_data)
 %0Afrom si
@@ -444,16 +444,29 @@
 ow(cuds,
+ select=None,
  testing
@@ -1230,58 +1230,1010 @@
     
-representation = CreateRepresentation(source, view
+view.ResetCamera()%0A        camera = view.GetActiveCamera()%0A        camera.Elevation(45)%0A%0A        representation = CreateRepresentation(source, view)%0A%0A        if isinstance(cuds, ABCLattice):%0A            representation.Representation = %22Points%22%0A            if select is not None:%0A                set_data(representation, source, select)%0A        elif isinstance(cuds, ABCParticles):%0A            sphere = Sphere(Radius=typical_distance(source))%0A            glyphs = Glyph(Input=source, ScaleMode='off', GlyphType=sphere)%0A            glyph_representation = CreateRepresentation(glyphs, view)%0A            if select is not None:%0A                if select%5B1%5D == 'particles':%0A                    set_data(glyph_representation, source, select)%0A                else:%0A                    set_data(representation, source, select)%0A        elif isinstance(cuds, ABCMesh):%0A            representation.Representation = %22Surface%22%0A            if select is not None:%0A                set_data(representation, source, select
 )%0A%0A 
@@ -2651,39 +2651,8 @@
 ry:%0A
-            view.ResetCamera()%0A
     

update the show function to provide custom visualization for each cuds container type