import os
has_x = ('DISPLAY' in os.environ.keys()) and (os.environ['DISPLAY'] is not None)

import numpy as np

from cellcomplex.property_topomesh.example_topomesh import vtk_ellipsoid_topomesh
from cellcomplex.property_topomesh.extraction import cut_surface_topomesh
from cellcomplex.property_topomesh.utils.matplotlib_tools import mpl_draw_topomesh, mpl_draw_incidence_graph

from cellcomplex.property_topomesh.visualization.vtk_actor_topomesh import VtkActorTopomesh
from cellcomplex.property_topomesh.visualization.vtk_tools import vtk_image_actors

topomesh = vtk_ellipsoid_topomesh(ellipsoid_radius=2,ellipsoid_scales=[1.5,1,1],subdivisions=2)
topomesh = cut_surface_topomesh(topomesh,z_offset=0,below=False)

figure = plt.figure(0)
figure.clf()

if has_x:
    figure.add_subplot(1,2,1)
figure.gca().axis('equal')

mpl_draw_topomesh(topomesh,figure,2,color='g')
mpl_draw_topomesh(topomesh,figure,1,color='b')
mpl_draw_topomesh(topomesh,figure,0,color='m')

if has_x:
    figure.add_subplot(1,2,2)

    actors = []

    face_actor = VtkActorTopomesh(topomesh,2)
    face_actor.update(colormap='YlGn',value_range=(0,0))
    actors += [face_actor.actor]

    edge_actor = VtkActorTopomesh(topomesh,1,line_glyph='tube',glyph_scale=0.01)
    edge_actor.update(colormap='Blues',value_range=(0,0),linewidth=3,opacity=1)
    actors += [edge_actor.actor]

    vertex_actor = VtkActorTopomesh(topomesh,0,glyph_scale=0.05)
    vertex_actor.update(colormap='RdPu',value_range=(0,0),opacity=1)
    actors += [vertex_actor.actor]

    figure.gca().imshow(vtk_image_actors(actors,focal_point=(0,0,1),view_up=(0,1,0)))

    figure.gca().axis('off')

    figure.set_size_inches(10,5)
figure.tight_layout()