<template>
  <div class="code-block" :class="{ 'dark': isDark }">
    <div class="code-header">
      <span class="language">{{ language }}</span>
      <v-btn
        variant="text"
        size="small"
        class="copy-btn"
        @click="copyCode"
        :color="copied ? 'success' : 'default'"
      >
        <v-icon size="16" :class="{ 'mr-1': !copied }">
          {{ copied ? 'mdi-check' : 'mdi-content-copy' }}
        </v-icon>
        <span v-if="!copied">复制</span>
        <span v-else class="success-text">已复制</span>
      </v-btn>
    </div>
    <pre class="code-content"><code ref="codeRef" v-html="highlightedCode"></code></pre>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, computed } from 'vue'
import hljs from 'highlight.js'
import 'highlight.js/styles/vs2015.css'

// 注册常用的编程语言
import 'highlight.js/lib/languages/javascript'
import 'highlight.js/lib/languages/typescript'
import 'highlight.js/lib/languages/python'
import 'highlight.js/lib/languages/java'
import 'highlight.js/lib/languages/cpp'
import 'highlight.js/lib/languages/csharp'
import 'highlight.js/lib/languages/php'
import 'highlight.js/lib/languages/ruby'
import 'highlight.js/lib/languages/go'
import 'highlight.js/lib/languages/rust'
import 'highlight.js/lib/languages/sql'
import 'highlight.js/lib/languages/xml'
import 'highlight.js/lib/languages/css'
import 'highlight.js/lib/languages/json'
import 'highlight.js/lib/languages/yaml'
import 'highlight.js/lib/languages/markdown'
import 'highlight.js/lib/languages/bash'
import 'highlight.js/lib/languages/shell'
import 'highlight.js/lib/languages/dockerfile'

const props = defineProps<{
  code: string
  language?: string
  isDark?: boolean
}>()

const codeRef = ref<HTMLElement | null>(null)
const copied = ref(false)

const highlightedCode = computed(() => {
  if (!props.code) return ''
  try {
    if (props.language && hljs.getLanguage(props.language)) {
      return hljs.highlight(props.code, {
        language: props.language,
        ignoreIllegals: true
      }).value
    } else {
      // 如果没有指定语言或语言不支持，尝试自动检测
      return hljs.highlightAuto(props.code).value
    }
  } catch (e) {
    console.warn('Failed to highlight code:', e)
    return props.code // 如果高亮失败，返回原始代码
  }
})

const copyCode = async () => {
  try {
    await navigator.clipboard.writeText(props.code)
    copied.value = true
    setTimeout(() => {
      copied.value = false
    }, 2000)
  } catch (err) {
    console.warn('Failed to copy code:', err)
  }
}

// 监听代码变化，确保滚动条重置
watch(() => props.code, () => {
  if (codeRef.value) {
    codeRef.value.scrollLeft = 0
  }
})
</script>

<style scoped>
.code-block {
  margin: 1rem 0;
  border-radius: 8px;
  overflow: hidden;
  background: #1e1e1e;
  font-family: 'JetBrains Mono', 'Fira Code', 'Consolas', monospace;
  transition: all 0.3s ease;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.code-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 16px;
  background: rgba(255, 255, 255, 0.05);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.language {
  font-size: 12px;
  color: #89ddff;
  text-transform: uppercase;
  letter-spacing: 1px;
  font-weight: 500;
}

.copy-btn {
  opacity: 0.8;
  transition: all 0.2s ease;
  color: #a9b7c6 !important;
  font-size: 12px !important;
  min-width: 64px !important;
  padding: 0 8px !important;
}

.copy-btn:hover {
  opacity: 1;
  background: rgba(255, 255, 255, 0.1) !important;
}

.copy-btn .v-icon {
  margin-right: 4px;
}

.code-content {
  margin: 0;
  padding: 16px;
  overflow-x: auto;
  font-size: 14px;
  line-height: 1.6;
  background: #1e1e1e;
  color: #d4d4d4;
}

/* 自定义滚动条 */
.code-content::-webkit-scrollbar {
  height: 6px;
  width: 6px;
}

.code-content::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.05);
}

.code-content::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 3px;
}

.code-content::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.3);
}

/* 代码高亮自定义样式 */
:deep(.hljs) {
  background: transparent;
  color: #d4d4d4;
  text-shadow: none;
  font-family: inherit;
}

:deep(.hljs-keyword) {
  color: #569cd6;
  font-weight: bold;
}

:deep(.hljs-string) {
  color: #ce9178;
}

:deep(.hljs-number) {
  color: #b5cea8;
}

:deep(.hljs-function) {
  color: #dcdcaa;
}

:deep(.hljs-title) {
  color: #4ec9b0;
}

:deep(.hljs-class) {
  color: #4ec9b0;
}

:deep(.hljs-params) {
  color: #9cdcfe;
}

:deep(.hljs-comment) {
  color: #6a9955;
  font-style: italic;
}

:deep(.hljs-built_in) {
  color: #4ec9b0;
}

:deep(.hljs-variable) {
  color: #9cdcfe;
}

:deep(.hljs-operator) {
  color: #d4d4d4;
}

:deep(.hljs-punctuation) {
  color: #d4d4d4;
}

:deep(.hljs-property) {
  color: #9cdcfe;
}

:deep(.hljs-literal) {
  color: #569cd6;
}

:deep(.hljs-attr) {
  color: #9cdcfe;
}

:deep(.hljs-tag) {
  color: #569cd6;
}

/* 深色主题适配 */
.dark {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.dark .code-header {
  background: rgba(0, 0, 0, 0.2);
}

.dark .language {
  color: #89ddff;
}

.dark .copy-btn {
  color: #a9b7c6 !important;
}

.dark .copy-btn:hover {
  background: rgba(255, 255, 255, 0.1) !important;
}

.success-text {
  color: #4caf50 !important;
}
</style> 