/* globals WebRTC */

Template.membersList.helpers({
	tAddUsers() {
		return t('Add_users');
	},

	isGroupChat() {
		return ['c', 'p'].includes(ChatRoom.findOne(this.rid, { reactive: false }).t);
	},

	isDirectChat() {
		return ChatRoom.findOne(this.rid, { reactive: false }).t === 'd';
	},

	seeAll() {
		if (Template.instance().showAllUsers.get()) {
			return t('Show_only_online');
		} else {
			return t('Show_all');
		}
	},

	roomUsers() {
		const onlineUsers = RoomManager.onlineUsers.get();
		const roomUsernames = Template.instance().users.get();
		const room = ChatRoom.findOne(this.rid);
		const roomMuted = (room != null ? room.muted : undefined) || [];
		const userUtcOffset = Meteor.user().utcOffset;
		let totalOnline = 0;
		let users = roomUsernames.map(function(username) {
			let utcOffset;
			if (onlineUsers[username] != null) {
				totalOnline++;
				({ utcOffset } = onlineUsers[username]);

				if (utcOffset != null) {
					if (utcOffset === userUtcOffset) {
						utcOffset = '';
					} else if (utcOffset > 0) {
						utcOffset = `+${ utcOffset }`;
					} else {
						utcOffset = `(UTC ${ utcOffset })`;
					}
				}
			}

			return {
				username,
				status: (onlineUsers[username] != null ? onlineUsers[username].status : undefined),
				muted: Array.from(roomMuted).includes(username),
				utcOffset
			};
		});

		users = _.sortBy(users, 'username');
		// show online users first.
		// sortBy is stable, so we can do this
		users = _.sortBy(users, u => u.status == null);

		let hasMore = undefined;
		const usersLimit = Template.instance().usersLimit.get();
		if (usersLimit) {
			hasMore = users.length > usersLimit;
			users = _.first(users, usersLimit);
		}
		const totalShowing = users.length;

		const ret = {
			_id: this.rid,
			total: Template.instance().total.get(),
			totalShowing,
			loading: Template.instance().loading.get(),
			totalOnline,
			users,
			hasMore
		};
		return ret;
	},

	canAddUser() {
		const roomData = Session.get(`roomData${ this._id }`);
		if (!roomData) { return ''; }
		return (() => {
			switch (roomData.t) {
				case 'p': return RocketChat.authz.hasAtLeastOnePermission(['add-user-to-any-p-room', 'add-user-to-joined-room'], this._id);
				case 'c': return RocketChat.authz.hasAtLeastOnePermission(['add-user-to-any-c-room', 'add-user-to-joined-room'], this._id);
				default: return false;
			}
		})();
	},

	autocompleteSettingsAddUser() {
		return {
			limit: 10,
			// inputDelay: 300
			rules: [
				{
					collection: 'UserAndRoom',
					subscription: 'userAutocomplete',
					field: 'username',
					template: Template.userSearch,
					noMatchTemplate: Template.userSearchEmpty,
					matchAll: true,
					filter: {
						exceptions: [Meteor.user().username]
					},
					selector(match) {
						return { term: match };
					},
					sort: 'username'
				}
			]
		};
	},

	showUserInfo() {
		const webrtc = WebRTC.getInstanceByRoomId(this.rid);
		let videoActive = undefined;
		if (webrtc && webrtc.localUrl && webrtc.localUrl.get()) {
			videoActive = webrtc.localUrl.get();
		} else if (webrtc && webrtc.remoteItems && webrtc.remoteItems.get() && webrtc.remoteItems.get().length > 0) {
			videoActive = webrtc.remoteItems.get();
		}
		return Template.instance().showDetail.get() && !videoActive;
	},

	userInfoDetail() {
		const room = ChatRoom.findOne(this.rid, { fields: { t: 1 } });

		return {
			tabBar: Template.currentData().tabBar,
			username: Template.instance().userDetail.get(),
			clear: Template.instance().clearUserDetail,
			showAll: ['c', 'p'].includes(room != null ? room.t : undefined),
			hideAdminControls: ['c', 'p', 'd'].includes(room != null ? room.t : undefined),
			video: ['d'].includes(room != null ? room.t : undefined)
		};
	}});

Template.membersList.events({
	'click .see-all'(e, instance) {
		const seeAll = instance.showAllUsers.get();
		instance.showAllUsers.set(!seeAll);

		if (!seeAll) {
			return instance.usersLimit.set(100);
		}
	},

	'autocompleteselect #user-add-search'(event, template, doc) {

		const roomData = Session.get(`roomData${ template.data.rid }`);

		if (['c', 'p'].includes(roomData.t)) {
			return Meteor.call('addUserToRoom', { rid: roomData._id, username: doc.username }, function(error) {
				if (error) {
					return handleError(error);
				}

				return $('#user-add-search').val('');
			});
		}
	},

	'click .show-more-users'(e, instance) {
		return instance.usersLimit.set(instance.usersLimit.get() + 100);
	}
});

Template.membersList.onCreated(function() {
	this.showAllUsers = new ReactiveVar(false);
	this.usersLimit = new ReactiveVar(100);
	this.userDetail = new ReactiveVar;
	this.showDetail = new ReactiveVar(false);

	this.users = new ReactiveVar([]);
	this.total = new ReactiveVar;
	this.loading = new ReactiveVar(true);

	this.tabBar = Template.instance().tabBar;

	Tracker.autorun(() => {
		if (this.data.rid == null) { return; }

		this.loading.set(true);
		return Meteor.call('getUsersOfRoom', this.data.rid, this.showAllUsers.get(), (error, users) => {
			this.users.set(users.records);
			this.total.set(users.total);
			return this.loading.set(false);
		}
		);
	}
	);

	this.clearUserDetail = () => {
		this.showDetail.set(false);
		return setTimeout(() => {
			return this.clearRoomUserDetail();
		}
		, 500);
	};

	this.showUserDetail = username => {
		this.showDetail.set(username != null);
		return this.userDetail.set(username);
	};

	this.clearRoomUserDetail = this.data.clearUserDetail;

	return this.autorun(() => {
		const data = Template.currentData();
		return this.showUserDetail(data.userDetail);
	}
	);
});
