<script lang="ts" setup>
import { computed, ref, watch } from 'vue'

import { defaultStickyProps } from './constants'
import type { StickyProps } from './types'
import { ElInput } from 'element-plus'
import Markdown from '../markdown/Markdown.vue'

const props = withDefaults(defineProps<StickyProps>(), defaultStickyProps)

const emit = defineEmits<{
	edit: [editing: boolean]
	'update:modelValue': [value: string]
	'markdown-click': [link: HTMLAnchorElement, e: MouseEvent]
}>()

const isResizing = ref(false)
const input = ref<HTMLTextAreaElement | undefined>(undefined)

const resHeight = computed((): number => {
	return props.height < props.minHeight ? props.minHeight : props.height
})

const resWidth = computed((): number => {
	return props.width < props.minWidth ? props.minWidth : props.width
})

const inputName = computed(() => (props.id ? `${props.id}-input` : undefined))

const styles = computed((): { height: string; width: string; backgroundColor: string } => ({
	height: `${resHeight.value}px`,
	width: `${resWidth.value}px`,
	backgroundColor: props.backgroundColor
}))

const shouldShowFooter = computed((): boolean => resHeight.value > 100 && resWidth.value > 155)

watch(
	() => props.editMode,
	(newMode, prevMode) => {
		setTimeout(() => {
			if (newMode && !prevMode && input.value) {
				if (props.defaultText === props.modelValue) {
					input.value.select()
				}
				input.value.focus()
			}
		}, 100)
	}
)

const onDoubleClick = () => {
	if (!props.readOnly) emit('edit', true)
}

const onInputBlur = () => {
	if (!isResizing.value) emit('edit', false)
}

const onUpdateModelValue = (value: string) => {
	emit('update:modelValue', value)
}

const onMarkdownClick = (link: HTMLAnchorElement, event: MouseEvent) => {
	emit('markdown-click', link, event)
}

const onInputScroll = (event: WheelEvent) => {
	// Pass through zoom events but hold regular scrolling
	if (!event.ctrlKey && !event.metaKey) {
		event.stopPropagation()
	}
}
</script>

<template>
	<div
		:class="{
			'sticky-note': true,
			[$style.sticky]: true,
			[$style.clickable]: !isResizing
		}"
		:style="styles"
		@keydown.prevent
	>
		<div v-show="!editMode" :class="$style.wrapper" @dblclick.stop="onDoubleClick">
			<Markdown
				theme="sticky"
				:content="modelValue"
				:with-multi-breaks="true"
				@markdown-click="onMarkdownClick"
				@update-content="onUpdateModelValue"
			/>
		</div>
		<div
			v-show="editMode"
			:class="{ 'full-height': !shouldShowFooter, 'sticky-textarea': true }"
			@click.stop
			@mousedown.stop
			@mouseup.stop
			@keydown.esc="onInputBlur"
			@keydown.stop
		>
			<ElInput
				ref="input"
				:model-value="modelValue"
				:name="inputName"
				type="textarea"
				:rows="5"
				@blur="onInputBlur"
				@update:model-value="onUpdateModelValue"
				@wheel="onInputScroll"
			/>
		</div>
		<div v-if="editMode && shouldShowFooter" :class="$style.footer">
			<span>{{ footerText }}</span>
		</div>
	</div>
</template>

<style lang="less" module>
.sticky {
	position: relative;
	border-radius: 4px;
	overflow: hidden;
	border: 1px solid #eee;
}

.clickable {
	cursor: pointer;
}

.wrapper {
	width: 100%;
	height: 100%;
	position: absolute;
	padding: 0.5rem 0.75rem 0;
	overflow: hidden;
}
</style>

<style lang="less">
.sticky-textarea {
	height: 100%;
	box-sizing: border-box;
	padding: 0.5rem 0.75rem;
	cursor: default;

	.el-textarea {
		height: 100%;

		.el-textarea__inner {
			height: 100%;
			resize: unset;
		}
	}
}

.full-height {
	height: calc(100% - var(--spacing--2xs));
}
</style>
