<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount, nextTick, computed } from 'vue'
import { getCachedImageUrl } from '@/common/imageCache'
import { maybeShow as tutorialMaybeShow } from '@/tutorial/controller'
import '@/assets/styles.css'

const props = withDefaults(
  defineProps<{
    modelValue?: string | null // selected image id or local-upload
    imageType?: number // 1 or 2, default 1
    disabled?: boolean
  }>(),
  {
    disabled: false,
  },
)
const emit = defineEmits(['update:modelValue', 'add-image'])

const fileInput = ref<HTMLInputElement | null>(null)
const imageScrollRef = ref<HTMLDivElement | null>(null)
const addPhotoBtn = ref<HTMLButtonElement | null>(null)
const cameraBtn = ref<HTMLButtonElement | null>(null)
const localImageUrl = ref<string | null>(null)
const showCamera = ref(false)
const cameraStream = ref<MediaStream | null>(null)
const cameraVideo = ref<HTMLVideoElement | null>(null)
const cameraError = ref<string | null>(null)
const capturedImageUrl = ref<string | null>(null)
const cameraFile = ref<File | null>(null)

const availableImages = ref<{ id: string; url: string }[]>([])
const loadingImages = ref(false)

const typeParam = computed(() => props.imageType ?? 1)

const selectImage = (id: string | undefined) => {
  if (props.disabled) return
  if (!id) {
    console.warn('selectImage called with null id')
    return
  }
  emit('update:modelValue', id)
}

const addFromLocal = () => {
  if (props.disabled) return
  fileInput.value?.click()
}

const onFileChange = async (event: Event) => {
  if (props.disabled) return
  const files = (event.target as HTMLInputElement).files
  if (!files || files.length === 0) return
  const file = files[0]
  if (!file) return
  if (localImageUrl.value) URL.revokeObjectURL(localImageUrl.value)
  const { blob, url } = await resizeImageFile(file, 512)
  localImageUrl.value = url
  updateLocalImage(url, new File([blob], file.name, { type: 'image/png' }))
}

onBeforeUnmount(() => {
  if (localImageUrl.value) URL.revokeObjectURL(localImageUrl.value)
})

const addFromCamera = async () => {
  if (props.disabled) return
  cameraError.value = null
  capturedImageUrl.value = null
  showCamera.value = true
  await nextTick()
  try {
    const stream = await navigator.mediaDevices.getUserMedia({ video: true })
    cameraStream.value = stream
    if (cameraVideo.value) {
      cameraVideo.value.srcObject = stream
      await cameraVideo.value.play()
    }
  } catch (err) {
    cameraError.value = 'Unable to access camera'
    cameraStream.value = null
  }
}

const takePhoto = async () => {
  if (!cameraVideo.value) return
  const canvas = document.createElement('canvas')
  canvas.width = cameraVideo.value.videoWidth
  canvas.height = cameraVideo.value.videoHeight
  const ctx = canvas.getContext('2d')
  if (ctx) {
    ctx.drawImage(cameraVideo.value, 0, 0, canvas.width, canvas.height)
    const dataUrl = canvas.toDataURL('image/png')
    capturedImageUrl.value = dataUrl
  }
}

const confirmPhoto = async () => {
  if (capturedImageUrl.value) {
    if (localImageUrl.value) URL.revokeObjectURL(localImageUrl.value)
    // Convert dataURL to Blob
    const res = await fetch(capturedImageUrl.value)
    const originalBlob = await res.blob()
    const { blob, url } = await resizeImageFile(originalBlob, 512)
    localImageUrl.value = url
    cameraFile.value = new File([blob], 'camera.png', { type: 'image/png' })
    updateLocalImage(url, cameraFile.value)
  }
  closeCamera()
}

const retakePhoto = async () => {
  capturedImageUrl.value = null
  cameraFile.value = null
  await resumeCameraStream()
}

