<template>
  <div class="cloud-folder-container pb-16 md:pb-16">
    <!-- Desktop Back Button -->
    <button @click="goBack" class="hidden md:flex mb-6 items-center gap-2 text-white/70 hover:text-white transition-colors">
      <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7" />
      </svg>
      Back to Cloud
    </button>

    <!-- Mobile Full-Width Back Button -->
    <button 
      @click="goBack"
      class="md:hidden fixed left-4 right-4 z-40 glass-button px-6 py-3 rounded-lg font-medium shadow-2xl flex items-center justify-center gap-2"
      :style="{
        bottom: bottomPosition,
        filter: 'drop-shadow(0 10px 25px rgba(0, 0, 0, 0.5))'
      }"
    >
      <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7" />
      </svg>
      <span>Back to Cloud</span>
    </button>

    <!-- Folder Header -->
    <div class="mb-8">
      <div class="flex items-center gap-4 mb-4">
        <div class="flex-shrink-0">
          <svg class="w-16 h-16 text-white/80" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path 
              v-for="(path, index) in getFolderIcon(folderType)" 
              :key="index"
              stroke-linecap="round" 
              stroke-linejoin="round" 
              stroke-width="2" 
              :d="path" 
            />
          </svg>
        </div>
        <div>
          <h1 class="text-3xl font-bold text-white mb-2">{{ folderName }}</h1>
          <p class="text-white/70">{{ items.length }} {{ items.length === 1 ? 'item' : 'items' }}</p>
        </div>
      </div>
    </div>

    <!-- Empty State -->
    <div v-if="items.length === 0" class="glass-card p-12 text-center">
      <svg class="w-24 h-24 text-white/20 mx-auto mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path 
          v-for="(path, index) in getFolderIcon(folderType)" 
          :key="index"
          stroke-linecap="round" 
          stroke-linejoin="round" 
          stroke-width="2" 
          :d="path" 
        />
      </svg>
      <h3 class="text-2xl font-semibold text-white mb-2">No items</h3>
      <p class="text-white/70">This folder is empty</p>
    </div>

    <!-- Items Grid -->
    <div v-else class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4">
      <div
        v-for="item in items"
        :key="item.id"
        class="glass-card p-4 cursor-pointer transition-all hover:-translate-y-1 hover:bg-white/10"
        @click="openItem(item)"
      >
        <div class="flex flex-col items-center text-center">
          <div class="mb-3">
            <svg class="w-12 h-12 text-white/60 mx-auto" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path 
                v-for="(path, index) in getItemIcon(folderType)" 
                :key="index"
                stroke-linecap="round" 
                stroke-linejoin="round" 
                stroke-width="2" 
                :d="path" 
              />
            </svg>
          </div>
          <h3 class="text-sm font-medium text-white mb-1 truncate w-full">
            {{ item.name }}
          </h3>
          <p class="text-xs text-white/60">
            {{ formatSize(item.size) }}
          </p>
        </div>
      </div>
    </div>

    <!-- Spacer for mobile back button -->
    <div class="md:hidden h-[calc(var(--mobile-tab-bar-height,_64px)+96px)]"></div>
  </div>
</template>

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

const { bottomPosition } = useMobileBackButton()

const router = useRouter()
const route = useRoute()

const folderId = computed(() => route.params.folderId as string)

const folderNames: Record<string, string> = {
  pictures: 'Pictures',
  videos: 'Videos',
  music: 'Music',
  documents: 'Documents',
  downloads: 'Downloads',
}

const folderName = computed(() => folderNames[folderId.value] || 'Folder')
const folderType = computed(() => folderId.value as string)

// Dummy items for each folder type
const items = ref<any[]>([])

function getFolderIcon(type: string): string[] {
  const icons: Record<string, string[]> = {
    pictures: ['M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z'],
    videos: ['M15 10l4.553-2.276A1 1 0 0121 8.618v6.764a1 1 0 01-1.447.894L15 14M5 18h8a2 2 0 002-2V8a2 2 0 00-2-2H5a2 2 0 00-2 2v8a2 2 0 002 2z'],
    music: ['M9 19V6l12-3v13M9 19c0 1.105-1.343 2-3 2s-3-.895-3-2 1.343-2 3-2 3 .895 3 2zm12-3c0 1.105-1.343 2-3 2s-3-.895-3-2 1.343-2 3-2 3 .895 3 2zM9 10l12-3'],
    documents: ['M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z'],
    downloads: ['M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4'],
  }
  return icons[type] || ['M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z']
}

function getItemIcon(type: string): string[] {
  const icons: Record<string, string[]> = {
    pictures: ['M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z'],
    videos: ['M15 10l4.553-2.276A1 1 0 0121 8.618v6.764a1 1 0 01-1.447.894L15 14M5 18h8a2 2 0 002-2V8a2 2 0 00-2-2H5a2 2 0 00-2 2v8a2 2 0 002 2z'],
    music: ['M9 19V6l12-3v13M9 19c0 1.105-1.343 2-3 2s-3-.895-3-2 1.343-2 3-2 3 .895 3 2zm12-3c0 1.105-1.343 2-3 2s-3-.895-3-2 1.343-2 3-2 3 .895 3 2zM9 10l12-3'],
    documents: ['M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z'],
    downloads: ['M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4'],
  }
  return icons[type] || ['M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z']
}

function formatSize(bytes: number): string {
  if (bytes === 0) return '0 B'
  const k = 1024
  const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))
  return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i]
}

function openItem(item: any) {
  console.log('Opening item:', item)
  // TODO: Implement item opening logic
}

function goBack() {
  router.push('/dashboard/cloud')
}

// Generate dummy items based on folder type
onMounted(() => {
  // For now, we'll leave items empty to show the empty state
  // In the future, this would fetch real items from the backend
  items.value = []
})
</script>

