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-revolut-blue' : 'bg-revolut-grayTone'}`}
        />
      ))}
    </div>
  );
}

function ImpactBadge({ impact }: { impact: 'bullish' | 'bearish' | 'neutral' }) {
  const config = {
    bullish: { icon: TrendingUp, text: '利多', color: 'text-revolut-teal', bg: 'bg-revolut-teal/10' },
    bearish: { icon: TrendingDown, text: '利空', color: 'text-revolut-danger', bg: 'bg-revolut-danger/10' },
    neutral: { icon: Minus, text: '中性', color: 'text-revolut-midSlate', bg: 'bg-revolut-midSlate/10' },
  };
  
  const { icon: Icon, text, color, bg } = config[impact];
  
  return (
    <div className={`flex items-center gap-1 px-2 py-1 rounded-pill ${bg}`}>
      <Icon className={`w-3 h-3 ${color}`} />
      <span className={`text-xs font-medium ${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-4">
      <div className="lg:col-span-3 space-y-3">
        <div className="flex items-center gap-2 mb-4">
          <Flame className="w-5 h-5 text-revolut-blue" />
          <h3 className="text-lg font-medium text-revolut-dark heading-display">热点事件</h3>
          <span className="text-xs text-revolut-midSlate 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-revolut-blue bg-revolut-blue/5'
                : 'border-revolut-grayTone bg-revolut-white hover:border-revolut-midSlate'
            }`}
          >
            <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-medium text-revolut-dark">{event.title}</h4>
                  <ImpactBadge impact={event.impact} />
                </div>
                
                <p className="text-sm text-revolut-midSlate mb-3 line-clamp-2">{event.summary}</p>
                
                <div className="flex items-center gap-4 text-xs">
                  <div className="flex items-center gap-1 text-revolut-coolGray">
                    <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-revolut-coolGray" />
                    <span className="text-revolut-coolGray">影响:</span>
                    <div className="flex gap-1">
                      {event.affectedProducts.slice(0, 3).map((product) => (
                        <span
                          key={product}
                          className="px-1.5 py-0.5 rounded-standard bg-revolut-surface text-revolut-dark"
                        >
                          {product}
                        </span>
                      ))}
                      {event.affectedProducts.length > 3 && (
                        <span className="text-revolut-coolGray">+{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-revolut-blue rotate-90' : 'text-revolut-coolGray'
                }`} />
              </div>
            </div>
          </div>
        ))}
      </div>
      
      <div className="lg:col-span-2">
        <div className="sticky top-4 p-5 rounded-card border border-revolut-grayTone bg-revolut-white">
          {selectedEvent ? (
            <>
              <div className="flex items-center gap-2 mb-4">
                <div className="p-2 rounded-standard bg-revolut-blue/10">
                  <AlertTriangle className="w-4 h-4 text-revolut-blue" />
                </div>
                <h3 className="font-medium text-revolut-dark">智能分析</h3>
              </div>
              
              <div className="space-y-4">
                <div>
                  <h4 className="text-sm text-revolut-midSlate mb-2">事件解读</h4>
                  <p className="text-sm text-revolut-dark leading-relaxed">{selectedEvent.analysis}</p>
                </div>
                
                <div>
                  <h4 className="text-sm text-revolut-midSlate mb-2">影响品种</h4>
                  <div className="flex flex-wrap gap-2">
                    {selectedEvent.affectedProducts.map((product) => (
                      <span
                        key={product}
                        className={`px-3 py-1.5 rounded-pill text-sm font-medium ${
                          selectedEvent.impact === 'bullish'
                            ? 'bg-revolut-teal/10 text-revolut-teal'
                            : selectedEvent.impact === 'bearish'
                            ? 'bg-revolut-danger/10 text-revolut-danger'
                            : 'bg-revolut-midSlate/10 text-revolut-midSlate'
                        }`}
                      >
                        {product}
                      </span>
                    ))}
                  </div>
                </div>
                
                <div>
                  <h4 className="text-sm text-revolut-midSlate 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-revolut-dark">
                        <span className="w-1.5 h-1.5 rounded-full bg-revolut-warning mt-1.5 flex-shrink-0" />
                        <span>{risk}</span>
                      </li>
                    ))}
                  </ul>
                </div>
                
                <div className="pt-4 border-t border-revolut-grayTone">
                  <div className="flex items-center justify-between">
                    <span className="text-sm text-revolut-midSlate">影响强度</span>
                    <ImpactStars level={selectedEvent.impactLevel} />
                  </div>
                </div>
              </div>
            </>
          ) : (
            <div className="text-center py-8 text-revolut-midSlate">
              <AlertTriangle className="w-8 h-8 mx-auto mb-2 opacity-50" />
              <p>选择事件查看分析</p>
            </div>
          )}
        </div>
      </div>
    </div>
  );
}