/**
 * 文件列表 Composable
 * 负责文件对的导航和加载
 */

import { computed } from 'vue'
import { useTemplateStore } from '@/stores'

export function useFileList() {
  const templateStore = useTemplateStore()
  
  // 计算属性
  const filePairs = computed(() => templateStore.filePairs)
  const hasFilePairs = computed(() => templateStore.hasFilePairs)
  const currentFilePair = computed(() => templateStore.currentFilePair)
  const currentPairIndex = computed(() => templateStore.currentPairIndex)
  const totalPairs = computed(() => templateStore.filePairs.length)
  
  const labeledCount = computed(() => templateStore.labeledCount)
  const unlabeledCount = computed(() => templateStore.unlabeledCount)
  
  const hasNextPair = computed(() => templateStore.hasNextPair)
  const hasPrevPair = computed(() => currentPairIndex.value > 0)
  
  const isFirstPair = computed(() => currentPairIndex.value === 0)
  const isLastPair = computed(() => currentPairIndex.value >= totalPairs.value - 1)
  
  /**
   * 跳转到下一个文件对
   */
  function nextPair() {
    if (hasNextPair.value) {
      templateStore.nextPair()
      return true
    }
    return false
  }
  
  /**
   * 跳转到上一个文件对
   */
  function prevPair() {
    if (hasPrevPair.value) {
      templateStore.prevPair()
      return true
    }
    return false
  }
  
  /**
   * 跳转到指定索引的文件对
   */
  function goToPair(index: number) {
    if (index >= 0 && index < totalPairs.value) {
      while (currentPairIndex.value < index) {
        templateStore.nextPair()
      }
      while (currentPairIndex.value > index) {
        templateStore.prevPair()
      }
      return true
    }
    return false
  }
  
  /**
   * 跳转到第一个未标注的文件
   */
  function goToFirstUnlabeled() {
    const index = filePairs.value.findIndex(pair => !pair.has_structure)
    if (index !== -1) {
      return goToPair(index)
    }
    return false
  }
  
  /**
   * 跳转到最后一个文件
   */
  function goToLastPair() {
    return goToPair(totalPairs.value - 1)
  }
  
  /**
   * 跳转到第一个文件
   */
  function goToFirstPair() {
    return goToPair(0)
  }
  
  /**
   * 获取当前文件的加载参数
   */
  function getCurrentFileParams() {
    const pair = currentFilePair.value
    if (pair) {
      return {
        imagePath: pair.image_path,
        jsonPath: pair.json_path,
        outputDir: templateStore.scanConfig.outputDir
      }
    }
    return null
  }
  
  /**
   * 更新当前文件的标注状态
   */
  function updateCurrentPairStatus(hasStructure: boolean, structurePath: string | null) {
    templateStore.updatePairStatus(currentPairIndex.value, hasStructure, structurePath)
  }
  
  /**
   * 获取文件统计信息
   */
  function getFileStats() {
    return {
      total: totalPairs.value,
      labeled: labeledCount.value,
      unlabeled: unlabeledCount.value,
      current: currentPairIndex.value + 1,
      percentage: totalPairs.value > 0 
        ? Math.round((labeledCount.value / totalPairs.value) * 100) 
        : 0
    }
  }
  
  return {
    // 计算属性
    filePairs,
    hasFilePairs,
    currentFilePair,
    currentPairIndex,
    totalPairs,
    labeledCount,
    unlabeledCount,
    hasNextPair,
    hasPrevPair,
    isFirstPair,
    isLastPair,
    
    // 方法
    nextPair,
    prevPair,
    goToPair,
    goToFirstUnlabeled,
    goToLastPair,
    goToFirstPair,
    getCurrentFileParams,
    updateCurrentPairStatus,
    getFileStats
  }
}
