<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useUserStore } from '../stores/user'
import { http } from '../services/api'
import { marked } from 'marked'
import { useRouter } from 'vue-router'

const router = useRouter()
const userStore = useUserStore()
const userInfo = computed(() => userStore.userInfo)

// 主流加密货币配置
const cryptoCoins = [
  { symbol: 'BTC', name: 'Bitcoin', icon: '₿', color: '#f7931a' },
  { symbol: 'ETH', name: 'Ethereum', icon: 'Ξ', color: '#627eea' },
  { symbol: 'SOL', name: 'Solana', icon: '◎', color: '#9945ff' },
  { symbol: 'BNB', name: 'BNB', icon: '🔶', color: '#f3ba2f' },
  { symbol: 'DOGE', name: 'Dogecoin', icon: 'Ð', color: '#c2a633' },
  { symbol: 'ADA', name: 'Cardano', icon: '♠', color: '#0033ad' },
  { symbol: 'XRP', name: 'XRP', icon: '◈', color: '#23292f' },
  { symbol: 'MATIC', name: 'Polygon', icon: '⬢', color: '#8247e5' },
]

// 状态管理
const isAnalyzing = ref(false)
const currentAnalyzing = ref('')
const analysisResults = ref<{ [key: string]: string }>({})
const recentAnalysis = ref<Array<{ symbol: string; timestamp: Date; preview: string }>>([])

// API基础URL
const apiBaseUrl =
  import.meta.env.MODE === 'development' ? 'http://127.0.0.1:8000' : 'https://api.ibtc.work'

// 配置marked选项
onMounted(() => {
  marked.setOptions({
    breaks: true,
    gfm: true,
  })
})

// 一键快速分析
const quickAnalyze = async (symbol: string, name: string) => {
  if (isAnalyzing.value) return
  
  isAnalyzing.value = true
  currentAnalyzing.value = symbol
  
  const message = `分析${symbol}行情`
  
  try {
    const response = await http.post(`${apiBaseUrl}/analysis/chat-messages`, {
      message: message,
    })

    if (!response.ok) {
      throw new Error('分析请求失败')
    }

    const reader = response.body?.getReader()
    if (!reader) {
      throw new Error('无法获取响应流')
    }

    const decoder = new TextDecoder()
    let buffer = ''
    let fullContent = ''

    while (true) {
      const { done, value } = await reader.read()
      if (done) break

      const chunk = decoder.decode(value, { stream: true })
      buffer += chunk

      const lines = buffer.split('\n')
      buffer = lines.pop() || ''

      for (const line of lines) {
        if (!line.trim() || !line.startsWith('data: ')) continue

        try {
          const jsonStr = line.slice(6)
          const data = JSON.parse(jsonStr)

          if (data.event === 'text_chunk' && data.data?.text) {
            fullContent += data.data.text
            analysisResults.value[symbol] = fullContent
          } else if (data.event === 'message' && data.answer) {
            fullContent += data.answer
            analysisResults.value[symbol] = fullContent
          } else if (data.event === 'workflow_finished') {
            if (data.data?.outputs?.text) {
              fullContent = data.data.outputs.text
              analysisResults.value[symbol] = fullContent
            }
            
            // 添加到最近分析记录
            const preview = fullContent.substring(0, 100) + '...'
            recentAnalysis.value.unshift({
              symbol,
              timestamp: new Date(),
              preview
            })
            
            // 只保留最近10条记录
            if (recentAnalysis.value.length > 10) {
              recentAnalysis.value = recentAnalysis.value.slice(0, 10)
            }
            
            break
          }
        } catch (error) {
          console.error('解析响应数据出错:', error)
        }
      }
    }
  } catch (error) {
    console.error('分析失败:', error)
    analysisResults.value[symbol] = '分析失败，请稍后重试'
  } finally {
    isAnalyzing.value = false
    currentAnalyzing.value = ''
  }
}

// 解析markdown
const parseMarkdown = (content: string) => {
  if (!content) return ''
  try {
    return marked(content) as string
  } catch (error) {
    return content.replace(/\n/g, '<br>')
  }
}

// 跳转到详细聊天页面
const goToDetailedChat = (symbol: string) => {
  router.push({
    path: '/ai-agent',
    query: { symbol }
  })
}