const closeCamera = () => {
  showCamera.value = false
  capturedImageUrl.value = null
  if (cameraStream.value) {
    cameraStream.value.getTracks().forEach((track) => track.stop())
    cameraStream.value = null
  }
  if (cameraVideo.value) {
    cameraVideo.value.srcObject = null
  }
}

const resumeCameraStream = async () => {
  await nextTick()
  if (cameraVideo.value && cameraStream.value) {
    cameraVideo.value.srcObject = cameraStream.value
    try {
      await cameraVideo.value.play()
    } catch (e) {}
  }
}

// Fetch images on mount
onMounted(async () => {
  loadingImages.value = true
  try {
    const resp = await fetch(`/api/image/list?type=${typeParam.value}`)
    if (resp.ok) {
      const data = await resp.json()
      const ids = data.ids || []
      // Fetch URLs for each image id using the cache
      const urls = await Promise.all(
        ids.map(async (id: string) => {
          try {
            const url = await getCachedImageUrl(id)
            return { id, url }
          } catch {
            return null
          }
        }),
      )
      const images = urls.filter(Boolean) as { id: string; url: string }[]
      // Move the selected image to the front if it exists
      if (props.modelValue) {
        const idx = images.findIndex((img) => img.id === props.modelValue)
        if (idx > 0) {
          const [selected] = images.splice(idx, 1)
          if (selected) {
            images.unshift(selected)
          }
        }
      }
      availableImages.value = images
    }
  } catch (err) {
    // Optionally handle error
  } finally {
    loadingImages.value = false
  }
})

async function resizeImageFile(
  file: File | Blob,
  maxDim = 512,
): Promise<{ blob: Blob; url: string }> {
  const img = new window.Image()
  const url = URL.createObjectURL(file)
  img.src = url
  await new Promise((resolve) => {
    img.onload = resolve
  })

  let { width, height } = img
  if (width > maxDim || height > maxDim) {
    if (width > height) {
      height = Math.round((height * maxDim) / width)
      width = maxDim
    } else {
      width = Math.round((width * maxDim) / height)
      height = maxDim
    }
  }
  const canvas = document.createElement('canvas')
  canvas.width = width
  canvas.height = height
  const ctx = canvas.getContext('2d')
  ctx?.drawImage(img, 0, 0, width, height)
  const blob: Blob = await new Promise((resolve) => canvas.toBlob((b) => resolve(b!), 'image/png'))
  URL.revokeObjectURL(url)
  return { blob, url: URL.createObjectURL(blob) }
}

function updateLocalImage(url: string, file: File) {
  const idx = availableImages.value.findIndex((img) => img.id === 'local-upload')
  if (idx === -1) {
    availableImages.value.unshift({ id: 'local-upload', url })
  } else if (availableImages.value[idx]) {
    availableImages.value[idx]!.url = url
  }

  nextTick(() => {
    if (imageScrollRef.value) {
      imageScrollRef.value.scrollLeft = 0
    }
  })

  emit('add-image', { id: 'local-upload', url, file })
  emit('update:modelValue', 'local-upload')
}
</script>

