<script setup>
import { computed, onMounted, ref, watch } from 'vue'
import { useWindowSize } from '@vueuse/core'
import { usePhotoboothStore } from '../stores/photoboothStore'
import { DEFAULT_STRIP_THEME_ID, STRIP_THEME_MAP } from '../data/stripThemes'
import PhotoStrip from '../components/PhotoStrip.vue'
import ThemeSelector from '../components/ThemeSelector.vue'
import ImageEditor from '../components/ImageEditor.vue'

const store = usePhotoboothStore()
const { width } = useWindowSize()

const stripSize = computed(() => {
  const theme = STRIP_THEME_MAP[store.stripTheme] || STRIP_THEME_MAP[DEFAULT_STRIP_THEME_ID]
  return theme.size
})

const stripScale = computed(() =>
  Math.min(1, Math.max(0.45, (width.value - 48) / stripSize.value.width)),
)
const stripReady = computed(() => store.photos.length >= 3)

// local preview copy (non-persistent) used for manual placement prototype
const previewPhotos = ref([...store.photos])
const editorIndex = ref(-1)

watch(
  () => store.photos,
  (v) => {
    previewPhotos.value = [...v]
  },
  { deep: true },
)

onMounted(() => {
  store.setStep(2)
})

function handleCancel() {
  editorIndex.value = -1
}

function handleApply(payload) {
  // payload: { dataUrl, transform }
  const { dataUrl, transform } = payload
  if (editorIndex.value >= 0) {
    const original = (store.photos[editorIndex.value] && (store.photos[editorIndex.value].src || store.photos[editorIndex.value])) || (previewPhotos.value[editorIndex.value] && (previewPhotos.value[editorIndex.value].src || previewPhotos.value[editorIndex.value])) || null
    const obj = { src: original, preview: dataUrl, transform }
    previewPhotos.value[editorIndex.value] = obj
    // persist to store as well
    if (store.photos[editorIndex.value]) {
      store.photos[editorIndex.value] = obj
    }
  }
  editorIndex.value = -1
}
</script>

<template>
  <main class="customize">
    <section class="strip-panel">
      <div class="strip-wrapper" :style="{ transform: `scale(${stripScale})` }">
        <PhotoStrip
          v-motion
          :initial="{ opacity: 0, y: 40 }"
          :enter="{ opacity: 1, y: 0, transition: { duration: 0.5 } }"
          :photos="previewPhotos"
          :theme="store.stripTheme"
        />
      </div>
      <div class="edit-thumbs">
        <h3>Edit photos</h3>
        <div class="thumbs-row">
          <div v-for="(p, i) in previewPhotos" :key="i" class="edit-thumb">
            <img v-if="p" :src="p.preview || p.src || p" />
            <button @click="editorIndex = i">Edit</button>
          </div>
        </div>
      </div>
      <p v-if="!stripReady" class="hint">Capture three shots to fill the strip.</p>
    </section>

    <section class="options-panel">
      <ThemeSelector />
      <RouterLink class="primary" to="/result">Generate strip</RouterLink>
    </section>
    <ImageEditor v-if="editorIndex >= 0" :src="(store.photos[editorIndex] && (store.photos[editorIndex].src || store.photos[editorIndex])) || previewPhotos[editorIndex]" :initialTransform="(store.photos[editorIndex] && store.photos[editorIndex].transform) || null" :outputWidth="440" :outputHeight="290" @cancel="handleCancel" @apply="handleApply" />
  </main>
</template>

<style scoped>
.customize {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(0, 1fr);
  gap: 28px;
  align-items: start;
}

.strip-panel {
  padding: 28px;
  border-radius: 32px;
  background: rgba(255, 255, 255, 0.85);
  box-shadow: var(--soft-shadow);
  display: grid;
  gap: 16px;
  justify-items: center;
}

.strip-wrapper {
  transform-origin: top center;
}

.hint {
  margin: 0;
  color: var(--muted);
}

.edit-thumbs {
  width: 100%;
}
.thumbs-row {
  display: flex;
  gap: 8px;
  align-items: center;
}
.edit-thumb {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}
.edit-thumb img { width: 84px; height: 64px; object-fit: cover; border-radius: 8px }

.options-panel {
  display: grid;
  gap: 16px;
}

.primary {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  padding: 12px 20px;
  border-radius: 999px;
  background: #c85a7c;
  color: #fff;
  font-weight: 600;
  box-shadow: 0 16px 30px rgba(200, 90, 124, 0.3);
}

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