<script setup lang="ts">
import type { DragState, InternalDragOptions } from '@vueuse/gesture';

definePageMeta({
    guestsGoesTo: '/user/login'
});

const { user, clear } = useUserSession();
const { data: projects } = useFetch(`/api/users/${user.value?.id}/projects`);
const { data: comments } = useFetch(`/api/users/${user.value?.id}/comments`);

const dialog = useTemplateRef('dialog'), bounds = useTemplateRef('bounds');
const files = ref<File[]>([]), imgURL = ref<string |null>(null);
const { top, left, update } = useElementBounding(bounds);

const posX = ref(0), posY = ref(0), size = ref(128);

async function loadImage(f: File[])
{
    files.value = f;
    if(f.length > 0)
    {
        imgURL.value = URL.createObjectURL(f[0]);
    }
    update();
}
async function approveThumbnail()
{

}
const dragHandler = ({ delta: [x, y] }: { delta: number[] }) => { 
    posX.value = clamp(posX.value + x, 0, 256 - size.value);
    posY.value = clamp(posY.value + y, 0, 256 - size.value);
};

const axisModifier: Record<string, (data: { values: number[]}) => void> = {
    'nw': ({values: [x, y]}: { values: number[]}): void => {
        x = clamp(x - left.value, 0, 256) - posX.value;
        y = clamp(y - top.value, 0, 256) - posY.value;
        size.value = clamp(Math.min(x, y), 0, 256 - Math.max(posX.value, posY.value));
    },
    'ne': ({values: [x, y]}: { values: number[]}): void => {
        x = clamp(x - left.value, 0, 256) - posX.value;
        y = clamp(y - top.value, 0, 256) - posY.value;
        size.value = clamp(Math.min(x, y), 0, 256 - Math.max(posX.value, posY.value));
    },
    'sw': ({values: [x, y]}: { values: number[]}): void => {
        x = clamp(x - left.value, 0, 256) - posX.value;
        y = clamp(y - top.value, 0, 256) - posY.value;
        size.value = clamp(Math.min(x, y), 0, 256 - Math.max(posX.value, posY.value));
    },
    'se': ({values: [x, y]}: { values: number[]}): void => {
        x = clamp(x - left.value, 0, 256) - posX.value;
        y = clamp(y - top.value, 0, 256) - posY.value;
        size.value = clamp(Math.min(x, y), 0, 256 - Math.max(posX.value, posY.value));
    },
};

const axisOptions: Partial<InternalDragOptions> = {
    bounds: [[0, 0], [256, 256]],
}
</script>

