<template>
  <div ref="toolbarRef" class="floating-toolbar" :class="{ 'is-hidden': !appStore.isToolbarVisible }">
    <div class="toolbar-buttons">
      <el-button class="toolbar-btn" @click="startScreenshot" :loading="appStore.isGrading">
        <span class="btn-icon">📷</span>
        <span class="btn-label">{{ appStore.isGrading ? '阅卷中...' : '截图阅卷' }}</span>
      </el-button>

      <el-button class="toolbar-btn" @click="openRubric">
        <span class="btn-icon">📋</span>
        <span class="btn-label">评分标准</span>
      </el-button>

      <template v-if="!showMenu">
        <el-button class="toolbar-btn" @click="showMenu = true">
          <span class="btn-icon">📎</span>
          <span class="btn-label">其它</span>
        </el-button>
      </template>
      <template v-else>
        <el-button class="toolbar-btn sub-btn" @click="showMenu = false; selectImage()">
          <span class="btn-icon">📁</span>
          <span class="btn-label">图片阅卷</span>
        </el-button>
        <el-button class="toolbar-btn sub-btn" @click="showMenu = false; openTemplates()">
          <span class="btn-icon">📚</span>
          <span class="btn-label">模板</span>
        </el-button>
        <el-button class="toolbar-btn sub-btn" @click="showMenu = false; openHistory()">
          <span class="btn-icon">📜</span>
          <span class="btn-label">历史</span>
        </el-button>
        <el-button class="toolbar-btn collapse-btn" @click="showMenu = false">
          <span class="btn-icon">↩</span>
          <span class="btn-label">收起</span>
        </el-button>
      </template>

      <el-button class="toolbar-btn" @click="openSettings">
        <span class="btn-icon">⚙</span>
        <span class="btn-label">设置</span>
      </el-button>
    </div>
    <div class="status-bar">{{ statusText }}</div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, nextTick, watch, onMounted, onUnmounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useAppStore } from '@/stores/app'
import { useGradingStore } from '@/stores/grading'
import { useSettingsStore } from '@/stores/settings'
import { addWatermark } from '@/utils/image'

const appStore = useAppStore()
const gradingStore = useGradingStore()
const settingsStore = useSettingsStore()

const showMenu = ref(false)
const toolbarRef = ref<HTMLElement | null>(null)

const statusText = computed(() => {
  const questions = gradingStore.questions
  const idx = gradingStore.currentQuestionIndex
  return questions?.length ? `第 ${idx + 1}/${questions.length} 题` : ''
})

async function resizeWindow() {
  await nextTick()
  if (toolbarRef.value && window.electronAPI) {
    const w = toolbarRef.value.scrollWidth + 2
    window.electronAPI.window.resizeMain(w)
  }
}

watch(showMenu, resizeWindow)

let cleanupScreenshotListener: (() => void) | null = null
let cleanupRubricListener: (() => void) | null = null

onMounted(() => {
  resizeWindow()
  if (window.electronAPI) {
    cleanupScreenshotListener = window.electronAPI.screenshot.onCompleted(async (base64: string) => {
      if (!appStore.isGrading) {
        try {
          gradingStore.setImage(await addWatermark(base64, '学生答案'))
          startGrading()
        } catch (err) {
          ElMessage.error('水印处理失败：' + (err as Error).message)
        }
      }
    })
    cleanupRubricListener = window.electronAPI.rubric.onUpdated(async () => {
      const data = await window.electronAPI!.rubric.getData()
      if (data?.questions?.length) {
        gradingStore.setQuestions(JSON.parse(JSON.stringify(data.questions)))
        ElMessage.success(`已应用模板（${data.questions.length} 题）`)
      }
    })
  }
})

onUnmounted(() => {
  cleanupScreenshotListener?.()
  cleanupRubricListener?.()
})

function startScreenshot() {
  if (window.electronAPI) window.electronAPI.screenshot.start()
}

async function selectImage() {
  if (!window.electronAPI) return
  const data = await window.electronAPI.dialog.openImage()
  if (data) {
    try {
      gradingStore.setImage(await addWatermark(data.base64, '学生答案'))
    } catch (err) {
      ElMessage.error('水印处理失败：' + (err as Error).message)
      return
    }
    startGrading()
  }
}

