// Generated by CoffeeScript 1.6.3
(function() {
  var Player, debug, pubsub;

  pubsub = require('./PubSub');

  debug = require('debug')('xbmc:Player');

  Player = (function() {
    var _this = this;

    function Player() {}

    Player.mixin = function(api) {
      var method, name;
      debug('mixin');
      this.api = api;
      api.player = {};
      for (name in this) {
        method = this[name];
        api.player[name] = method;
      }
      return delete api.player.mixin;
    };

    Player.open = function(item, options, fn) {
      var dfd;
      if (options == null) {
        options = {};
      }
      if (fn == null) {
        fn = null;
      }
      debug('open', item, options);
      dfd = Player.api.send('Player.Open', {
        item: item,
        options: options
      });
      return dfd.then(function(data) {
        pubsub.emit('player.open', data);
        if (fn) {
          return fn(data);
        }
      });
    };

    Player.getActivePlayers = function(fn) {
      var dfd;
      if (fn == null) {
        fn = null;
      }
      debug('getActivePlayers');
      dfd = Player.api.send('Player.GetActivePlayers');
      return dfd.then(function(data) {
        pubsub.emit('player.activePlayers', data);
        if (fn) {
          return fn(data);
        }
      });
    };

    Player.playPause = function(fn) {
      if (fn == null) {
        fn = null;
      }
      debug('playPause');
      return Player.getActivePlayers(function(data) {
        var dfd, playerId, _ref, _ref1,
          _this = this;
        playerId = ((_ref = data.result) != null ? _ref.playerid : void 0) || ((_ref1 = data.player) != null ? _ref1.playerid : void 0);
        dfd = this.api.send('Player.PlayPause', {
          playerid: playerId
        });
        return dfd.then(function(data) {
          pubsub.emit('player.playpause', data);
          if (fn) {
            return fn(data);
          }
        });
      });
    };

    Player.stop = function(fn) {
      if (fn == null) {
        fn = null;
      }
      debug('stop');
      return Player.getActivePlayers(function(data) {
        var dfd, playerId, _ref, _ref1,
          _this = this;
        playerId = ((_ref = data.result) != null ? _ref.playerid : void 0) || ((_ref1 = data.player) != null ? _ref1.playerid : void 0);
        dfd = this.api.send('Player.Stop', {
          playerid: playerId
        });
        return dfd.then(function(data) {
          pubsub.emit('player.stop', data);
          if (fn) {
            return fn(data);
          }
        });
      });
    };

    Player.forward = function(fn) {
      if (fn == null) {
        fn = null;
      }
      debug('forward');
      return Player.rewind(true, fn);
    };

    Player.rewind = function(forward, fn) {
      var speed;
      if (forward == null) {
        forward = false;
      }
      if (fn == null) {
        fn = null;
      }
      speed = forward ? 'increment' : 'decrement';
      debug('rewind', speed);
      return Player.getActivePlayers(function(data) {
        var dfd, playerId, _ref, _ref1,
          _this = this;
        playerId = ((_ref = data.result) != null ? _ref.playerid : void 0) || ((_ref1 = data.player) != null ? _ref1.playerid : void 0);
        dfd = this.api.send('Player.SetSpeed', {
          playerid: playerId,
          speed: speed
        });
        return dfd.then(function(data) {
          pubsub.emit('player.setspeed', data);
          if (fn) {
            return fn(data);
          }
        });
      });
    };

    Player.openYoutube = function(id, options, fn) {
      var item;
      if (options == null) {
        options = {};
      }
      if (fn == null) {
        fn = null;
      }
      debug('openYoutube', id, options);
      item = {
        file: "plugin://plugin.video.youtube/?action=play_video&videoid=" + id
      };
      return Player.open(item, options, fn);
    };

    Player.openFile = function(file, options, fn) {
      var item;
      if (options == null) {
        options = {};
      }
      if (fn == null) {
        fn = null;
      }
      debug('openFile', file, options);
      item = {
        file: file
      };
      return Player.open(item, options, fn);
    };

    return Player;

  }).call(this);

  module.exports = Player;

}).call(this);
