<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()
const imageParam = ref('')
const hasError = ref(false)
const isDownloading = ref(false)
const downloadSuccess = ref(false)

const imageUrl = computed(() => {
  if (imageParam.value) {
    return `https://assets.dekthai-online.com/static/photobooth/${imageParam.value}`
  }
  return ''
})

const downloadImage = async () => {
  if (!imageUrl.value || isDownloading.value) return

  isDownloading.value = true
  downloadSuccess.value = false

  try {
    // Create a temporary link element for download
    const link = document.createElement('a')
    link.href = imageUrl.value
    link.download = imageParam.value || 'photobooth-image.jpg'
    link.style.display = 'none'

    // Add to DOM, click, and remove
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)

    // Show success message
    downloadSuccess.value = true

    // Hide success message after 3 seconds
    setTimeout(() => {
      downloadSuccess.value = false
    }, 3000)

  } catch (error) {
    console.error('Download failed:', error)
    hasError.value = true
  } finally {
    isDownloading.value = false
  }
}

const startOver = () => {
  // เคลียร์ข้อมูลและกลับไปหน้าแรก
  localStorage.removeItem('photobooth-photos')
  localStorage.removeItem('photobooth-layout')
  localStorage.removeItem('photobooth-frame')
  localStorage.removeItem('photobooth-uploaded-filename')
  router.push('/')
}

onMounted(() => {
  // Get image parameter from URL query
  const image = route.query.image as string
  if (image) {
    imageParam.value = image
  } else {
    hasError.value = true
  }
})
</script>

<template>
  <div class="download-container">
    <header class="header">
      <h1>ดาวน์โหลดรูปภาพ</h1>
      <p>รูปภาพของคุณพร้อมให้ดาวน์โหลด</p>
    </header>

    <section v-if="!hasError && imageUrl" class="download-section">
        <div class="download-actions">

        <button @click="downloadImage" :disabled="isDownloading" class="start-over-button">
          <span v-if="isDownloading" class="button-icon">⏳</span>
          <span v-else class="button-icon">📥</span>
          {{ isDownloading ? 'กำลังดาวน์โหลด...' : 'ดาวน์โหลดรูปภาพ' }}
        </button>
        &emsp;
        <button @click="startOver" class="start-over-button">
          เริ่มใหม่
        </button>
        
        <div v-if="downloadSuccess" class="success-message">
          ✅ ดาวน์โหลดสำเร็จ! ไฟล์ได้บันทึกในเครื่องของคุณแล้ว
        </div>
      </div>

      <div class="additional-actions">
        
      </div>

      <div class="image-display">
        <img :src="imageUrl" alt="Photo for download" class="download-image" @error="hasError = true" />
      </div>


    </section>

    <section v-else class="error-section">
      <div class="error-message">
        <h2>ไม่พบรูปภาพ</h2>
        <p>ไม่สามารถโหลดรูปภาพได้ กรุณาตรวจสอบลิงก์อีกครั้ง</p>
      </div>
    </section>
  </div>
</template>

<style scoped>
@import url('https://fonts.googleapis.com/css2?family=Crete+Round&family=Old+Standard+TT&display=swap');

.download-container {
  min-height: 100vh;
  padding: 1.5rem;
  background: #fdfaf6;
  color: #3e2723;
  font-family: 'Old Standard TT', serif;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.header {
  text-align: center;
  margin-bottom: 2.5rem;
}

.header h1 {
  font-size: 2.5rem;
  color: #5d4037;
  margin-bottom: 0.5rem;
  font-family: 'Crete Round', serif;
  text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
}

.header p {
  color: #6d4c41;
  font-size: 1.1rem;
}

.download-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2rem;
}

.image-display {
  background: #e0d8cf;
  padding: 15px;
  border-radius: 8px;
  box-shadow: 0 5px 20px rgba(0,0,0,0.15);
  border: 4px solid #a1887f;
}

.download-image {
  max-width: 100%;
  height: auto;
  border-radius: 4px;
  display: block;
  filter: sepia(0.1) brightness(0.95);
  max-height: 70vh;
}

.download-actions {
  text-align: center;
}

.download-button {
  background: #8d6e63;
  color: #fdfaf6;
  border: 2px solid #795548;
  padding: 1rem 2.5rem;
  font-size: 1.2rem;
  font-weight: bold;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: inline-flex;
  align-items: center;
  gap: 0.75rem;
  box-shadow: 0 4px 10px rgba(0,0,0,0.2);
  font-family: 'Crete Round', serif;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.download-button:hover {
  background: #795548;
  transform: translateY(-2px);
  box-shadow: 0 6px 15px rgba(0,0,0,0.3);
}

.button-icon {
  font-size: 1.5rem;
}

.error-section {
  text-align: center;
  background: #e0d8cf;
  padding: 2rem;
  border-radius: 12px;
  box-shadow: 0 5px 20px rgba(0,0,0,0.15);
  border: 2px solid #bcaaa4;
  max-width: 500px;
}

.error-message h2 {
  color: #d32f2f;
  font-size: 2rem;
  margin-bottom: 1rem;
  font-family: 'Crete Round', serif;
}

.error-message p {
  color: #6d4c41;
  font-size: 1.1rem;
}

.download-button:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  transform: none;
}

.download-button:disabled:hover {
  transform: none;
  box-shadow: 0 4px 10px rgba(0,0,0,0.2);
}

.success-message {
  margin-top: 1rem;
  padding: 1rem;
  background: #d4edda;
  color: #155724;
  border: 1px solid #c3e6cb;
  border-radius: 8px;
  text-align: center;
  font-weight: bold;
  animation: fadeIn 0.5s ease-in-out;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

.download-actions {
  text-align: center;
}

.additional-actions {
  margin-top: 2rem;
  text-align: center;
}

.start-over-button {
  background: #6d4c41;
  color: #fdfaf6;
  border: 1px solid #5d4037;
  padding: 0.8rem 1.8rem;
  font-size: 1rem;
  font-weight: bold;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
  font-family: 'Old Standard TT', serif;
}

.start-over-button:hover {
  background: #5d4037;
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(0,0,0,0.15);
}

@media (max-width: 768px) {
  .download-image {
    max-height: 50vh;
  }

  .download-button {
    width: 280px;
    justify-content: center;
    font-size: 1.1rem;
    padding: 0.8rem 1.5rem;
  }

  .button-icon {
    font-size: 1.3rem;
  }

  .header h1 {
    font-size: 2rem;
  }

  .error-message h2 {
    font-size: 1.5rem;
  }
}
</style>