var ActionController, config, feedback,
  bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

feedback = window.feedback;

config = window.config;

ActionController = (function() {
  function ActionController() {
    this.tearDownRecipes = bind(this.tearDownRecipes, this);
    this.getActiveRecipes = bind(this.getActiveRecipes, this);
    this.tearDownRecipe = bind(this.tearDownRecipe, this);
    this.activateRecipe = bind(this.activateRecipe, this);
    this.executeAction = bind(this.executeAction, this);
    this.mouseButton = bind(this.mouseButton, this);
    this.mouseMove = bind(this.mouseMove, this);
    var name, recipe, ref;
    this.actions = window.config.actions;
    this.recipes = window.config.recipes;
    window.ActionController = this;
    this.robot = require('robotjs');
    this.mouseState = {
      left: "up",
      right: "up"
    };
    this.position = {
      x: void 0,
      y: void 0
    };
    this.keyboardModel = {
      test: false
    };
    this.recipeState = {};
    ref = this.recipes;
    for (name in ref) {
      recipe = ref[name];
      this.recipeState[name] = 'inactive';
    }
  }

  ActionController.prototype.mouseMove = function(position) {
    var moveTo, screenSize;
    screenSize = this.robot.getScreenSize();
    moveTo = {
      x: position.x * screenSize.width,
      y: position.y * screenSize.height
    };
    return this.robot.moveMouse(moveTo.x, moveTo.y);
  };

  ActionController.prototype.mouseButton = function(buttonState, button) {
    feedback = window.feedback;
    if (buttonState === 'up') {
      this.robot.mouseToggle(buttonState, button);
      if (this.mouseState.button !== buttonState) {
        window.feedback.audioNotification('asset/audio/mouseup.ogg');
      }
    } else if (buttonState === 'down') {
      if (this.mouseState.button !== buttonState) {
        this.robot.mouseToggle(buttonState, button);
        window.feedback.audioNotification('asset/audio/mousedown.ogg');
      }
    }
    window.feedback.mouseStatus(button, buttonState);
    return this.mouseState.button = buttonState;
  };

  ActionController.prototype.executeAction = function(action) {
    var button, cmd;
    console.log("Execute action: ", action);
    cmd = this.actions[action];
    if (cmd.type === 'mouse') {
      if (cmd.action === 'hold') {
        button = cmd.target;
        this.mouseButton('down', button);
      }
      if (cmd.action === 'release') {
        button = cmd.target;
        this.mouseButton('up', button);
      }
      if (cmd.action === 'move') {
        this.mouseMove(this.position);
      }
    }
    if (cmd.type === 'keyboardTest') {
      return this.keyboardTest(cmd.action);
    }
  };

  ActionController.prototype.activateRecipe = function(recipeName) {
    var actionName, recipe;
    recipe = this.recipes[recipeName];
    actionName = recipe.action;
    if (recipe.continuous) {
      console.log("activate recipe: ", recipeName);
      return this.executeAction(actionName);
    } else if (this.recipeState[recipeName] === 'inactive') {
      console.log("activate recipe: ", recipeName);
      this.recipeState[recipeName] = 'active';
      this.executeAction(actionName);
      return true;
    } else {
      return false;
    }
  };

  ActionController.prototype.tearDownRecipe = function(recipeName) {
    var actionName, recipe;
    recipe = this.recipes[recipeName];
    actionName = recipe.tearDown;
    if (this.recipeState[recipeName] === 'active') {
      console.log("Tear down: " + recipeName);
      this.executeAction(actionName);
      this.recipeState[recipeName] = 'inactive';
      return true;
    } else {
      return false;
    }
  };

  ActionController.prototype.getActiveRecipes = function(filter) {
    var recipeList, recipeName, recipeState, ref;
    console.log("get Active recipes..");
    recipeList = [];
    ref = this.recipeState;
    for (recipeName in ref) {
      recipeState = ref[recipeName];
      console.log("recipeName: " + recipeName + ", recipeState: " + recipeState);
      if (recipeState === 'active') {
        if (typeof filter !== 'function' || filter(recipeName)) {
          recipeList.push(recipeName);
        }
      }
    }
    return recipeList;
  };

  ActionController.prototype.tearDownRecipes = function(filter) {
    var recipeName, ref, state;
    console.log("Tear down recipes: ", filter);
    ref = this.recipeState;
    for (recipeName in ref) {
      state = ref[recipeName];
      console.log("recipeName, state: ", recipeName, state);
      if (state !== 'inactive') {
        if (typeof filter !== 'function' || filter(recipeName, state)) {
          this.tearDownRecipe(recipeName);
        }
      }
    }
  };

  return ActionController;

})();

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