#!/usr/bin/python

import os, sys, optparse
import clutter
import gtk
import gst
import cluttergst
import gobject

gobject.threads_init()

class Awesome:
    def OnDynamicPad(self, dbin, pad, islast):
        pad.link(self.colorspace.get_pad("sink"))

    def __init__(self, video_file):
        # Controls depth
        self.foward = True

        def bus_handler(unused_bus, message):
            if message.type == gst.MESSAGE_ERROR:
                pass
            return gst.BUS_PASS

        self.stage = clutter.stage_get_default()
        self.stage.set_color(clutter.color_parse("#000000"))
        self.stage.set_title("Awesome")
        self.stage.connect('destroy', clutter.main_quit)
        self.stage.connect('key-press-event', clutter.main_quit)

        # Creates a gstreamer texture
        self.video_texture = clutter.Texture()

        self.pipeline = gst.Pipeline()
        self.bus = self.pipeline.get_bus()
        self.bus.add_signal_watch()
        self.bus.connect("message", bus_handler)

        if video_file == 'none':
            self.src = gst.element_factory_make("v4l2src", "src")
        else:
            self.src = gst.element_factory_make("filesrc", "filesrc")
            self.src.set_property("location", video_file)
        self.decode = gst.element_factory_make("decodebin", "decode")
        self.colorspace = gst.element_factory_make("ffmpegcolorspace", "colorspace")

        # Attaches the output texture to gstreamer sink
        self.sink = cluttergst.VideoSink(self.video_texture)

        self.pipeline.add(self.src, self.decode, self.colorspace, self.sink)

        self.decode.connect("new-decoded-pad", self.OnDynamicPad)
        gst.element_link_many(self.src, self.decode)
        gst.element_link_many(self.colorspace, self.sink)
        self.pipeline.set_state(gst.STATE_PLAYING)

        self.timeline = clutter.Timeline(250,50)
        self.timeline.set_loop(True)
        self.timeline.connect("completed", self.timeline_completed)

        alpha = clutter.Alpha(self.timeline, clutter.sine_half_func)
        self.behaviour_scale = clutter.BehaviourScale(alpha=alpha, \
            x_scale_start=1.0, x_scale_end=1.5, \
            y_scale_start=1.0, y_scale_end=1.6)
        self.behaviour_rotate_y = clutter.BehaviourRotate(alpha=alpha, \
            angle_start=30.0, angle_end=5.0, axis=clutter.Y_AXIS, \
            direction = "ccw")
        self.behaviour_rotate_z = clutter.BehaviourRotate(alpha=alpha, \
            angle_start=3.0, angle_end=0.0, axis=clutter.Z_AXIS, \
            direction = "ccw")

        self.behaviour_scale.apply(self.video_texture)
        self.behaviour_rotate_y.apply(self.video_texture) 
        self.behaviour_rotate_z.apply(self.video_texture)

        self.stage.add(self.video_texture)
        self.stage.set_size(800, 600)
        self.stage.show_all()

        self.timeline.start()
        clutter.main()

  # Change the direction 
    def timeline_completed(self,data):
        if self.foward:
            self.x_scale_start = 2.0
            self.x_scale_end = 1.0
            self.y_scale_start = 2.2
            self.y_scale_end = 1.0
            self.foward = False
        else:
            self.x_scale_start = 2.0
            self.x_scale_end = 1.0
            self.y_scale_start = 2.2
            self.y_scale_end = 1.0
            self.foward = True

        self.behaviour_scale.set_property("x_scale_start",self.x_scale_start)
        self.behaviour_scale.set_property("x_scale_end",self.x_scale_end)
        self.behaviour_scale.set_property("y_scale_start",self.y_scale_start)
        self.behaviour_scale.set_property("y_scale_end",self.y_scale_end)
        self.timeline.rewind()
        self.timeline.start()


if __name__ == "__main__":
    usage = """ pudovkin -i [file] """

    parser = optparse.OptionParser(usage=usage)
    parser.add_option("-i", "--input", action="store", type="string", \
        dest="video_file", help="Input video file", default="")
    (options, args) = parser.parse_args()

    if options.video_file:
        video_file = options.video_file
        if not os.path.exists(options.video_file):
            print "%s was not found" % options.video_file
            os._exit(2)
    else:
        video_file = 'none'

    awesome = Awesome(video_file)

