import cvisual

class light( cvisual.light):
    def __init__( self, pos, _other=None, **keywords):
        if _other:
            cvisual.light.__init__(self, _other)
            self.pos = cvisual.vector(pos)
            self.__display = keywords.get( 'display', _other.display)
        else:
            cvisual.light.__init__(self, pos)
            self.__display = keywords.get( 'display',
                cvisual.display.get_selected())
        if keywords.has_key('display'):
            del keywords['display']
        for key, value in keywords.iteritems():
            self.__setattr__(key, value)
        if self.__display:
            self.__display.add_light(self)

    def __copy__(self, pos=None, **keywords):
        if not pos:
            pos = self.pos
        return light( _other=self, pos=pos, **keywords)

    def _get_display(self):
        return self.__display

    def _set_display(self, display):
        if display != self.__display:
            if display and not self.__display:
                display.add_light(self)
            elif not display and self.__display:
                self.__display.remove_light(self)
            else:
                self.__display.remove_light(self)
                display.add_light(self)
            self.__display = display
    display = property( _get_display, _set_display)
    # TODO: set_color: affects diffuse_color and specular_color
    # get_color returns just diffuse_color
    color = cvisual.light.diffuse_color


# Code to provide special initialization for a display object, and overloaded
# properties.
class display( cvisual.display):
    def __init__( self, **keywords):
        cvisual.display.__init__(self)
        keys = keywords.keys()
        keys.sort()
        haslights = False
        for kw in keys:
            if (kw == 'lights'):
                haslights = True
            self.__setattr__(kw, keywords[kw])
        self.ambient = 0.2
        if (not haslights):
            light0 = light( pos=cvisual.vector(0.22, 0.44, 0.88).norm(), color=0.8,
                    local=0, display=self)
            light1 = light( pos=cvisual.vector(-0.88, -0.22, -.44).norm(), color=0.3,
                    local=0, display=self)
        self.select()
    def select(self):
            cvisual.display.set_selected(self)
    range = property( cvisual.display._get_range, cvisual.display._set_range)
    
    def _set_lights(self, n_lights):
        old_lights = self._get_lights()
        for lt in old_lights:
            lt.display = None
        if type(n_lights) is tuple: # compatibilty with old "scene.lights = (1,0,0)"
            n_lights = [n_lights]
        for lt in list(n_lights):
            lt = cvisual.vector(lt)
            if isinstance( lt, cvisual.vector):
                n_light = light( pos=lt.norm(), color=lt.mag, local=0, display=self)
            elif isinstance( lt, cvisual.light):
                lt.display = self
            else:
                raise TypeError(lt, "is not a light!")
    lights = property( cvisual.display._get_lights, _set_lights, None)
