"use client";

import { useTranslations, useLocale } from 'next-intl';
import Image from 'next/image';
import { motion, useScroll, useTransform, useInView, useSpring } from 'framer-motion';
import { useRef, useState, useEffect } from 'react';

export default function About() {
  const t = useTranslations('about');
  const locale = useLocale();
  const containerRef = useRef<HTMLDivElement>(null);
  const statsRef = useRef<HTMLDivElement>(null);
  const isInView = useInView(statsRef, { once: false, amount: 0.3 });
  
  // Paralaks kaydırma efekti için
  const { scrollYProgress } = useScroll({
    target: containerRef,
    offset: ["start end", "end start"]
  });
  
  const y1 = useTransform(scrollYProgress, [0, 1], [0, -50]);
  const y2 = useTransform(scrollYProgress, [0, 1], [0, -100]);
  const opacity = useTransform(scrollYProgress, [0, 0.3, 0.6], [0, 1, 1]);
  
  // Sayı kartları için animasyon
  const containerVariants = {
    hidden: { opacity: 0 },
    visible: {
      opacity: 1,
      transition: {
        staggerChildren: 0.1
      }
    }
  };
  
  const itemVariants = {
    hidden: { opacity: 0, y: 20 },
    visible: {
      opacity: 1,
      y: 0,
      transition: {
        duration: 0.6,
        ease: [0.22, 1, 0.36, 1]
      }
    }
  };
  
  const stats = [
    { figure: 1000000, label: t('stats.startups') },
    { figure: 50, label: t('stats.investors') },
    { figure: 1500000, label: t('stats.mentors') }
  ];

  // Sayıları tutmak için state
  const [counts, setCounts] = useState(stats.map(() => 0));

  // Sayıları hedef değerlerine doğru artırma
  useEffect(() => {
    if (isInView) {
      // Tüm sayaçlar için aynı toplam süre
      const duration = 3000; // 3 saniye içinde (daha hızlı)
      const intervalTime = 20;
      const steps = duration / intervalTime;
      
      // Her bir sayaç için aynı toplam süre içinde tamamlanacak şekilde ayarla
      const timers = stats.map((stat, index) => {
        const incrementValue = stat.figure / steps;
        
        return setInterval(() => {
          setCounts(prevCounts => {
            const newCounts = [...prevCounts];
            if (newCounts[index] < stat.figure) {
              // Hedef değeri aşmamak için kontrol
              newCounts[index] = Math.min(newCounts[index] + incrementValue, stat.figure);
            }
            return newCounts;
          });
        }, intervalTime);
      });
      
      return () => {
        timers.forEach(timer => clearInterval(timer));
      };
    } else {
      // Görünümden çıktığında sayıları sıfırla
      setCounts(stats.map(() => 0));
    }
  }, [isInView, stats]);

  // Sayı formatını oluşturan yardımcı fonksiyon
  const formatNumber = (num: number): string => {
    // Sadece sayıyı formatla (milyon yazısı yok)
    return new Intl.NumberFormat(locale === 'tr' ? 'tr-TR' : 'en-US').format(Math.round(num));
  };

  return (
    <section id="about" className="relative py-20 bg-gradient-to-b from-slate-900 to-slate-800 overflow-hidden" ref={containerRef}>
      {/* Dekoratif arka plan öğeleri */}
      <div className="absolute inset-0 overflow-hidden pointer-events-none">
        <div className="absolute top-0 right-0 w-[800px] h-[800px] bg-blue-600/5 rounded-full blur-3xl transform translate-x-1/3 -translate-y-1/2"></div>
        <div className="absolute bottom-0 left-0 w-[600px] h-[600px] bg-indigo-600/10 rounded-full blur-3xl transform -translate-x-1/3 translate-y-1/2"></div>
        
        {/* Izgara arka plan deseni */}
        <div className="absolute inset-0 bg-grid-white/[0.03] bg-grid-white/[0.02]"></div>
        
        {/* Dekoratif çizgiler */}
        <motion.div 
          className="absolute top-40 right-10 w-1/3 h-px bg-gradient-to-r from-transparent via-blue-500/20 to-transparent"
          style={{ y: y1 }}
        />
        <motion.div 
          className="absolute bottom-32 left-10 w-1/4 h-px bg-gradient-to-r from-transparent via-indigo-500/20 to-transparent"
          style={{ y: y2 }}
        />
      </div>
      
      <div className="container mx-auto px-4 sm:px-6 lg:px-8 relative">
        <motion.div
          className="text-center mb-16"
          initial={{ opacity: 0, y: 20 }}
          whileInView={{ opacity: 1, y: 0 }}
          viewport={{ once: false, margin: "-10%" }}
          transition={{ duration: 0.7, ease: "easeOut" }}
        >
          <motion.h2 
            className="text-3xl md:text-4xl lg:text-5xl font-bold mb-6 bg-clip-text text-transparent bg-gradient-to-r from-white to-blue-200"
            initial={{ opacity: 0, y: 20 }}
            whileInView={{ opacity: 1, y: 0 }}
            viewport={{ once: false, margin: "-10%" }}
            transition={{ duration: 0.5, delay: 0.1 }}
          >
            {t('title')}
          </motion.h2>
          
          <motion.div 
            className="w-24 h-1.5 bg-gradient-to-r from-blue-600 to-indigo-600 mx-auto rounded-full mb-8"
            initial={{ opacity: 0, width: 0 }}
            whileInView={{ opacity: 1, width: 96 }}
            viewport={{ once: false, margin: "-10%" }}
            transition={{ duration: 0.6, delay: 0.3 }}
          />
          
          <motion.p 
            className="max-w-3xl mx-auto text-gray-300 text-lg"
            initial={{ opacity: 0 }}
            whileInView={{ opacity: 1 }}
            viewport={{ once: false, margin: "-10%" }}
            transition={{ duration: 0.7, delay: 0.4 }}
          >
            {t('subtitle')}
          </motion.p>
        </motion.div>
        
        <div className="grid grid-cols-1 lg:grid-cols-2 gap-12 items-center">
          {/* İçerik bölümü */}
          <motion.div 
            className="order-2 lg:order-1"
            initial={{ opacity: 0, x: -50 }}
            whileInView={{ opacity: 1, x: 0 }}
            viewport={{ once: false, margin: "-10%" }}
            transition={{ duration: 0.7 }}
          >
            <h3 className="text-2xl md:text-3xl font-semibold text-white mb-6">
              {t('vision.title')}
            </h3>
            
            <p className="text-gray-300 mb-6">
              {t('vision.description')}
            </p>
            
            <div className="space-y-4 mb-8">
              {[1, 2, 3].map((item) => (
                <motion.div 
                  key={item}
                  className="flex items-start space-x-3"
                  initial={{ opacity: 0, y: 20 }}
                  whileInView={{ opacity: 1, y: 0 }}
                  viewport={{ once: false }}
                  transition={{ duration: 0.5, delay: 0.1 * item }}
                >
                  <div className="flex-shrink-0 w-6 h-6 rounded-full bg-gradient-to-r from-blue-600 to-indigo-600 flex items-center justify-center mt-1">
                    <svg className="w-4 h-4 text-white" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                      <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
                    </svg>
                  </div>
                  <div>
                    <h4 className="text-lg font-medium text-white mb-1">
                      {t(`keyFeatures.${item}.title`)}
                    </h4>
                    <p className="text-gray-400">
                      {t(`keyFeatures.${item}.description`)}
                    </p>
                  </div>
                </motion.div>
              ))}
            </div>
            
            <motion.div 
              className="inline-flex"
              initial={{ opacity: 0 }}
              whileInView={{ opacity: 1 }}
              viewport={{ once: false }}
              transition={{ duration: 0.5, delay: 0.6 }}
            >
              <a 
                href="#services" 
                className="px-8 py-3 rounded-full bg-gradient-to-r from-blue-600 to-indigo-600 text-white font-medium inline-flex items-center transition-transform hover:translate-y-[-2px] hover:shadow-lg hover:shadow-blue-500/20"
              >
                {t('cta')}
                <svg className="w-5 h-5 ml-2" viewBox="0 0 24 24" fill="none" stroke="currentColor">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M14 5l7 7m0 0l-7 7m7-7H3" />
                </svg>
              </a>
            </motion.div>
          </motion.div>
          
          {/* Görsel bölümü */}
          <motion.div 
            className="order-1 lg:order-2 relative"
            initial={{ opacity: 0, x: 50 }}
            whileInView={{ opacity: 1, x: 0 }}
            viewport={{ once: false, margin: "-10%" }}
            transition={{ duration: 0.7 }}
          >
            <div className="relative h-[400px] lg:h-[500px] rounded-2xl overflow-hidden shadow-2xl shadow-blue-900/30">
              {/* Ana görsel */}
              <Image
                src="/hakkimizda/gm2hakkimizda.jpg"
                alt={t('imageAlt')}
                fill
                style={{ objectFit: 'cover' }}
                className="transition-transform duration-500 hover:scale-105"
                priority
              />
              
              {/* Cam efekti overlay */}
              <div className="absolute inset-0 bg-gradient-to-tr from-slate-900/40 to-slate-900/10 backdrop-blur-[2px]"></div>
              
              {/* Çerçeve efekti */}
              <div className="absolute inset-4 border border-white/20 rounded-xl pointer-events-none"></div>
            </div>
            
            {/* Floating dekoratif öğeler */}
            <motion.div 
              className="absolute -top-6 -right-6 w-20 h-20 bg-blue-600/90 backdrop-blur-sm rounded-2xl p-4 shadow-xl shadow-blue-600/20 flex items-center justify-center"
              animate={{ 
                y: [0, -10, 0],
                rotate: [0, 5, 0],
              }}
              transition={{ 
                duration: 5, 
                repeat: Infinity,
                ease: "easeInOut" 
              }}
            >
              <svg className="w-12 h-12 text-white" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="1.5" d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z" />
              </svg>
            </motion.div>
            
            <motion.div 
              className="absolute -bottom-8 -left-8 w-16 h-16 bg-indigo-600/80 backdrop-blur-sm rounded-full p-3 shadow-xl shadow-indigo-600/20 flex items-center justify-center"
              animate={{ 
                y: [0, 10, 0],
                rotate: [0, -5, 0],
              }}
              transition={{ 
                duration: 6, 
                repeat: Infinity,
                ease: "easeInOut",
                delay: 1
              }}
            >
              <svg className="w-10 h-10 text-white" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="1.5" d="M13 10V3L4 14h7v7l9-11h-7z" />
              </svg>
            </motion.div>
          </motion.div>
        </div>
        
        {/* İstatistik sayıları */}
        <motion.div 
          ref={statsRef}
          className="mt-20 grid grid-cols-1 md:grid-cols-3 gap-6 md:gap-8"
          variants={containerVariants}
          initial="hidden"
          whileInView="visible"
          viewport={{ once: false, margin: "-5%" }}
        >
          {stats.map((stat, index) => (
            <motion.div 
              key={index}
              className="relative p-6 rounded-2xl bg-gradient-to-b from-slate-800 to-slate-900 border border-slate-700/50 shadow-lg group hover:border-blue-500/50 transition-colors duration-300"
              variants={itemVariants}
            >
              <div className="absolute inset-0 bg-blue-600/5 rounded-2xl opacity-0 group-hover:opacity-100 transition-opacity duration-300"></div>
              
              <motion.div
                className="text-2xl md:text-3xl font-bold text-white mb-1 flex items-end"
                initial={{ opacity: 0, y: 20 }}
                whileInView={{ opacity: 1, y: 0 }}
                viewport={{ once: false }}
                transition={{ duration: 0.5, delay: 0.1 * index }}
              >
                {formatNumber(counts[index])}
                {counts[index] >= stat.figure && '+'}
              </motion.div>
              
              <p className="text-gray-400 text-sm md:text-base">
                {stat.label}
              </p>
            </motion.div>
          ))}
        </motion.div>
      </div>
    </section>
  );
} 