<script>
	import spotIcon from './spotIcon.vue';
	import spotIconStack from './spotIconStack.vue';
	import spotButton from './spotButton.vue';
	import projectMediaLink from './projectMediaLink.vue';
	import projectMapLink from './projectMapLink.vue';
	import projectRelTime from './projectRelTime.vue';
	import { LngLat } from 'maplibre-gl';
	import { copyTextToClipboard } from '../scripts/common.js';

	import autosize from 'autosize';

	export default {
		components: {
			spotIcon,
			spotIconStack,
			spotButton,
			projectMediaLink,
			projectMapLink,
			projectRelTime
		},
		props: {
			options: Object
		},
		data() {
			return {
				mouseOverHeader: false,
				absTime: this.options.formatted_time,
				absTimeLocal: this.options.formatted_time_local,
				timeDiff: (this.options.formatted_time && this.options.formatted_time_local != this.options.formatted_time),
				anchorVisible: ['message', 'media', 'post'].includes(this.options.type),
				anchorTitle: this.lang.get('copy_to_clipboard'),
				anchorIcon: 'link',
				popupRequested: false,
				mouseOverDrill: false,
				postMessage: '',
				sending: false,
				focusZoomLevel: 15
			};
		},
		computed: {
			postClass() {
				let sHeaderLess = this.options.headerless?' headerless':'';
				return 'post-item '+this.options.type+sHeaderLess;
			},
			postId() {
				return this.options.id?(this.options.type+'-'+this.options.id):'';
			},
			subType() {
				return this.options.subtype || this.options.type;
			},
			displayedId() {
				return this.options.displayed_id?(this.lang.get('counter', this.options.displayed_id)):'';
			},
			drillMainIcon() {
				return this.mouseOverDrill?'drill-message':'marker';
			},
			anchorLink() {
				return '#'+[this.hash.page, this.hash.items[0], this.options.type, this.options.id].join(this.consts.hash_sep);
			},
			modeHisto() {
				return (this.project.currProject.mode == this.consts.modes.histo);
			},
			relTime() {
				return this.modeHisto?(this.options.formatted_time || '').substr(0, 10):this.options.relative_time;
			},
			lngLat() {
				return (
					['message', 'media'].includes(this.options.type)
					&& this.options.longitude
					&& this.options.latitude
				)?(new LngLat(this.options.longitude, this.options.latitude)):null;
			}
			
		},
		inject: ['api', 'lang', 'project', 'user', 'map', 'hash', 'consts', 'isMobile'],
		methods: {
			copyAnchor() {
				copyTextToClipboard(this.consts.server+this.anchorLink);
				this.anchorTitle = this.lang.get('link_copied');
				this.anchorIcon = 'copied';
				setTimeout(()=>{ //TODO animation
					this.anchorTitle = this.lang.get('copy_to_clipboard');
					this.anchorIcon = 'link';
				}, 5000);
			},
			panMapToMessage() {
				this.popupRequested = true;
				if(this.isMobile()) this.project.toggleFeedPanel(false, 'panToInstant');
				this.map.panToBetweenPanels(
					this.lngLat,
					this.focusZoomLevel,
					() => {this.map.openMarkerPopup(this.options.id_message);}
				);
				this.hash.items = [this.hash.items[0], this.options.type, this.options.id_message];
			},
			openMarkerPopup() {
				this.mouseOverDrill = true;
				if(this.map.isMarkerVisible(this.lngLat)) this.map.openMarkerPopup(this.options.id_message);
			},
			closeMarkerPopup() {
				this.mouseOverDrill = false;
				if(!this.popupRequested) this.map.closeMarkerPopup();
				this.popupRequested = false;
			},
			send() {
				if(this.postMessage != '' && this.user.name != '') {
					this.sending = true;
					this.api.get(
							'add_post',
							{
								id_project: this.project.currProject.id,
								name: this.user.name,
								content: this.postMessage
							}
						)
						.then(() => {
							this.postMessage = '';
							this.project.checkNewFeed();
							this.sending = false;
						})
						.catch((sDesc) => {
							this.sending = false;
						});
				}
			},
			executeMainAction() {
				switch(this.options.type) {
					case 'message':
						this.panMapToMessage();
						break;
					case 'media':
						this.$refs.medialink.openMedia();
						if(this.lngLat) this.map.panToBetweenPanels(this.lngLat, this.focusZoomLevel);
						break;
				}
			}
		},
		mounted() {
			//Auto-adjust text area height
			if(this.options.type == 'poster') autosize(this.$refs.post);
		}
	}
