define([
	'js/system/events',
	'js/rendering/renderer'
], function (
	events,
	renderer
) {
	var scale = 40;

	return {
		type: 'stats',

		values: null,

		hpSprite: null,
		hpSpriteInner: null,

		init: function (blueprint) {
			if (this.obj.self)
				events.emit('onGetStats', this.values);

			var serverId = this.obj.serverId;
			if (serverId != null)
				events.emit('onGetPartyStats', serverId, this.values);

			var obj = this.obj;

			var yOffset = -12;
			if (obj.isChampion)
				yOffset = -18;

			this.hpSprite = renderer.buildRectangle({
				layerName: 'effects',
				x: obj.x * scale,
				y: obj.y * scale,
				w: 1,
				h: 1,
				color: 0x802343
			});

			this.hpSpriteInner = renderer.buildRectangle({
				x: 0,
				y: 0,
				w: 1,
				h: 1,
				layerName: 'effects',
				color: 0xd43346
			});

			this.updateHpSprite();
		},

		updateHpSprite: function () {
			if (this.obj.dead)
				return;

			var obj = this.obj;

			var yOffset = -12;
			if (obj.isChampion)
				yOffset = -18;

			var x = obj.x * scale;
			var y = (obj.y * scale) + yOffset;

			renderer.moveRectangle({
				sprite: this.hpSprite,
				x: x + 4,
				y: y,
				w: (scale - 8),
				h: 5
			});

			renderer.moveRectangle({
				sprite: this.hpSpriteInner,
				x: x + 4,
				y: y,
				w: (this.values.hp / this.values.hpMax) * (scale - 8),
				h: 5
			});

			this.hpSprite.visible = (this.values.hp < this.values.hpMax);
			this.hpSpriteInner.visible = this.hpSprite.visible;
		},

		extend: function (blueprint) {
			var bValues = blueprint.values || {};

			var values = this.values;

			for (var b in bValues) {
				values[b] = bValues[b];
			}

			if (this.obj.self)
				events.emit('onGetStats', this.values);

			var serverId = this.obj.serverId;
			if (serverId != null)
				events.emit('onGetPartyStats', serverId, this.values);

			this.updateHpSprite();
		},

		destroy: function () {
			renderer.destroyObject({
				sprite: this.hpSprite,
				layerName: 'effects'
			});

			renderer.destroyObject({
				sprite: this.hpSpriteInner,
				layerName: 'effects'
			});
		}
	};
});
