Macro.add('say', {
	skipArgs : false,
	handler  : function () {
		try {
			const currentLocation = State.variables.location.passage;
			if(!State.variables.currentConversation || State.variables.currentConversation.passage != currentLocation)
				State.variables.currentConversation = {passage: currentLocation,npcs:[[],[]]};

			let [npcId,message] = [];

			if(this.args.length == 2)
				[npcId,message] = this.args;
			else
				[npcId,message] = ['pc',this.args[0]];

			if(typeof npcId == "object")
				npcId = npcId.id;

			message = message.replaceAll("'","&apos;");

			var output;
			if(npcId == 'pc'){
				output = `<<textWithTooltip '<span class="dialogue pc">${message}</span>'>>You<</textWithTooltip>>`;
			}else if(npcId =='female'){
				output = `<span class="dialogue speaker_1_0">${message}</span>`;
			}else if(npcId =='male'){
				output = `<span class="dialogue speaker_0_0">${message}</span>`;
			}else{
				const npc = State.variables.npc(npcId);
				const npcGender = npc.gender;
				
				let npcSpeakerIndex = State.variables.currentConversation.npcs[npcGender].indexOf(npcId);
				if(npcSpeakerIndex == -1){
					npcSpeakerIndex = State.variables.currentConversation.npcs[npcGender].length;
					State.variables.currentConversation.npcs[npcGender].push(npcId);
				}

				output = `<<textWithTooltip '<span class="dialogue speaker_${npcGender}_${npcSpeakerIndex}">${message}</span>'>>${npc.usedname}<</textWithTooltip>>`;
			}
			jQuery(this.output).wiki(output);
		}
		catch (ex) {
			return this.error('ERROR in say-widget: ' + ex.message);
		}
	}
});