<template>
    <Head>
        <Title>Votre profil</Title>
    </Head>
    <div v-if="user" class="flex-1 grid gap-4 grid-cols-3">
        <div class="border border-light-35 dark:border-dark-35 p-4 flex gap-4 col-span-2">
            <Picture @click="() => dialog?.show()" :src="`/users/${user?.id}/normal.jpg`" :width=128 :height=128 class="inline-block hover:bg-light-70 hover:bg-opacity-50 hover:opacity-50 transition-all cursor-pointer">
                <Icon :icon="`icons/unknown`" :width=128 :height=128></Icon>
            </Picture>
            <Dialog :close-icon="true" :focused="false" ref="dialog">
                <div class="w-[640px] h-[320px] flex justify-center gap-4 items-center">
                    <div class="w-[256px] h-[256px] relative" ref="bounds">
                        <Picture :src="imgURL ?? undefined" :width=256 :height=256 class="inline-block select-none" :style="{ filter: `saturate(0.3) contrast(0.5)` }">
                            <Icon :icon="`icons/unknown`" :width=256 :height=256></Icon>
                        </Picture>
                        <div class="z-[3] absolute top-0 bottom-0 left-0 right-0" v-if="imgURL">
                            <Picture :src="imgURL" :width=256 :height=256 :style="{
                                clipPath: `polygon(${posX}px ${posY}px, ${posX}px ${posY + size}px, ${posX + size}px ${posY + size}px, ${posX + size}px ${posY}px)`,
                            }"></Picture>
                            <div class="border border-dashed border-light-20 dark:border-dark-20 absolute cursor-move" :style="{
                                top: `${posY}px`,
                                left: `${posX}px`,
                                width: `${size}px`,
                                height: `${size}px`,
                            }" v-drag="dragHandler">
                            </div>
                            <span :drag-options="axisOptions" v-drag="axisModifier['nw']" class="z-10 cursor-nw-resize absolute border-4 border-light-100 dark:border-dark-100" :style="{ top: `${posY}px`, left: `${posX}px`, }"></span>
                            <span :drag-options="axisOptions" v-drag="axisModifier['ne']" class="z-10 cursor-ne-resize absolute border-4 border-light-100 dark:border-dark-100" :style="{ top: `${posY + size - 8}px`, left: `${posX}px`, }"></span>
                            <span :drag-options="axisOptions" v-drag="axisModifier['sw']" class="z-10 cursor-sw-resize absolute border-4 border-light-100 dark:border-dark-100" :style="{ top: `${posY}px`, left: `${posX + size - 8}px`, }"></span>
                            <span :drag-options="axisOptions" v-drag="axisModifier['se']" class="z-10 cursor-se-resize absolute border-4 border-light-100 dark:border-dark-100" :style="{ top: `${posY + size - 8}px`, left: `${posX + size - 8}px`, }"></span>
                        </div>
                    </div>
                    <span v-if="imgURL" class="text-light-100 dark:text-dark-100">></span>
                    <div v-if="imgURL" class="relative bg-light-red dark:bg-dark-red bg-no-repeat w-32 h-32" :style="{backgroundImage: `url(${imgURL})`, backgroundPosition: `left -${posX + (posX + size)}px top -${posY + (256 / size)}px`, backgroundSize: `${256 / size * 100}%`}"></div>
                </div>
                <div class="border-t border-light-35 dark:border-dark-35 absolute bottom-0 left-0 right-0 p-4 flex justify-between">
                    <div class="flex gap-3">
                        <Upload accept="image/png,image/jpeg,image/webp" @change="loadImage">
                            <span class="py-1 px-2 cursor-pointer text-light-100 dark:text-dark-100 transition-all duration-200 mb-4 border border-light-35 dark:border-dark-35 bg-light-20 dark:bg-dark-20 hover:border-light-50 dark:hover:border-dark-50 hover:bg-light-30 dark:hover:bg-dark-30">Importer un fichier</span>
                        </Upload>
                        <span class="text-sm text-light-60 dark:text-dark-60 italic">5 Mo max. 128 x 128 min recommandé.</span>
                    </div>
                    <div>
                        <span @click="approveThumbnail" class="py-1 px-2 cursor-pointer text-light-100 dark:text-dark-100 transition-all duration-200 mb-4 border border-light-35 dark:border-dark-35 bg-light-20 dark:bg-dark-20 hover:border-light-50 dark:hover:border-dark-50 hover:bg-light-30 dark:hover:bg-dark-30">Valider</span>
                    </div>
                </div>
            </Dialog>
            <div class="flex flex-col">
                <span class="text-xl font-semibold">{{ user?.username }}</span>
                <span>Inscrit depuis le {{ format(new Date(user.signin_timestamp), 'dd/MM/yyyy') }}</span>
            </div>
        </div>
        <div class="p-4 flex gap-4 flex-col">
            <button @click="clear()" class="py-1 transition-all duration-200 mb-4 border border-light-35 dark:border-dark-35 bg-light-20 dark:bg-dark-20 hover:border-light-50 dark:hover:border-dark-50 hover:bg-light-30 dark:hover:bg-dark-30">Se déconnecter</button>
            <button @click="" class="py-1 transition-all duration-200 border border-light-35 dark:border-dark-35 bg-light-20 dark:bg-dark-20 hover:border-light-50 dark:hover:border-dark-50 hover:bg-light-30 dark:hover:bg-dark-30">Mettre à jour le profil</button>
            <button @click="" class="py-1 transition-all duration-200 border border-light-35 dark:border-dark-35 bg-light-20 dark:bg-dark-20 hover:border-light-red dark:hover:border-dark-red hover:bg-light-30 dark:hover:bg-dark-30 text-light-red dark:text-dark-red">Supprimer le compte</button>
        </div>
        <div class="border border-light-35 dark:border-dark-35 p-4 flex flex-col">
            <div class="text-2xl">Mes projets</div>
            <div class="flex flex-col">
                <div v-for="project of projects" class="flex justify-between items-center py-1">
                    <NuxtLink :to="{ path: `/explorer/${project.id}/${project.home}`, force: true }" class="text-accent-blue font-bold cursor-pointer hover:text-opacity-80 text-lg">{{ project.name }}</NuxtLink>
                    <span>{{ project.pages }} pages</span>
                </div>
            </div>
        </div>
    </div>
</template>