'use strict';
var LevelUtil = require('../../src/levels').LevelUtil,
  Skills = require('../../src/skills').Skills,
  CommandUtil = require('../../src/command_util').CommandUtil,
  util = require('util'),
  Commands = require('../../src/commands').Commands;

exports.listeners = {

  //// Function wrappers needed to access "this" (Player obj)
  //TODO: Do regenHealth, regenSanity, and regenActions
  regen: function(l10n) {
    return function(bonus) {
      bonus = bonus || 1;
      const self = this;
      const regenInterval = 2000;

      const regen = setInterval(() => {
        const health = self.getAttribute('health');
        let regenerated = Math.floor(Math.random() * self.getAttribute('stamina') + bonus);

        regenerated = Math.min(self.getAttribute('max_health'), health + regenerated);
        util.log(self.getName() + ' has regenerated up to ' + regenerated + ' health.');

        self.setAttribute('health', regenerated);
        if (regenerated === self.getAttribute('max_health')) {
          clearInterval(regen);
        }
      }, regenInterval);
    }
  },

  experience: function(l10n) {
    return function(experience) {

      const maxLevel = 60;
      if (this.getAttribute('level') >= maxLevel) {
        return;
      }

      util.log(this.getName() + ' has gained ' + experience + ' XP.');
      this.sayL10n(l10n, 'EXPGAIN', experience);

      const tnl = LevelUtil.expToLevel(this.getAttribute('level')) - this.getAttribute('experience');

      if (experience >= tnl) {
        return this.emit('level');
      }

      this.setAttribute('experience', this.getAttribute('experience') + experience);
    }
  },

  level: function(l10n) {
    return function() {
      const name = this.getName();
      const newLevel = this.getAttribute('level') + 1;
      const healthGain = Math.ceil(this.getAttribute('max_health') * 1.10);
      const gainedMutation = newLevel % 2 === 0;

      let mutationPoints = this.getAttribute('mutagens');

      util.log(name + ' is now level ' + newLevel);

      this.sayL10n(l10n, 'LEVELUP');

      if (gainedMutation) {
        this.sayL10n(l10n, 'MUTAGEN_GAIN');
        mutationPoints++;
        this.setAttribute('mutagens', mutationPoints);
      }

      this.setAttribute('level', newLevel);
      this.setAttribute('experience', 0);
      this.setAttribute('attrPoints', (this.getAttribute('attrPoints') || 0) + 1);

      // Do whatever you want to do here when a player levels up...
      this.setAttribute('max_health', healthGain);
      this.setAttribute('health', this.getAttribute('max_health'));
      util.log(name + ' now has ' + healthGain + ' max health.');

      // Add points for skills
      const skillGain = LevelUtil.getTrainingTime(newLevel);
      const newTrainingTime = this.getTraining('time') + skillGain;
      util.log(name + ' can train x', newTrainingTime);
      this.setTraining('time', newTrainingTime);
    }
  },

  //TODO: Permadeath, add it.
  die: function(l10n) {
    return function() {
      const startLocation = 1;
      const playerExp = this.getAttribute('experience');
      const experiencePenalty = playerExp - Math.ceil((playerExp * 0.10));

      util.log(this.getName() + ' died.');
      Commands.player_commands.remove('all', this, true);
      Commands.player_commands.drop('all', this);

      this.setLocation(startLocation);
      this.emit('regen');
      this.setAttribute('experience', experiencePenalty);
    }
  },

  changeTime: function(l10n) {
    return function (wasDaytime, rooms) {
      const playerIsOutside = rooms.getAt(this.getLocation()).biome === 'outdoors';

      if (playerIsOutside) {
        if (wasDaytime) {
          this.sayL10n(l10n, "SUN_SETTING");
          setTimeout(() => this.sayL10n(l10n, "SUN_SET"), 5000);

        } else {
          this.sayL10n(l10n, "SUN_RISING");
          setTimeout(() => this.sayL10n(l10n, "SUN_UP"), 5000);
        }

      } else if (wasDaytime) {
        this.sayL10n(l10n, "SUN_SETTING_INDOORS");
      } else {
        this.sayL10n(l10n, "SUN_UP_INDOORS");
      }

    }
  }
};

@@ -351,53 +351,62 @@
 DO: 
-Do regenHealth, regenSanity, and regenAc
+Add check for movement which ends regen or medita
 tion
-s
+.
 %0A  r
@@ -947,32 +947,32 @@
 , regenerated);%0A
-
         if (rege
@@ -1097,16 +1097,714 @@
 %7D%0A  %7D,%0A%0A
+  meditate: function(l10n) %7B%0A    return function(bonus) %7B%0A      bonus = bonus %7C%7C 1;%0A      const self = this;%0A      const regenInterval = 2000;%0A%0A      const regen = setInterval(() =%3E %7B%0A        const sanity = self.getAttribute('sanity');%0A        let regenerated = Math.floor(Math.random() * self.getAttribute('stamina') + bonus);%0A%0A        regenerated = Math.min(self.getAttribute('max_sanity'), sanity + regenerated);%0A        util.log(self.getName() + ' has regenerated up to ' + regenerated + ' sanity.');%0A%0A        self.setAttribute('sanity', regenerated);%0A        if (regenerated === self.getAttribute('max_sanity')) %7B%0A          clearInterval(regen);%0A        %7D%0A      %7D, regenInterval);%0A    %7D%0A  %7D,%0A%0A
   experi

Add meditate function which mimics the regen function.