<script setup>
import { inject } from 'vue';
import { useImageUpload } from '../../composables/useImageUpload';

const props = defineProps({
    block: {
        type: Object,
        required: true,
    },
    activeLang: {
        type: String,
        default: '',
    },
});

const resolveAlt = (alt, fallback) => {
    if (!alt) return fallback;
    if (typeof alt === 'string') return alt || fallback;
    return alt[props.activeLang] || alt[Object.keys(alt)[0]] || fallback;
};

const uploadUrl = inject('editorUploadUrl', null);
const updateBlockImage = inject('editorUpdateBlockImage', null);

const { isDragOver: isDragOver0, isUploading: isUploading0, fileInputRef: fileInputRef0, onDragOver: onDragOver0, onDragLeave: onDragLeave0, onDrop: onDrop0, openPicker: openPicker0, onFileSelected: onFileSelected0 } = useImageUpload(
    () => uploadUrl?.value ?? null,
    (url) => updateBlockImage?.(props.block.id, 'images', 0, url),
);

const { isDragOver: isDragOver1, isUploading: isUploading1, fileInputRef: fileInputRef1, onDragOver: onDragOver1, onDragLeave: onDragLeave1, onDrop: onDrop1, openPicker: openPicker1, onFileSelected: onFileSelected1 } = useImageUpload(
    () => uploadUrl?.value ?? null,
    (url) => updateBlockImage?.(props.block.id, 'images', 1, url),
);

const dragHandlers = [
    { isDragOver: isDragOver0, isUploading: isUploading0, fileInputRef: fileInputRef0, onDragOver: onDragOver0, onDragLeave: onDragLeave0, onDrop: onDrop0, openPicker: openPicker0, onFileSelected: onFileSelected0 },
    { isDragOver: isDragOver1, isUploading: isUploading1, fileInputRef: fileInputRef1, onDragOver: onDragOver1, onDragLeave: onDragLeave1, onDrop: onDrop1, openPicker: openPicker1, onFileSelected: onFileSelected1 },
];
</script>

<template>
    <section class="block-images-split">
        <figure
            v-for="(image, index) in block.images"
            :key="`${block.id}-${index}`"
            :class="{
                'block-images-split__figure--over': dragHandlers[index]?.isDragOver.value,
                'block-images-split__figure--uploading': dragHandlers[index]?.isUploading.value,
                'block-images-split__figure--editable': !!uploadUrl?.value,
            }"
            @dragover="uploadUrl?.value ? dragHandlers[index]?.onDragOver($event) : null"
            @dragleave="dragHandlers[index]?.onDragLeave()"
            @drop="uploadUrl?.value ? dragHandlers[index]?.onDrop($event) : null"
            @click="uploadUrl?.value ? dragHandlers[index]?.openPicker() : null"
        >
            <img v-if="image?.url" :src="image.url" :alt="resolveAlt(image.alt, `Project image ${index + 1}`)" loading="lazy">
            <div v-else class="block-images-split__placeholder">
                {{ dragHandlers[index]?.isUploading.value ? 'Uploading…' : (uploadUrl?.value ? 'Drop or click to upload.' : `Drop in image URL ${index + 1}`) }}
            </div>
            <div v-if="dragHandlers[index]?.isDragOver.value" class="block-images-split__overlay">Drop to upload</div>
            <div v-if="dragHandlers[index]?.isUploading.value && image?.url" class="block-images-split__overlay block-images-split__overlay--uploading">Uploading…</div>
            <input :ref="dragHandlers[index]?.fileInputRef" type="file" accept="image/*" style="display:none" @change="dragHandlers[index]?.onFileSelected($event)">
        </figure>
    </section>
</template>

<style scoped>
.block-images-split {
    display: grid;
    gap: 1.4rem;
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

figure {
    aspect-ratio: 4 / 4;
    background: linear-gradient(135deg, rgba(15, 23, 42, 0.08), rgba(148, 163, 184, 0.15));
    border-radius: 1.5rem;
    margin: 0;
    overflow: hidden;
    position: relative;
    transition: outline-color 0.15s;
}

.block-images-split__figure--editable {
    cursor: pointer;
}

.block-images-split__figure--over {
    outline: 2px dashed rgba(14, 116, 144, 0.7);
    outline-offset: -2px;
}

img {
    display: block;
    height: 100%;
    object-fit: cover;
    width: 100%;
}

.block-images-split__figure--uploading img {
    opacity: 0.5;
}

.block-images-split__placeholder {
    align-items: center;
    color: var(--project-muted, #6b7280);
    display: flex;
    height: 100%;
    justify-content: center;
    padding: 2rem;
    text-align: center;
}

.block-images-split__overlay {
    align-items: center;
    background: rgba(14, 116, 144, 0.55);
    bottom: 0;
    color: #fff;
    display: flex;
    font-size: 0.9rem;
    font-weight: 700;
    justify-content: center;
    left: 0;
    pointer-events: none;
    position: absolute;
    right: 0;
    top: 0;
}

.block-images-split__overlay--uploading {
    background: rgba(15, 23, 42, 0.4);
}

@media (max-width: 900px) {
    .block-images-split {
        grid-template-columns: 1fr;
    }
}
</style>