</script>

<template>
	<div :class="postClass" :id="postId">
		<div class="header" v-if="!options.headerless">
			<div class="index">
				<spotIcon :icon="subType" :text="displayedId" width="auto" />
				<a v-if="anchorVisible" class="link desktop" @click="copyAnchor" ref="anchor" :href="anchorLink" :title="anchorTitle">
					<spotIcon :icon="anchorIcon" margin="left" />
				</a>
			</div>
			<div class="time" @mouseleave="mouseOverHeader = false" @mouseover="mouseOverHeader = true" :title="timeDiff?lang.get('local_time', absTimeLocal):''">
				<Transition name="fade" mode="out-in">
					<span v-if="mouseOverHeader">{{ timeDiff?lang.get('your_time', absTime):absTime }}</span>
					<span v-else>{{ relTime }}</span>
				</Transition>
			</div>
		</div>
		<div class="body">
			<div v-if="options.type == 'message'" class="body-box">
				<p>
					<spotIcon :icon="'coords'" margin="right" size="lg" />
					<projectMapLink :options="options" />
				</p>
				<p v-if="timeDiff">
					<projectRelTime :icon="'time'" margin="right" :localTime="absTimeLocal" :siteTime="options.formatted_time" :offset="options.day_offset" />
				</p>
				<a class="drill" @click.prevent="executeMainAction" @mouseenter="openMarkerPopup" @mouseleave="closeMarkerPopup">
					<span v-if="options.weather_icon && options.weather_icon!='unknown'" class="weather clickable" :title="lang.get(options.weather_cond)">
						<spotIcon :icon="options.weather_icon" :text="Math.round(options.weather_temp)+'°C'" text-classes="temperature" />
					</span>
					<img class="staticmap clickable" :title="lang.get('click_zoom')" :src="options.static_img_url" />
					<spotIconStack :mainClasses="'message drill-icon'" :iconMain="drillMainIcon" :iconSub="'footprint'" :icon-sub-transform="'rotate-270'" />
				</a>
			</div>
			<div v-else-if="options.type == 'media'" class="body-box">
				<projectMediaLink :options="options" :type="'post'" ref="medialink" />
			</div>
			<div v-else-if="options.type == 'post'">
				<p class="message">{{ options.content }}</p>
				<p class="signature">
					<img v-if="options.gravatar" :src="'data:image/png;base64, '+options.gravatar" width="24" height="24" alt="--" />
					<span v-else>-- </span>
					<span>{{ options.formatted_name }}</span>
				</p>
			</div>
			<div v-else-if="options.type == 'poster'" class="poster-form">
				<textarea ref="post" name="post" :placeholder="lang.get('post_message')" class="autoExpand" rows="1" v-model="postMessage"></textarea>
				<div class="poster-actions">
					<input type="text" name="name" :placeholder="lang.get('post_name')" v-model="user.name" />
					<spotButton name="submit" :aria-label="lang.get('send')" :title="lang.get('send')" :icon="'send'" @click="send()" :iconClasses="sending?'flicker':''" />
				</div>
			</div>
			<div v-else-if="options.type == 'archived'">
				<p><spotIcon :icon="'success'" /></p>
				<p>{{ lang.get('mode_histo') }}</p>
			</div>
			<div v-else-if="options.type == 'loading'">
				<p class="flicker"><spotIcon :icon="'post'" /></p>
			</div>
		</div>
	</div>
</template>
