'use client';

import { useState, useEffect, useRef } from 'react';
import Image from 'next/image';
import { useLanguage } from '@/contexts/LanguageContext';
import ConsenSysLogo from './ConsenSysLogo';

export default function TrustedBySection() {
  const { t } = useLanguage();
  const [animate, setAnimate] = useState(false);
  const sectionRef = useRef<HTMLElement>(null);

  useEffect(() => {
    const currentRef = sectionRef.current;
    if (!currentRef) return;

    const observer = new IntersectionObserver(
      (entries) => {
        if (entries[0].isIntersecting) {
          setAnimate(true);
          observer.disconnect(); // 触发后立即断开
        }
      },
      { threshold: 0.3 }
    );

    observer.observe(currentRef);
    return () => observer.disconnect();
  }, []);

  // 合作伙伴 logo 数据 - 统一高度为40px
  const partners = [
    {
      name: 'BlackRock',
      src: '/nav-ireland0.svg',
      width: 200,
      height: 40,
      className: 'w-[200px] h-[40px]'
    },
    {
      name: 'Coinbase',
      src: '/coinbase-10.svg',
      width: 180,
      height: 40,
      className: 'w-[180px] h-[40px]'
    },
    {
      name: 'Wintermute',
      src: '/wintermute0.svg',
      width: 247,
      height: 40,
      className: 'w-[247px] h-[40px]'
    },
    {
      name: 'Circle',
      src: '/group0.svg',
      width: 156,
      height: 40,
      className: 'w-[156px] h-[40px]'
    },
    {
      name: 'ConsenSys',
      src: '', // 使用组合的vector文件
      width: 220,
      height: 50,
      className: 'w-[220px] h-[50px]'
    }
  ];

  return (
    <section
      ref={sectionRef}
      className="bg-[#f9fafb] flex flex-col items-center justify-center flex-shrink-0 w-full relative"
      style={{
        borderStyle: 'solid',
        borderColor: '#e5e7eb',
        borderWidth: '0px 0px 1px 0px',
        padding: '80px 0px',
        gap: '32px'
      }}
    >
      {/* 标题 - .text6 */}
      <div
        className="text-[#111827] text-center relative flex items-center justify-center font-inter"
        style={{
          fontSize: '18px',
          lineHeight: '150%',
          fontWeight: 500
        }}
      >
        {t('trusted.title')}
      </div>

      {/* Logo容器 - .frame-26 */}
      <div
        className="flex flex-row items-center justify-between flex-shrink-0 w-[1440px] relative"
      >
        {partners.map((partner, index) => (
          <div
            key={partner.name}
            className={`flex-shrink-0 relative overflow-hidden cursor-pointer hover:brightness-0 ${
              animate
                ? 'translate-y-0 opacity-100'
                : 'translate-y-12 opacity-0'
            }`}
            style={{
              transition: `transform 0.7s ease-out ${index * 100}ms, opacity 0.7s ease-out ${index * 100}ms, filter 0.3s ease-out`,
              width: `${partner.width}px`,
              height: `${partner.height}px`,
              aspectRatio: `${partner.width}/${partner.height}`
            }}
          >
            {partner.name === 'ConsenSys' ? (
              <ConsenSysLogo width={partner.width} height={partner.height} />
            ) : (
              <Image
                src={partner.src}
                alt={partner.name}
                width={partner.width}
                height={partner.height}
                className="w-full h-full object-contain"
              />
            )}
          </div>
        ))}
      </div>
    </section>
  );
}
