<template>
	<div>
		<div v-if="modal != 'true'" class="dropdown">
			<button class="btn btn-link text-dark no-caret dropdown-toggle py-0" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" title="Post options">
				<span v-bind:class="[size =='lg' ? 'fas fa-ellipsis-v fa-lg text-muted' : 'fas fa-ellipsis-v fa-sm text-lighter']"></span>
			</button>
			<div class="dropdown-menu dropdown-menu-right">
				<a class="dropdown-item font-weight-bold text-decoration-none" :href="status.url">Go to post</a>
				<!-- <a class="dropdown-item font-weight-bold text-decoration-none" href="#">Share</a>
				<a class="dropdown-item font-weight-bold text-decoration-none" href="#">Embed</a> -->
				<span v-if="statusOwner(status) == false">
					<a class="dropdown-item font-weight-bold" :href="reportUrl(status)">Report</a>
				</span>
				<span v-if="statusOwner(status) == true">
					<a class="dropdown-item font-weight-bold text-decoration-none" @click.prevent="muteProfile(status)">Mute Profile</a>
					<a class="dropdown-item font-weight-bold text-decoration-none" @click.prevent="blockProfile(status)">Block Profile</a>
				</span>
				<span v-if="profile.is_admin == true">
					<div class="dropdown-divider"></div>
					<a class="dropdown-item font-weight-bold text-danger text-decoration-none" v-on:click="deletePost(status)">Delete</a>
					<div class="dropdown-divider"></div>
					<h6 class="dropdown-header">Mod Tools</h6>
					<a class="dropdown-item font-weight-bold text-decoration-none" v-on:click="moderatePost(status, 'autocw')">
						<p class="mb-0">Enforce CW</p>
						<p class="mb-0  small text-muted">Adds a CW to every post <br> made by this account.</p>
					</a>
					<a class="dropdown-item font-weight-bold text-decoration-none" v-on:click="moderatePost(status, 'noautolink')">
						<p class="mb-0">No Autolinking</p>
						<p class="mb-0 small text-muted">Do not transform mentions, <br> hashtags or urls into HTML.</p>
					</a>
					<a class="dropdown-item font-weight-bold text-decoration-none" v-on:click="moderatePost(status, 'unlisted')">
						<p class="mb-0">Unlisted Posts</p>
						<p class="mb-0 small text-muted">Removes account from <br> public/network timelines.</p>
					</a>
					<a class="dropdown-item font-weight-bold text-decoration-none" v-on:click="moderatePost(status, 'disable')">
						<p class="mb-0">Disable Account</p>
						<p class="mb-0 small text-muted">Temporarily disable account <br> until next time user log in.</p>
					</a>
					<a class="dropdown-item font-weight-bold text-decoration-none" v-on:click="moderatePost(status, 'suspend')">
						<p class="mb-0">Suspend Account</p>
						<p class="mb-0 small text-muted">This prevents any new interactions, <br> without deleting existing data.</p>
					</a>

				</span>
			</div>
		</div>
		<div v-if="modal == 'true'">
			<span data-toggle="modal" :data-target="'#mt_pid_'+status.id">
				<span v-bind:class="[size =='lg' ? 'fas fa-ellipsis-v fa-lg text-muted' : 'fas fa-ellipsis-v fa-sm text-lighter']"></span>
			</span>
			<div class="modal" tabindex="-1" role="dialog" :id="'mt_pid_'+status.id">
				<div class="modal-dialog modal-sm" role="document">
					<div class="modal-content">
						<div class="modal-body">
							<div class="list-group">
								<a class="list-group-item font-weight-bold text-decoration-none" :href="status.url">Go to post</a>
								<!-- <a class="list-group-item font-weight-bold text-decoration-none" :href="status.url">Share</a>
								<a class="list-group-item font-weight-bold text-decoration-none" :href="status.url">Embed</a> -->
								<a class="list-group-item font-weight-bold text-decoration-none" href="#" @click="hidePost(status)">Hide</a>
								<span v-if="statusOwner(status) == false">
									<a class="list-group-item font-weight-bold text-decoration-none" :href="reportUrl(status)">Report</a>
									<a class="list-group-item font-weight-bold text-decoration-none" v-on:click="muteProfile(status)" href="#">Mute Profile</a>
									<a class="list-group-item font-weight-bold text-decoration-none" v-on:click="blockProfile(status)" href="#">Block Profile</a>
								</span>
								<span v-if="statusOwner(status) == true || profile.is_admin == true">
									<a class="list-group-item font-weight-bold text-danger text-decoration-none" v-on:click="deletePost">Delete</a>
								</span>
								<span v-if="profile.is_admin == true">
									<a class="list-group-item font-weight-bold text-decoration-none" v-on:click="moderatePost(status, 'autocw')" href="#">
										<p class="mb-0">Enforce CW</p>
										<p class="mb-0  small text-muted">Adds a CW to every post <br> made by this account.</p>
									</a>
									<a class="list-group-item font-weight-bold text-decoration-none" v-on:click="moderatePost(status, 'noautolink')" href="#">
										<p class="mb-0">No Autolinking</p>
										<p class="mb-0 small text-muted">Do not transform mentions, <br> hashtags or urls into HTML.</p>
									</a>
									<a class="list-group-item font-weight-bold text-decoration-none" v-on:click="moderatePost(status, 'unlisted')" href="#">
										<p class="mb-0">Unlisted Posts</p>
										<p class="mb-0 small text-muted">Removes account from <br> public/network timelines.</p>
									</a>
									<a class="list-group-item font-weight-bold text-decoration-none" v-on:click="moderatePost(status, 'disable')" href="#">
										<p class="mb-0">Disable Account</p>
										<p class="mb-0 small text-muted">Temporarily disable account <br> until next time user log in.</p>
									</a>
									<a class="list-group-item font-weight-bold text-decoration-none" v-on:click="moderatePost(status, 'suspend')" href="#">
										<p class="mb-0">Suspend Account</p>
										<p class="mb-0 small text-muted">This prevents any new interactions, <br> without deleting existing data.</p>
									</a>

								</span>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</template>

