/* globals KonchatNotification */

Template.chatRoomItem.helpers({
	roomData() {
		let name = this.name;
		const realNameForDirectMessages = RocketChat.settings.get('UI_Use_Real_Name') && this.t === 'd';
		const realNameForChannel = RocketChat.settings.get('UI_Allow_room_names_with_special_chars') && this.t !== 'd';
		if ((realNameForDirectMessages || realNameForChannel) && this.fname) {
			name = this.fname;
		}

		let unread = false;
		if (((FlowRouter.getParam('_id') !== this.rid) || !document.hasFocus()) && (this.unread > 0)) {
			unread = this.unread;
		}

		let active = false;
		if ([this.rid, this._id].find(id => id === Session.get('openedRoom'))) {
			active = true;
		}

		const archivedClass = this.archived ? 'archived' : false;

		let alertClass = false;
		if (!this.hideUnreadStatus && (FlowRouter.getParam('_id') !== this.rid || !document.hasFocus()) && this.alert) {
			alertClass = 'sidebar-item__link--active';
		}

		let statusClass = false;

		if (this.t === 'd') {
			switch (RocketChat.roomTypes.getUserStatus(this.t, this.rid)) {
				case 'online':
					statusClass = 'general-success-background';
					break;
				case 'away':
					statusClass = 'general-pending-background';
					break;
				case 'busy':
					statusClass = 'general-error-background';
					break;
				case 'offline':
					statusClass = 'general-inactive-background';
					break;
				default:
					statusClass = 'general-inactive-background';
			}
		}

		// Sound notification
		if (!(FlowRouter.getParam('name') === this.name) && !this.ls && this.alert === true) {
			KonchatNotification.newRoom(this.rid);
		}

		const icon = RocketChat.roomTypes.getIcon(this.t);
		const avatar = !icon;

		return {
			...this,
			icon,
			avatar,
			route: RocketChat.roomTypes.getRouteLink(this.t, this),
			name,
			unread,
			active,
			archivedClass,
			alertClass,
			statusClass
		};
	}
});

Template.chatRoomItem.onRendered = function() {
	if (!(FlowRouter.getParam('name') && (FlowRouter.getParam('name') === this.name)) && !this.ls && (this.alert === true)) {
		return KonchatNotification.newRoom(this.rid);
	}
};

@@ -961,16 +961,41 @@
  false;%0A
+%09%09let status = 'offline';
 %0A%09%09if (t
@@ -1019,15 +1019,82 @@
 %09%09%09s
-witch (
+tatus = Session.get(%60user_$%7B this.name %7D_status%60);%0A%09%09%7D else %7B%0A%09%09%09status = 
 Rock
@@ -1141,21 +1141,42 @@
 his.rid)
+;%0A%09%09%7D%0A%09%09switch (status
 ) %7B%0A
-%09
 %09%09%09case 
@@ -1181,33 +1181,32 @@
 e 'online':%0A%09%09%09%09
-%09
 statusClass = 'g
@@ -1233,33 +1233,31 @@
 round';%0A%09%09%09%09
-%09
 break;%0A
-%09
 %09%09%09case 'awa
@@ -1256,33 +1256,32 @@
 ase 'away':%0A%09%09%09%09
-%09
 statusClass = 'g
@@ -1300,33 +1300,32 @@
 ng-background';%0A
-%09
 %09%09%09%09break;%0A%09%09%09%09c
@@ -1318,25 +1318,24 @@
 %09%09break;%0A%09%09%09
-%09
 case 'busy':
@@ -1335,17 +1335,16 @@
 'busy':%0A
-%09
 %09%09%09%09stat
@@ -1385,25 +1385,23 @@
 d';%0A%09%09%09%09
-%09
 break;%0A
-%09
 %09%09%09case 
@@ -1411,25 +1411,24 @@
 fline':%0A%09%09%09%09
-%09
 statusClass 
@@ -1464,25 +1464,23 @@
 d';%0A%09%09%09%09
-%09
 break;%0A
-%09
 %09%09%09defau
@@ -1483,17 +1483,16 @@
 efault:%0A
-%09
 %09%09%09%09stat
@@ -1538,14 +1538,10 @@
 ;%0A%09%09
-%09
 %7D%0A
-%09%09%7D
 %0A%0A%09%09

fix offline users on search