<template>
    <div class="absolute" ref="dom" :style="{transform: `translate(${node.x}px, ${node.y}px)`, width: `${node.width}px`, height: `${node.height}px`, '--canvas-color': node.color?.hex}" :class="{'-z-10': node.type === 'group', 'z-10': node.type !== 'group'}">
        <div v-if="!editing || node.type === 'group'" style="outline-style: solid;" :class="[style.border, style.outline, { '!outline-4 cursor-move': focusing }]" class="outline-0 transition-[outline-width] border-2 bg-light-20 dark:bg-dark-20 w-full h-full">
            <div class="w-full h-full py-2 px-4 flex !bg-opacity-[0.07]" :class="style.bg" @click.left="(e) => { if(node.type !== 'group') selectNode(e) }" @dblclick.left="(e) => { if(node.type !== 'group') editNode(e) }">
                <div v-if="node.text?.length > 0" class="flex items-center">
                    <MarkdownRenderer :content="node.text" :proses="{ a: FakeA }" />
                </div>
            </div>
            <div v-if="focusing">
                <span @mousedown.left="(e) => resizeNode(e, 0, 1, 0, -1)" id="n " class="cursor-n-resize absolute -top-3 -right-3 -left-3  h-6 group">
                    <span @mousedown.left="(e) => dragEdge(e, 'top')" :class="[style.bg]" class="hidden group-hover:block absolute rounded-full border-2 border-light-70 dark:border-dark-70 w-6 h-6 -top-1.5 left-1/2 -translate-x-3"></span>
                </span> <!-- North -->
                <span @mousedown.left="(e) => resizeNode(e, 0, 0, 0, 1)" id="s " class="cursor-s-resize absolute -bottom-3 -right-3 -left-3 h-6 group">
                    <span @mousedown.left="(e) => dragEdge(e, 'bottom')" :class="[style.bg]" class="hidden group-hover:block absolute rounded-full border-2 border-light-70 dark:border-dark-70 w-6 h-6 -bottom-1.5 left-1/2 -translate-x-3"></span>
                </span> <!-- South -->
                <span @mousedown.left="(e) => resizeNode(e, 0, 0, 1, 0)" id="e " class="cursor-e-resize absolute -top-3 -bottom-3 -right-3 w-6 group">
                    <span @mousedown.left="(e) => dragEdge(e, 'right')" :class="[style.bg]" class="hidden group-hover:block absolute rounded-full border-2 border-light-70 dark:border-dark-70 w-6 h-6 -right-1.5 top-1/2 -translate-y-3"></span>
                </span> <!-- East -->
                <span @mousedown.left="(e) => resizeNode(e, 1, 0, -1, 0)" id="w " class="cursor-w-resize absolute -top-3 -bottom-3 -left-3 w-6 group">
                    <span @mousedown.left="(e) => dragEdge(e, 'left')" :class="[style.bg]" class="hidden group-hover:block absolute rounded-full border-2 border-light-70 dark:border-dark-70 w-6 h-6 -left-1.5 top-1/2 -translate-y-3"></span>
                </span> <!-- West -->
                <span @mousedown.left="(e) => resizeNode(e, 1, 1, -1, -1)" id="nw" class="cursor-nw-resize absolute -top-4 -left-4  w-8 h-8"></span> <!-- North West -->
                <span @mousedown.left="(e) => resizeNode(e, 0, 1, 1, -1)" id="ne" class="cursor-ne-resize absolute -top-4 -right-4 w-8 h-8"></span> <!-- North East -->
                <span @mousedown.left="(e) => resizeNode(e, 0,  0, 1, 1)" id="se" class="cursor-se-resize absolute -bottom-4 -right-4 w-8 h-8"></span> <!-- South East -->
                <span @mousedown.left="(e) => resizeNode(e, 1, 0, -1, 1)" id="sw" class="cursor-sw-resize absolute -bottom-4 -left-4  w-8 h-8"></span> <!-- South West -->
            </div>
        </div>
        <div v-else style="outline-style: solid;" :class="[style.border, style.outline, { '!outline-4': focusing }]" class="outline-0 transition-[outline-width] border-2 bg-light-20 dark:bg-dark-20 overflow-hidden contain-strict w-full h-full flex" >
            <Editor v-model="node.text" />
        </div>
        <div v-if="!editing && node.type === 'group' && node.label !== undefined"  @click.left="(e) => selectNode(e)" @dblclick.left="(e) => editNode(e)" :class="style.border" style="max-width: 100%; font-size: calc(18px * var(--zoom-multiplier))" class="origin-bottom-left tracking-wider border-4 truncate inline-block text-light-100 dark:text-dark-100 absolute bottom-[100%] mb-2 px-2 py-1 font-thin">{{ node.label }}</div>
        <input v-else-if="editing && node.type === 'group'" v-model="node.label" @click="e => e.stopImmediatePropagation()" autofocus :class="[style.border, style.outline]" style="max-width: 100%; font-size: calc(18px * var(--zoom-multiplier))" class="origin-bottom-left tracking-wider border-4 truncate inline-block text-light-100 dark:text-dark-100 absolute bottom-[100%] appearance-none bg-transparent outline-4 mb-2 px-2 py-1 font-thin min-w-4" />
    </div>