<template>
  <div class="picker">
    <div ref="imageScrollRef" class="image-scroll">
      <div v-if="loadingImages" class="loading-images">Loading images...</div>
      <div v-else class="image-list">
        <img
          v-for="img in availableImages"
          :key="img.id"
          :src="img.url"
          class="selectable-image"
          :class="{ selected: modelValue === img.id, disabled: props.disabled }"
          :alt="`Image ${img.id}`"
          @click="selectImage(img.id)"
        />
      </div>
    </div>
    <input
      ref="fileInput"
      type="file"
      accept=".png,.jpg,.jpeg,.gif,image/png,image/jpeg,image/gif"
      style="display: none"
      tabindex="-1"
      @change="onFileChange"
    />
    <div class="image-actions">
      <button ref="addPhotoBtn" type="button" class="icon-btn" :disabled="props.disabled" @click="addFromLocal" aria-label="Add from device">
        <span class="icon">＋</span>
      </button>
      <button ref="cameraBtn" type="button" class="icon-btn" :disabled="props.disabled" @click="addFromCamera" aria-label="Add from camera">
        <span class="icon">
          <svg width="20" height="20" viewBox="0 0 20 20" fill="none">
            <rect x="3" y="6" width="14" height="10" rx="2" stroke="#667eea" stroke-width="1.5" />
            <circle cx="10" cy="11" r="3" stroke="#667eea" stroke-width="1.5" />
            <rect x="7" y="3" width="6" height="3" rx="1" stroke="#667eea" stroke-width="1.5" />
          </svg>
        </span>
      </button>
    </div>

    <!-- Camera modal -->
    <div v-if="showCamera" class="modal-backdrop">
      <div class="modal camera-modal">
        <h3>Take a photo</h3>
        <div v-if="cameraError" class="error">{{ cameraError }}</div>
        <div v-else>
          <div v-if="!capturedImageUrl">
            <video ref="cameraVideo" autoplay playsinline class="camera-display"></video>
            <div class="actions">
              <button type="button" class="btn btn-primary" @click="takePhoto">Capture</button>
              <button type="button" class="btn btn-secondary" @click="closeCamera">Cancel</button>
            </div>
          </div>
          <div v-else>
            <img :src="capturedImageUrl" class="camera-display" alt="Preview" />
            <div class="actions">
              <button type="button" class="btn btn-primary" @click="confirmPhoto">Choose</button>
              <button type="button" class="btn btn-secondary" @click="retakePhoto">Retake</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.picker {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
}

.image-scroll {
  width: 100%;
  margin: 0.7rem 0 0.2rem 0;
  overflow-x: auto;
  overflow-y: hidden;
  padding-bottom: 0.2rem;
}
.image-list {
  display: flex;
  gap: 0.7rem;
  min-width: min-content;
  align-items: center;
}
.selectable-image {
  width: 64px;
  height: 64px;
  object-fit: cover;
  border-radius: 8px;
  border: 2px solid var(--selectable-image-border);
  background: var(--selectable-image-bg);
  cursor: pointer;
  transition: border 0.18s;
}
.selectable-image:hover,
.selectable-image.selected {
  border-color: var(--selectable-image-selected);
  box-shadow: 0 0 0 2px #667eea55;
}
.selectable-image.disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.loading-images {
  color: var(--loading-text);
  font-size: 0.98rem;
  padding: 0.5rem 0;
  text-align: center;
}

.image-actions {
  display: flex;
  gap: 4rem;
  justify-content: center;
  margin-top: 1.2rem; /* Increased space below images */
}

.icon-btn {
  background: var(--icon-btn-bg);
  border: none;
  border-radius: 50%;
  width: 56px; /* Increased size */
  height: 56px; /* Increased size */
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.18s;
  font-size: 2.2rem; /* Bigger + icon */
  color: var(--icon-btn-color);
  box-shadow: var(--icon-btn-shadow);
}
.icon-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.icon-btn svg {
  width: 32px; /* Bigger camera icon */
  height: 32px;
}

.icon {
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Camera modal styles */
.camera-modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: var(--modal-bg);
  border-radius: 12px;
  box-shadow: var(--modal-shadow);
  z-index: 1300;
  width: 380px;
  max-width: calc(100vw - 32px);
  padding-bottom: 1.5rem;
  text-align: center;
}

.camera-display {
  width: auto;
  max-width: 100%;
  max-height: 240px;
  border-radius: 12px;
  display: block;
  margin-left: auto;
  margin-right: auto;
  object-fit: contain;
}

.error {
  color: var(--error);
  margin-top: 0.7rem;
  text-align: center;
  background: var(--error-bg);
  border-radius: 8px;
  padding: 1rem;
}

.actions {
  display: flex;
  gap: 3rem;
  justify-content: center;
  margin-top: 0.5rem;
  margin-bottom: 0.4rem;
}
.actions .btn {
  padding: 1rem 2.2rem;
  font-weight: 700;
  font-size: 1.25rem;
  min-width: 120px;
}
</style>
