<template>

	<div class='container'>

		<div class='header'>
			<div class='title'>{{ gs.room.get('text') ? gs.room.get('text').name : '?' }}</div>
		</div>

		<div class='body'>

			<section class='message-stack'>

				<div class='message' v-for='m in gs.content.messages'>
					<div class='icon' :style='`background-image: url("https://picsum.photos/150/?random&nonce=${m.uid}");`'></div>
					<span class='meta'>
						<span class='author'>{{ gs.content.user(m.uid).username || `guest ${m.uid}` }}</span>
						<span class='date'  >{{ m.ts || 'inconnu' }}</span>
					</span>
					<span class='text'>{{ m.msg }}</span>
				</div>

			</section>

			<section class='message-input'>
				<textarea :value='message' :placeholder='`Message #${gs.room.get(`text`).name}`' @keydown='keydown' @keyup='keyup'></textarea>
			</section>

		</div>

	</div>

</template><script>
export default {

	name: 'channel-',

	data(){ return {
		gs: gs.get,
		message: '', // message text
		pressed: {
			16: false // SHIFT key
		}

	}; },

	methods: {

		/* (1) Auto-grow text area
		*
		---------------------------------------------------------*/
		auto_grow(e){

			setTimeout(() => {
				e.target.style.height = '0';
				e.target.style.height = `calc( ${e.target.scrollHeight}px )`;
			}, 1);

		},


		/* (2) Mange Key Down
		*
		---------------------------------------------------------*/
		keydown(e){

			// Manage auto grow
			this.auto_grow(e);

			// register pressed keys
			this.pressed[e.keyCode] = true;

			// if not ENTER OR SHIFT -> do nothing
			if( e.keyCode !== 13 || this.pressed[16] )
				return;



			// bufferize message + remove trailing line
			let buffer = e.target.value.replace(/\n*$/, '');
			this.message = buffer;

			// send message
			if( !gs.get.content.send_message(buffer) )
				return;

			// empty message
			this.message = '';

		},


		/* (3) Mange Key Up
		*
		---------------------------------------------------------*/
		keyup(e){

			// unregister pressed keys
			this.pressed[e.keyCode] = false;

		}

	}

}

</script>