import { useState } from 'react';
import { Flame, TrendingUp, TrendingDown, Minus, AlertTriangle, ChevronRight, Calendar, Target } from 'lucide-react';
import type { HotEvent } from '@/types';

interface HotEventsProps {
  events: HotEvent[];
}

function ImpactStars({ level }: { level: number }) {
  return (
    <div className="flex gap-0.5">
      {Array.from({ length: 5 }).map((_, i) => (
        <div
          key={i}
          className={`w-4 h-4 rounded-sm ${i < level ? 'bg-binance-yellow' : 'bg-binanceBorder-light'}`}
        />
      ))}
    </div>
  );
}

function ImpactBadge({ impact }: { impact: 'bullish' | 'bearish' | 'neutral' }) {
  const config = {
    bullish: { icon: TrendingUp, text: '利多', color: 'text-semantic-green', bg: 'bg-semantic-green/10' },
    bearish: { icon: TrendingDown, text: '利空', color: 'text-semantic-red', bg: 'bg-semantic-red/10' },
    neutral: { icon: Minus, text: '中性', color: 'text-text-slate', bg: 'bg-text-slate/10' },
  };
  
  const { icon: Icon, text, color, bg } = config[impact];
  
  return (
    <div className={`flex items-center gap-1 px-2 py-1 rounded-md ${bg}`}>
      <Icon className={`w-3 h-3 ${color}`} />
      <span className={`text-xs font-semibold ${color}`}>{text}</span>
    </div>
  );
}

export function HotEventsPanel({ events }: HotEventsProps) {
  const [selectedEvent, setSelectedEvent] = useState<HotEvent>(events[0]);

  return (
    <div className="grid grid-cols-1 lg:grid-cols-5 gap-space-4">
      <div className="lg:col-span-3 space-y-space-3">
        <div className="flex items-center gap-2 mb-space-4">
          <Flame className="w-5 h-5 text-binance-yellow" />
          <h3 className="text-lg font-bold text-text-ink">热点事件</h3>
          <span className="text-xs text-text-slate ml-auto">共 {events.length} 条</span>
        </div>
        
        {events.map((event) => (
          <div
            key={event.id}
            onClick={() => setSelectedEvent(event)}
            className={`p-4 rounded-card border cursor-pointer transition-all duration-200 ${
              selectedEvent?.id === event.id
                ? 'border-binance-yellow bg-binance-yellow/5'
                : 'border-border-light bg-white hover:border-binance-gold'
            }`}
          >
            <div className="flex items-start justify-between gap-4">
              <div className="flex-1">
                <div className="flex items-center gap-2 mb-2">
                  <h4 className="font-semibold text-text-ink">{event.title}</h4>
                  <ImpactBadge impact={event.impact} />
                </div>
                
                <p className="text-sm text-text-slate mb-space-3 line-clamp-2">{event.summary}</p>
                
                <div className="flex items-center gap-4 text-xs">
                  <div className="flex items-center gap-1 text-text-slate">
                    <Calendar className="w-3 h-3" />
                    <span>{event.time}</span>
                  </div>
                  
                  <div className="flex items-center gap-2">
                    <Target className="w-3 h-3 text-text-slate" />
                    <span className="text-text-slate">影响:</span>
                    <div className="flex gap-1">
                      {event.affectedProducts.slice(0, 3).map((product) => (
                        <span
                          key={product}
                          className="px-1.5 py-0.5 rounded bg-surface-snow text-text-secondary"
                        >
                          {product}
                        </span>
                      ))}
                      {event.affectedProducts.length > 3 && (
                        <span className="text-text-slate">+{event.affectedProducts.length - 3}</span>
                      )}
                    </div>
                  </div>
                </div>
              </div>
              
              <div className="flex flex-col items-end gap-2">
                <ImpactStars level={event.impactLevel} />
                <ChevronRight className={`w-4 h-4 transition-transform ${
                  selectedEvent?.id === event.id ? 'text-binance-yellow rotate-90' : 'text-text-slate'
                }`} />
              </div>
            </div>
          </div>
        ))}
      </div>
      
      <div className="lg:col-span-2">
        <div className="sticky top-4 p-space-5 rounded-card border border-border-light bg-white">
          {selectedEvent ? (
            <>
              <div className="flex items-center gap-2 mb-space-4">
                <div className="p-2 rounded-lg bg-binance-yellow/10">
                  <AlertTriangle className="w-4 h-4 text-binance-yellow" />
                </div>
                <h3 className="font-bold text-text-ink">智能分析</h3>
              </div>
              
              <div className="space-y-space-4">
                <div>
                  <h4 className="text-sm text-text-slate mb-2">事件解读</h4>
                  <p className="text-sm text-text-ink leading-relaxed">{selectedEvent.analysis}</p>
                </div>
                
                <div>
                  <h4 className="text-sm text-text-slate mb-2">影响品种</h4>
                  <div className="flex flex-wrap gap-2">
                    {selectedEvent.affectedProducts.map((product) => (
                      <span
                        key={product}
                        className={`px-3 py-1.5 rounded-lg text-sm font-semibold ${
                          selectedEvent.impact === 'bullish'
                            ? 'bg-semantic-green/10 text-semantic-green'
                            : selectedEvent.impact === 'bearish'
                            ? 'bg-semantic-red/10 text-semantic-red'
                            : 'bg-text-slate/10 text-text-slate'
                        }`}
                      >
                        {product}
                      </span>
                    ))}
                  </div>
                </div>
                
                <div>
                  <h4 className="text-sm text-text-slate mb-2">风险提示</h4>
                  <ul className="space-y-2">
                    {selectedEvent.risks.map((risk, index) => (
                      <li key={index} className="flex items-start gap-2 text-sm text-text-secondary">
                        <span className="w-1.5 h-1.5 rounded-full bg-binance-gold mt-1.5 flex-shrink-0" />
                        <span>{risk}</span>
                      </li>
                    ))}
                  </ul>
                </div>
                
                <div className="pt-space-4 border-t border-binanceBorder-light">
                  <div className="flex items-center justify-between">
                    <span className="text-sm text-text-slate">影响强度</span>
                    <ImpactStars level={selectedEvent.impactLevel} />
                  </div>
                </div>
              </div>
            </>
          ) : (
            <div className="text-center py-8 text-text-slate">
              <AlertTriangle className="w-8 h-8 mx-auto mb-2 opacity-50" />
              <p>选择事件查看分析</p>
            </div>
          )}
        </div>
      </div>
    </div>
  );
}