<style type="text/css" scoped>
	.text-lighter {
		color:#B8C2CC !important;
	}
	.modal-body {
		padding: 0;
	}
</style>

<script type="text/javascript">
	export default {
		props: ['feed', 'status', 'profile', 'size', 'modal'],

		methods: {
			reportUrl(status) {
				let type = status.in_reply_to ? 'comment' : 'post';
				let id = status.id;
				return '/i/report?type=' + type + '&id=' + id;
			},

			timestampFormat(timestamp) {
				let ts = new Date(timestamp);
				return ts.toDateString() + ' ' + ts.toLocaleTimeString();
			},

			editUrl(status) {
				return status.url + '/edit';
			},

			redirect(url) {
				window.location.href = url;
				return;
			},

			replyUrl(status) {
				let username = this.profile.username;
				let id = status.account.id == this.profile.id ? status.id : status.in_reply_to_id;
				return '/p/' + username + '/' + id;
			},

			mentionUrl(status) {
				let username = status.account.username;
				let id = status.id;
				return '/p/' + username + '/' + id;
			},

			statusOwner(status) {
				let sid = parseInt(status.account.id);
				let uid = parseInt(this.profile.id);
				if(sid == uid) {
					return true;
				} else {
					return false;
				}
			},

			deletePost() {
				this.$emit('deletePost');
				$('#mt_pid_'+this.status.id).modal('hide');
			},

			hidePost(status) {
				status.sensitive = true;
				$('#mt_pid_'+status.id).modal('hide');
			},

			moderatePost(status, action, $event) {
				let username = status.account.username;
				switch(action) {
					case 'autocw':
						let msg = 'Are you sure you want to enforce CW for ' + username + ' ?';
						swal({
							title: 'Confirm',
							text: msg,
							icon: 'warning',
							buttons: true,
							dangerMode: true
						});
					break;
					case 'suspend':
						msg = 'Are you sure you want to suspend the account of ' + username + ' ?';
						swal({
							title: 'Confirm',
							text: msg,
							icon: 'warning',
							buttons: true,
							dangerMode: true
						});
					break;
				}
			},

			muteProfile(status) {
				if($('body').hasClass('loggedIn') == false) {
					return;
				}

				axios.post('/i/mute', {
					type: 'user',
					item: status.account.id
				}).then(res => {
					swal('Success', 'You have successfully muted ' + status.account.acct, 'success');
				}).catch(err => {
					swal('Error', 'Something went wrong. Please try again later.', 'error');
				});
			},

			blockProfile(status) {
				if($('body').hasClass('loggedIn') == false) {
					return;
				}

				axios.post('/i/block', {
					type: 'user',
					item: status.account.id
				}).then(res => {
					swal('Success', 'You have successfully blocked ' + status.account.acct, 'success');
				}).catch(err => {
					swal('Error', 'Something went wrong. Please try again later.', 'error');
				});
			}
		}
	}
</script>