</template>

<script setup lang="ts">
import type { Direction } from '~/shared/canvas.util';
import FakeA from '../prose/FakeA.vue';
import type { CanvasNode } from '~/types/canvas';

const { node, index, zoom } = defineProps<{
    node: CanvasNode
    index: number
    zoom: number
}>();

const emit = defineEmits<{
    (e: 'select', index: number): void,
    (e: 'edit', index: number): void,
    (e: 'move', index: number, x: number, y: number): void,
    (e: 'resize', index: number, x: number, y: number, w: number, h: number): void,
}>();

const dom = useTemplateRef('dom');
const focusing = ref(false), editing = ref(false);

function selectNode(e: Event) {
    if(editing.value)
        return;

    focusing.value = true;
    emit('select', index);

    dom.value?.addEventListener('mousedown', dragstart, { passive: true });
}
function editNode(e: Event) {
    focusing.value = true;
    editing.value = true;

    e.stopImmediatePropagation();

    dom.value?.removeEventListener('mousedown', dragstart);
    emit('edit', index);
}
function resizeNode(e: MouseEvent, x: number, y: number, w: number, h: number) {
    e.stopImmediatePropagation();

    const startx = node.x, starty = node.y, startw = node.width, starth = node.height;
    const resizemove = (e: MouseEvent) => {
        if(e.button !== 0)
            return;

        node.x += (e.movementX / zoom) * x;
        node.y += (e.movementY / zoom) * y;
        node.width += (e.movementX / zoom) * w;
        node.height += (e.movementY / zoom) * h;
    };
    const resizeend = (e: MouseEvent) => {
        if(e.button !== 0)
            return;
        emit('resize', index, node.x - startx, node.y - starty, node.width - startw, node.height - starth);

        window.removeEventListener('mousemove', resizemove);
        window.removeEventListener('mouseup', resizeend);
    }

    window.addEventListener('mousemove', resizemove);
    window.addEventListener('mouseup', resizeend);
}
function dragEdge(e: Event, direction: Direction) {
    e.stopImmediatePropagation();
}
function unselect() {
    focusing.value = false;
    editing.value = false;

    dom.value?.removeEventListener('mousedown', dragstart);
}

let lastx = 0, lasty = 0;
const dragmove = (e: MouseEvent) => {
    if(e.button !== 0)
        return;

    node.x += e.movementX / zoom;
    node.y += e.movementY / zoom;
};
const dragend = (e: MouseEvent) => {
    if(e.button !== 0)
        return;
    
    window.removeEventListener('mousemove', dragmove);
    window.removeEventListener('mouseup', dragend);

    if(node.x - lastx !== 0 && node.y - lasty !== 0)
        emit('move', index, node.x - lastx, node.y - lasty);
};
const dragstart = (e: MouseEvent) => {
    if(e.button !== 0)
        return;
    
    lastx = node.x, lasty = node.y;

    window.addEventListener('mousemove', dragmove, { passive: true });
    window.addEventListener('mouseup', dragend, { passive: true });
};

defineExpose({ unselect, dom });

const style = computed(() => {
    return node.color ? node.color?.class ? 
        { bg: `bg-light-${node.color?.class} dark:bg-dark-${node.color?.class}`, border: `border-light-${node.color?.class} dark:border-dark-${node.color?.class}`, outline: `outline-light-${node.color?.class} dark:outline-dark-${node.color?.class}` } : 
        { bg: `bg-colored`, border: `border-[color:var(--canvas-color)]`, outline: `outline-[color:var(--canvas-color)]` } : 
        { border: `border-light-40 dark:border-dark-40`, bg: `bg-light-40 dark:bg-dark-40`, outline: `outline-light-40 dark:outline-dark-40` }
});
</script>