async function handleMenu(cmd: string) {
  if (cmd === 'image') await selectImage()
  else if (cmd === 'templates') window.electronAPI?.dialog.open('templates')
  else if (cmd === 'history') window.electronAPI?.dialog.open('history')
}

async function gradeQuestion(questionIdx: number) {
  const questions = gradingStore.questions
  if (!questions.length) {
    ElMessage.warning('请先在评分标准中添加题目')
    return null
  }
  const q = questions[Math.min(questionIdx, questions.length - 1)]
  const result = await window.electronAPI.ai.grade({
    imageBase64: gradingStore.currentImage,
    rubric: q.rubric || `请根据答案内容评分，满分${q.maxScore}分`,
    maxScore: q.maxScore,
    referenceAnswer: q.referenceAnswer,
    referenceAnswerImage: q.referenceAnswerImage,
    questionTitle: q.questionTitle,
    apiConfig: {
      apiKey: settingsStore.settings.apiKey,
      baseUrl: settingsStore.settings.baseUrl,
      model: settingsStore.settings.model
    }
  })
  return result
}

async function startGrading() {
  if (!gradingStore.currentImage) {
    ElMessage.info('请先截图或选择图片')
    return
  }
  if (!settingsStore.settings.apiKey) {
    ElMessage.warning('请先在设置中配置 API Key')
    if (window.electronAPI) window.electronAPI.dialog.open('settings')
    return
  }

  // load rubric data
  const rubricData = await window.electronAPI?.rubric.getData()
  const questions = rubricData?.questions?.length
    ? JSON.parse(JSON.stringify(rubricData.questions))
    : [{ questionTitle: '', maxScore: 100, referenceAnswer: '', rubric: '' }]
  const nextIdx = Math.min(await window.electronAPI?.rubric.getNextIndex() ?? 0, questions.length - 1)
  gradingStore.setQuestions(questions)
  gradingStore.setCurrentQuestionIndex(nextIdx)

  appStore.isGrading = true
  try {
    const result = await gradeQuestion(nextIdx)
    if (!result) return

    gradingStore.setResult(result)
    if (window.electronAPI) {
      // store grading data for result window
      await window.electronAPI.grading.storeData({
        result,
        image: gradingStore.currentImage,
        questions,
        currentQuestionIndex: nextIdx
      })
      await window.electronAPI.dialog.open('result')
    }
  } catch (err) {
    ElMessage.error('AI 阅卷失败：' + (err as Error).message)
  } finally {
    appStore.isGrading = false
  }
}

function openRubric() {
  window.electronAPI?.dialog.open('rubric')
}

async function openTemplates() { window.electronAPI?.dialog.open('templates') }
function openHistory() { window.electronAPI?.dialog.open('history') }
function openSettings() { window.electronAPI?.dialog.open('settings') }
</script>

<style scoped>
.floating-toolbar {
  position: fixed;
  top: 0;
  left: 0;
  width: fit-content;
  min-width: 200px;
  z-index: 99999;
  user-select: none;
  -webkit-app-region: drag;
  background: rgba(30, 30, 30, 0.85);
  backdrop-filter: blur(12px);
  border-radius: 12px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.toolbar-buttons {
  display: flex;
  gap: 6px;
  padding: 8px 14px 0;
  justify-content: center;
  align-items: center;
}

.status-bar {
  padding: 0 14px 6px;
  font-size: 11px;
  font-weight: 700;
  color: rgba(255, 255, 255, 0.6);
  white-space: nowrap;
  text-align: center;
}

.sub-btn {
  padding: 4px 8px !important;
}
.collapse-btn {
  padding: 4px 6px !important;
  color: rgba(255, 255, 255, 0.5) !important;
}
.collapse-btn:hover {
  color: rgba(255, 255, 255, 0.9) !important;
}

.toolbar-btn {
  -webkit-app-region: no-drag;
  border: none !important;
  background: transparent !important;
  color: rgba(255, 255, 255, 0.85) !important;
  height: 46px !important;
  padding: 4px 14px !important;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  transition: all 0.2s;
  cursor: pointer;
}

.toolbar-btn:hover {
  background: rgba(255, 255, 255, 0.12) !important;
  color: #fff !important;
}

.btn-icon {
  font-size: 16px;
  line-height: 1;
}

.btn-label {
  font-size: 10px;
  line-height: 1;
  white-space: nowrap;
}

.is-hidden {
  opacity: 0;
  pointer-events: none;
}
</style>
