var GestureController, config,
  bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

config = window.config;

GestureController = (function() {
  function GestureController() {
    this.parseGestures = bind(this.parseGestures, this);
    this.getActiveSigns = bind(this.getActiveSigns, this);
    this.assertSign = bind(this.assertSign, this);
    this.updateRecipeRecord = bind(this.updateRecipeRecord, this);
    this.updateSignRecord = bind(this.updateSignRecord, this);
    this.wipeRecord = bind(this.wipeRecord, this);
    this.resetSignRecord = bind(this.resetSignRecord, this);
    var recipe, recipeName, ref, ref1, sign, signName, state;
    state = {};
    state.lastTimestamp = 0;
    state.currentTimestamp = 0;
    state.signRecord = {};
    state.recipeRecord = {};
    state.activeSigns = [];
    state.timeout = window.config.timeout;
    ref = window.config.signs;
    for (signName in ref) {
      sign = ref[signName];
      sign.name = signName;
      sign.timeVisible = 0;
      sign.status = 'inactive';
      state.signRecord[signName] = sign;
    }
    ref1 = window.config.recipes;
    for (recipeName in ref1) {
      recipe = ref1[recipeName];
      recipe.name = recipeName;
      recipe.timeVisible = 0;
      recipe.signIndex = 0;
      state.recipeRecord[recipeName] = recipe;
    }
    this.state = state;
    this.currentFrame = {};
    window.gestureController = this;
  }

  GestureController.prototype.resetSignRecord = function(sign) {
    var data;
    console.log("Reset sign " + sign);
    data = this.state.signRecord[sign];
    data.status = 'inactive';
    return data.timeVisible = 0;
  };

  GestureController.prototype.wipeRecord = function() {
    var manager, recipe, results, sign;
    console.log("Wiping record..");
    manager = window.actionHero;
    for (sign in this.state.signRecord) {
      this.resetSignRecord(sign);
    }
    results = [];
    for (recipe in this.state.recipeRecord) {
      manager.tearDownRecipe(recipe);
      results.push(this.state.recipeRecord[recipe].signIndex = 0);
    }
    return results;
  };

  GestureController.prototype.updateSignRecord = function(sign) {
    var data, oldStatus;
    console.log("Update sign " + sign);
    data = this.state.signRecord[sign];
    oldStatus = data.status;
    if (this.assertSign(data, this.state.currentFrame)) {
      if (oldStatus !== 'inactive') {
        data.timeVisible += this.state.currentTimestamp - this.state.lastTimestamp;
      }
      if (!data.minTime || data.minTime < data.timeVisible) {
        return data.status = 'active';
      } else {
        return data.status = 'pending';
      }
    } else {
      return this.resetSignRecord(sign);
    }
  };

  GestureController.prototype.updateRecipeRecord = function(recipe) {
    var data, manager, oldIndex, secondaryIndex, secondarySign, sign;
    console.log("Update recipe " + recipe);
    data = this.state.recipeRecord[recipe];
    oldIndex = data.signIndex;
    sign = data.signs[oldIndex];
    if (indexOf.call(this.state.activeSigns, sign) >= 0) {
      data.signIndex += 1;
    } else if (oldIndex > 0) {
      secondaryIndex = oldIndex - 1;
      secondarySign = data.signs[secondaryIndex];
      if (indexOf.call(this.state.activeSigns, secondarySign) >= 0) {
        data.signIndex = oldIndex;
      } else {
        data.signIndex = 0;
      }
    } else {
      data.signIndex = 0;
    }
    manager = window.actionHero;
    if (data.signIndex === data.signs.length) {
      return manager.activateRecipe(data.name);
    } else {
      return manager.tearDownRecipe(data.name);
    }
  };

  GestureController.prototype.assertSign = function(sign, frameData) {
    var extendedFingers, grabStrength, handModel, i, len, pinchStrength, pincher, ref, sign_ok;
    sign_ok = true;
    ref = frameData.hands;
    for (i = 0, len = ref.length; i < len; i++) {
      handModel = ref[i];
      if (sign.grab) {
        grabStrength = handModel.grabStrength;
        if (sign.grab.min) {
          if (grabStrength < sign.grab.min) {
            sign_ok = false;
          }
        }
        if (sign.grab.max) {
          if (grabStrength > sign.grab.max) {
            sign_ok = false;
          }
        }
      }
      if (sign.pinch) {
        pinchStrength = handModel.pinchStrength;
        pincher = handModel.pinchingFinger;
        if (sign.pinch.pincher) {
          if (sign.pinch.pincher !== pincher) {
            sign_ok = false;
          }
        }
        if (sign.pinch.min) {
          if (pinchStrength < sign.pinch.min) {
            sign_ok = false;
          }
        }
        if (sign.pinch.max) {
          if (pinchStrength > sign.pinch.max) {
            sign_ok = false;
          }
        }
      }
      if (sign.extendedFingers) {
        extendedFingers = sign.extendedFingers;
        if (extendedFingers.indexFinger === !void 0) {
          if (extendedFingers.indexFinger !== handModel.extendedFingers.indexFinger) {
            sign_ok = false;
          }
        }
        if (extendedFingers.middleFinger === !void 0) {
          if (extendedFingers.middleFinger !== handModel.extendedFingers.middleFinger) {
            sign_ok = false;
          }
        }
        if (extendedFingers.ringFinger === !void 0) {
          if (extendedFingers.ringFinger !== handModel.extendedFingers.ringFinger) {
            sign_ok = false;
          }
        }
        if (extendedFingers.pinky === !void 0) {
          if (extendedFingers.pinky !== handModel.extendedFingers.ringFinger) {
            sign_ok = false;
          }
        }
        if (extendedFingers.thumb === !void 0) {
          if (extendedFingers.thumb !== handModel.extendedFingers.thumb) {
            sign_ok = false;
          }
        }
      }
    }
    return sign_ok;
  };

  GestureController.prototype.getActiveSigns = function() {
    var activeSigns, data, ref, sign;
    activeSigns = [];
    ref = this.state.signRecord;
    for (sign in ref) {
      data = ref[sign];
      if (data.status === 'active') {
        activeSigns.push(sign);
      }
    }
    return activeSigns;
  };

  GestureController.prototype.parseGestures = function(model) {
    var callback, delay, manager, recipe, sign;
    clearTimeout(this.timerID);
    manager = window.actionHero;
    manager.position = model.hands[0].position;
    this.state.lastTimestamp = this.state.currentTimestamp;
    this.state.currentTimestamp = model.timestamp;
    this.state.currentFrame = model;
    for (sign in this.state.signRecord) {
      this.updateSignRecord(sign);
    }
    this.state.activeSigns = this.getActiveSigns();
    for (recipe in this.state.recipeRecord) {
      this.updateRecipeRecord(recipe);
    }
    callback = (function(_this) {
      return function() {
        return _this.wipeRecord();
      };
    })(this);
    delay = window.config.timeout;
    console.log("Callback", callback);
    console.log("Delay: ", delay);
    return this.timerID = setTimeout(callback, delay);
  };

  return GestureController;

})();

//# sourceMappingURL=maps/GestureController.js.map