// 格式化时间
const formatTime = (date: Date) => {
  return date.toLocaleString('zh-CN', {
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}
</script>

<template>
  <div class="quick-analysis-view">
    <!-- 页面标题 -->
    <div class="page-header">
      <h1 class="page-title">快速行情分析</h1>
      <p class="page-subtitle">一键获取AI加密货币行情分析报告</p>
    </div>

    <!-- 使用限制提示 -->
    <div v-if="!userInfo?.is_subscribed" class="usage-notice">
      <div class="notice-icon">💡</div>
      <div class="notice-content">
        <span class="notice-text">非订阅用户每天只有 1 次体验分析，</span>
        <router-link to="/subscription" class="notice-link">订阅后解锁无限次分析</router-link>
      </div>
    </div>

    <!-- 加密货币网格 -->
    <div class="crypto-grid">
      <div
        v-for="coin in cryptoCoins"
        :key="coin.symbol"
        class="crypto-card"
        :class="{
          analyzing: currentAnalyzing === coin.symbol,
          analyzed: analysisResults[coin.symbol]
        }"
      >
        <div class="card-header">
          <div class="coin-info">
            <div class="coin-icon" :style="{ color: coin.color }">{{ coin.icon }}</div>
            <div class="coin-details">
              <div class="coin-symbol">{{ coin.symbol }}</div>
              <div class="coin-name">{{ coin.name }}</div>
            </div>
          </div>
          <button
            class="analyze-btn"
            @click="quickAnalyze(coin.symbol, coin.name)"
            :disabled="isAnalyzing"
            :class="{ active: currentAnalyzing === coin.symbol }"
          >
            <div v-if="currentAnalyzing === coin.symbol" class="loading-spinner"></div>
            <span v-else>{{ analysisResults[coin.symbol] ? '重新分析' : '快速分析' }}</span>
          </button>
        </div>

        <!-- 分析结果 -->
        <div v-if="analysisResults[coin.symbol]" class="analysis-result">
          <div class="result-header">
            <h4>分析结果</h4>
            <button class="detail-btn" @click="goToDetailedChat(coin.symbol)">
              详细讨论
            </button>
          </div>
          <div 
            class="result-content markdown-content"
            v-html="parseMarkdown(analysisResults[coin.symbol])"
          ></div>
        </div>

        <!-- 分析中状态 -->
        <div v-else-if="currentAnalyzing === coin.symbol" class="analyzing-state">
          <div class="analyzing-icon">
            <div class="pulse-dot"></div>
          </div>
          <p>AI正在分析 {{ coin.symbol }} 行情...</p>
        </div>

        <!-- 默认状态 -->
        <div v-else class="default-state">
          <div class="placeholder-icon">📊</div>
          <p>点击"快速分析"获取 {{ coin.symbol }} 行情分析</p>
        </div>
      </div>
    </div>

    <!-- 最近分析历史 -->
    <div v-if="recentAnalysis.length > 0" class="recent-analysis">
      <h3 class="section-title">最近分析</h3>
      <div class="analysis-list">
        <div
          v-for="(item, index) in recentAnalysis"
          :key="`${item.symbol}-${index}`"
          class="analysis-item"
          @click="goToDetailedChat(item.symbol)"
        >
          <div class="item-header">
            <span class="item-symbol">{{ item.symbol }}</span>
            <span class="item-time">{{ formatTime(item.timestamp) }}</span>
          </div>
          <div class="item-preview">{{ item.preview }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.quick-analysis-view {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
  min-height: 100vh;
  background-color: var(--color-bg-primary);
}

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

.page-title {
  font-size: 2rem;
  font-weight: 700;
  color: var(--color-text-primary);
  margin-bottom: 0.5rem;
  background: linear-gradient(135deg, var(--color-accent) 0%, #667eea 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.page-subtitle {
  color: var(--color-text-secondary);
  font-size: 1.1rem;
  margin: 0;
}

/* 使用限制提示 */
.usage-notice {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 1rem 1.5rem;
  background: linear-gradient(135deg, #fff3cd, #ffeaa7);
  border: 1px solid #ffd60a;
  border-radius: 12px;
  margin-bottom: 2rem;
  box-shadow: 0 2px 8px rgba(255, 214, 10, 0.1);
}

.notice-icon {
  font-size: 1.5rem;
  flex-shrink: 0;
}

.notice-content {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  flex: 1;
}

.notice-text {
  font-size: 0.95rem;
  color: #856404;
  line-height: 1.4;
}

.notice-link {
  font-size: 0.95rem;
  color: #0066cc;
  text-decoration: none;
  font-weight: 600;
  transition: color 0.2s ease;
}

.notice-link:hover {
  color: #004080;
  text-decoration: underline;
}

/* 加密货币网格 */
.crypto-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 1.5rem;
  margin-bottom: 3rem;
}

.crypto-card {
  background: var(--color-bg-secondary);
  border: 1px solid var(--color-border);
  border-radius: 16px;
  padding: 1.5rem;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.crypto-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
  border-color: var(--color-accent);
}

.crypto-card.analyzing {
  border-color: var(--color-accent);
  background: rgba(51, 85, 255, 0.02);
}

.crypto-card.analyzed {
  background: var(--color-bg-primary);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.coin-info {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.coin-icon {
  font-size: 2rem;
  font-weight: bold;
  width: 50px;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 12px;
}

.coin-symbol {
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--color-text-primary);
}

.coin-name {
  font-size: 0.9rem;
  color: var(--color-text-secondary);
}

.analyze-btn {
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 8px;
  background: var(--color-accent);
  color: white;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 100px;
  justify-content: center;
}

.analyze-btn:hover:not(:disabled) {
  background: var(--color-accent-hover);
  transform: translateY(-1px);
}

.analyze-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.analyze-btn.active {
  background: #ff6b35;
}

.loading-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* 分析结果 */
.analysis-result {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--color-border);
}

.result-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.result-header h4 {
  margin: 0;
  color: var(--color-text-primary);
  font-size: 1rem;
}

.detail-btn {
  padding: 0.4rem 0.8rem;
  border: 1px solid var(--color-accent);
  border-radius: 6px;
  background: transparent;
  color: var(--color-accent);
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.2s ease;
}

.detail-btn:hover {
  background: var(--color-accent);
  color: white;
}

.result-content {
  max-height: 300px;
  overflow-y: auto;
  line-height: 1.6;
  font-size: 0.9rem;
}

/* 分析中状态 */
.analyzing-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 2rem;
}

.analyzing-icon {
  margin-bottom: 1rem;
}

.pulse-dot {
  width: 20px;
  height: 20px;
  background: var(--color-accent);
  border-radius: 50%;
  animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
  0% {
    transform: scale(0.95);
    opacity: 0.7;
  }
  70% {
    transform: scale(1);
    opacity: 1;
  }
  100% {
    transform: scale(0.95);
    opacity: 0.7;
  }
}

/* 默认状态 */
.default-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 2rem;
  color: var(--color-text-secondary);
}

.placeholder-icon {
  font-size: 3rem;
  margin-bottom: 1rem;
  opacity: 0.5;
}

/* 最近分析 */
.recent-analysis {
  margin-top: 3rem;
}

.section-title {
  font-size: 1.5rem;
  font-weight: 600;
  color: var(--color-text-primary);
  margin-bottom: 1rem;
}

.analysis-list {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.analysis-item {
  padding: 1rem 1.5rem;
  background: var(--color-bg-secondary);
  border: 1px solid var(--color-border);
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.analysis-item:hover {
  border-color: var(--color-accent);
  background: var(--color-bg-primary);
  transform: translateY(-1px);
}

.item-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
}

.item-symbol {
  font-weight: 600;
  color: var(--color-accent);
}

.item-time {
  font-size: 0.85rem;
  color: var(--color-text-secondary);
}

.item-preview {
  color: var(--color-text-secondary);
  font-size: 0.9rem;
  line-height: 1.5;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .quick-analysis-view {
    padding: 1rem;
  }
  
  .crypto-grid {
    grid-template-columns: 1fr;
    gap: 1rem;
  }
  
  .crypto-card {
    padding: 1rem;
  }
  
  .page-title {
    font-size: 1.5rem;
  }
  
  .page-subtitle {
    font-size: 1rem;
  }
  
  .card-header {
    flex-direction: column;
    gap: 1rem;
    align-items: stretch;
  }
  
  .analyze-btn {
    width: 100%;
  }
}
</style>

<style>
/* Markdown 样式 */
.quick-analysis-view .markdown-content h1,
.quick-analysis-view .markdown-content h2,
.quick-analysis-view .markdown-content h3,
.quick-analysis-view .markdown-content h4 {
  color: var(--color-text-primary);
  margin-top: 1rem;
  margin-bottom: 0.5rem;
  font-size: 0.95rem;
  font-weight: 600;
}

.quick-analysis-view .markdown-content p {
  margin-bottom: 0.75rem;
  line-height: 1.6;
  color: var(--color-text-primary);
}

.quick-analysis-view .markdown-content ul,
.quick-analysis-view .markdown-content ol {
  margin-bottom: 0.75rem;
  padding-left: 1.5rem;
}

.quick-analysis-view .markdown-content li {
  margin-bottom: 0.25rem;
  color: var(--color-text-primary);
}

.quick-analysis-view .markdown-content strong {
  color: var(--color-accent);
  font-weight: 600;
}

.quick-analysis-view .markdown-content code {
  background: rgba(0, 0, 0, 0.1);
  padding: 0.2rem 0.4rem;
  border-radius: 4px;
  font-size: 0.85rem;
}

.quick-analysis-view .markdown-content pre {
  background: rgba(0, 0, 0, 0.05);
  padding: 0.75rem;
  border-radius: 8px;
  overflow-x: auto;
  margin-bottom: 0.75rem;
}

.quick-analysis-view .markdown-content blockquote {
  border-left: 4px solid var(--color-accent);
  padding-left: 1rem;
  margin: 0.75rem 0;
  color: var(--color-text-secondary);
}
</style>