var Q = require("q");
var Qimage = require("qimage");
var Qajax = require("qajax");
var GlslTransition = require("glsl-transition");
var GlslTransitions = require("glsl-transitions");

function extend (obj) {
  var source, prop;
  for (var i = 1, length = arguments.length; i < length; i++) {
    source = arguments[i];
    for (prop in source) {
      if (source.hasOwnProperty(prop)) {
        obj[prop] = source[prop];
      }
    }
  }
  return obj;
};

var GLSL_IDENTITY_FROM = "#ifdef GL_ES\nprecision highp float;\n#endif\nuniform sampler2D from, to;\nuniform float progress;\nuniform vec2 resolution;\nvoid main() {vec2 p = gl_FragCoord.xy / resolution.xy;gl_FragColor = texture2D(from, p);}";

function Viewer (json, canvas) {
  this.data = json;
  this.cursor = 0;
  this.canvas = canvas;
  this.T = GlslTransition(this.canvas);
  this.identity = this.T(GLSL_IDENTITY_FROM);
  this.images = {};
  json.timeline.forEach(function (item) {
    var url = item.image;
    this.images[url] = Qimage.anonymously(url);
  }, this);
  this.transitions = {};
  json.timeline.forEach(function (item) {
    var name = item.transitionNext && item.transitionNext.name;
    if (name && !(name in this.transitions) && (name in GlslTransitions)) {
      this.transitions[name] = this.T(GlslTransitions[name]);
    }
  }, this);
  this._preloadAudio();
  console.log(json);
}

Viewer.prototype = {
  start: function () {
    var self = this;
    var data = this.data;
    var item = data.timeline[0];
    this._startAudio();
    return this.images[item.image]
      .then(function (from) {
        return self.identity({ from: from, to: from }, 100)
      })
      .delay(item.duration)
      .then(function () {
        return self.next();
      });
  },
  end: function () {
    this._stopAudio();
  },
  next: function () {
    var self = this;
    var data = this.data;
    var from = this.cursor;
    var endReached = from+1 === data.timeline.length;
    if (endReached && !data.loop) return this.end();
    var to = endReached ? 0 : from + 1;
    this.cursor = to;

    var itemDuration = data.timeline[from].duration;
    var transitionNext = data.timeline[from].transitionNext;
    var transition = transitionNext && transitionNext.name ?
      this.transitions[transitionNext.name] :
      this.identity;
    console.log(transitionNext, transition);
    var transitionDuration = transitionNext && transitionNext.duration || 0;
    var transitionUniforms = transitionNext && transitionNext.uniforms || {};

    console.log(from, "->", to);

    return Q.all([
      this.images[data.timeline[from].image],
      this.images[data.timeline[to].image]
    ])
    .spread(function (fromImage, toImage) {
      return transition(extend({ from: fromImage, to: toImage }, transitionUniforms), transitionDuration);
    })
    .delay(itemDuration)
    .then(function () {
      return self.next();
    });
  },
  _preloadAudio: function () {

  },
  _startAudio: function () {

  },
  _stopAudio: function () {

  }
};

module.exports = Viewer;

@@ -178,16 +178,194 @@
 ons%22);%0A%0A
+function findTransitionByName (name) %7B%0A  for (var i=0; i%3CGlslTransitions.length; ++i) %7B%0A    if (GlslTransitions%5Bi%5D.name === name) %7B%0A      return GlslTransitions%5Bi%5D;%0A    %7D%0A  %7D%0A%7D%0A%0A
 function
@@ -380,16 +380,16 @@
 (obj) %7B%0A
-
   var so
@@ -1382,26 +1382,80 @@
 ons)
- && (name in GlslT
+) %7B%0A      var transitionObject = findTransitionByName(name);%0A      if (t
 rans
@@ -1455,30 +1455,36 @@
  (transition
-s)
+Object
 ) %7B%0A
+  
       this.t
@@ -1506,38 +1506,118 @@
 %5D = 
-this.T(GlslTransitions%5Bname%5D);
+%7B%0A          t: this.T(transitionObject.glsl),%0A          uniforms: transitionObject.uniforms%0A        %7D;%0A      %7D
 %0A   
@@ -2596,24 +2596,29 @@
 ame%5D :%0A     
+ %7B t:
  this.identi
@@ -2623,53 +2623,24 @@
 tity
-;%0A    console.log(transitionNext, transition)
+, uniforms: %7B%7D %7D
 ;%0A  
@@ -2813,22 +2813,18 @@
 log(
-from, %22-%3E%22, to
+transition
 );%0A%0A
@@ -3005,16 +3005,18 @@
 ansition
+.t
 (extend(
@@ -3067,16 +3067,37 @@
 Uniforms
+, transition.uniforms
 ), trans

fix viewer