"use client";

import { useRef, ReactNode } from 'react';
import { motion, useInView, MotionProps, Variant } from 'framer-motion';
import { 
  fadeIn, 
  fadeInUp, 
  fadeInDown, 
  fadeInLeft, 
  fadeInRight, 
  staggerContainer,
  defaultViewThreshold,
  cardHover,
  buttonHover
} from '@/animations';

// Framer Motion'ın tür tanımlamaları için yardımcı
type AnimationVariant = Variant & {
  transition?: {
    delay?: number;
    delayChildren?: number;
    duration?: number;
    ease?: any;
    [key: string]: any;
  };
};

// Component props
interface MotionComponentProps extends MotionProps {
  children: ReactNode;
  threshold?: number;
  once?: boolean;
  delay?: number;
  className?: string;
}

// Görünürlük temelli animasyon bileşeni
export const MotionInView = ({ 
  children, 
  variants = fadeIn,
  threshold = defaultViewThreshold,
  once = true,
  className = "",
  ...rest 
}: { 
  children: ReactNode;
  variants?: any;
  threshold?: number;
  once?: boolean;
  className?: string;
} & MotionProps) => {
  const ref = useRef(null);
  const isInView = useInView(ref, { once, amount: threshold });
  
  return (
    <motion.div
      ref={ref}
      variants={variants}
      initial="hidden"
      animate={isInView ? "visible" : "hidden"}
      className={className}
      {...rest}
    >
      {children}
    </motion.div>
  );
};

// Yukarıdan aşağı doğru animasyonlu bileşen
export const MotionFadeInDown = ({ 
  children, 
  threshold = defaultViewThreshold,
  once = true,
  delay = 0,
  className = "",
  ...rest 
}: MotionComponentProps) => {
  const customVariants = {
    hidden: { ...fadeInDown.hidden },
    visible: { 
      ...fadeInDown.visible,
      transition: {
        duration: 0.7,
        ease: [0.6, 0.05, 0.01, 0.9],
        delay
      }
    }
  };
  
  return (
    <MotionInView 
      variants={customVariants} 
      threshold={threshold} 
      once={once} 
      className={className}
      {...rest}
    >
      {children}
    </MotionInView>
  );
};

// Aşağıdan yukarı doğru animasyonlu bileşen
export const MotionFadeInUp = ({ 
  children, 
  threshold = defaultViewThreshold,
  once = true,
  delay = 0,
  className = "",
  ...rest 
}: MotionComponentProps) => {
  const customVariants = {
    hidden: { ...fadeInUp.hidden },
    visible: { 
      ...fadeInUp.visible,
      transition: {
        duration: 0.7,
        ease: [0.6, 0.05, 0.01, 0.9],
        delay
      }
    }
  };
  
  return (
    <MotionInView 
      variants={customVariants} 
      threshold={threshold} 
      once={once} 
      className={className}
      {...rest}
    >
      {children}
    </MotionInView>
  );
};

// Soldan sağa doğru animasyonlu bileşen
export const MotionFadeInLeft = ({ 
  children, 
  threshold = defaultViewThreshold,
  once = true,
  delay = 0,
  className = "",
  ...rest 
}: MotionComponentProps) => {
  const customVariants = {
    hidden: { ...fadeInLeft.hidden },
    visible: { 
      ...fadeInLeft.visible,
      transition: {
        duration: 0.7,
        ease: [0.6, 0.05, 0.01, 0.9],
        delay
      }
    }
  };
  
  return (
    <MotionInView 
      variants={customVariants} 
      threshold={threshold} 
      once={once} 
      className={className}
      {...rest}
    >
      {children}
    </MotionInView>
  );
};

// Sağdan sola doğru animasyonlu bileşen
export const MotionFadeInRight = ({ 
  children, 
  threshold = defaultViewThreshold,
  once = true,
  delay = 0,
  className = "",
  ...rest 
}: MotionComponentProps) => {
  const customVariants = {
    hidden: { ...fadeInRight.hidden },
    visible: { 
      ...fadeInRight.visible,
      transition: {
        duration: 0.7,
        ease: [0.6, 0.05, 0.01, 0.9],
        delay
      }
    }
  };
  
  return (
    <MotionInView 
      variants={customVariants} 
      threshold={threshold} 
      once={once} 
      className={className}
      {...rest}
    >
      {children}
    </MotionInView>
  );
};

// Kademeli animasyonlar için container bileşeni
export const MotionStaggerContainer = ({ 
  children, 
  threshold = defaultViewThreshold,
  once = true,
  delay = 0,
  className = "",
  ...rest 
}: MotionComponentProps) => {
  const ref = useRef(null);
  const isInView = useInView(ref, { once, amount: threshold });
  
  const customVariants = {
    hidden: { ...staggerContainer.hidden },
    visible: { 
      ...staggerContainer.visible,
      transition: {
        staggerChildren: 0.2,
        delayChildren: delay
      }
    }
  };
  
  return (
    <motion.div
      ref={ref}
      variants={customVariants}
      initial="hidden"
      animate={isInView ? "visible" : "hidden"}
      className={className}
      {...rest}
    >
      {children}
    </motion.div>
  );
};

// Kart için hover animasyonu bileşeni
export const MotionCard = ({ 
  children, 
  className = "",
  ...rest 
}: { 
  children: ReactNode;
  className?: string;
} & MotionProps) => {
  return (
    <motion.div
      className={className}
      initial="rest"
      whileHover="hover"
      variants={cardHover}
      {...rest}
    >
      {children}
    </motion.div>
  );
};

// Buton için hover animasyonu bileşeni
export const MotionButton = ({ 
  children, 
  className = "",
  ...rest 
}: { 
  children: ReactNode;
  className?: string;
} & MotionProps) => {
  return (
    <motion.button
      className={className}
      initial="rest"
      whileHover="hover"
      variants={buttonHover}
      {...rest}
    >
      {children}
    </motion.button>
  );
};

// Scroll bazlı paralaks efekti için bileşen
export const MotionParallax = ({ 
  children, 
  offset = 50, 
  direction = 'up',
  className = "",
  ...rest 
}: { 
  children: ReactNode;
  offset?: number;
  direction?: 'up' | 'down' | 'left' | 'right';
  className?: string;
} & MotionProps) => {
  const ref = useRef(null);
  const isInView = useInView(ref, { once: false, amount: 0.3 });
  
  const getDirectionValues = () => {
    switch (direction) {
      case 'up':
        return { y: [0, -offset] };
      case 'down':
        return { y: [0, offset] };
      case 'left':
        return { x: [0, -offset] };
      case 'right':
        return { x: [0, offset] };
      default:
        return { y: [0, -offset] };
    }
  };
  
  return (
    <motion.div
      ref={ref}
      initial="hidden"
      animate={isInView ? "visible" : "hidden"}
      variants={{
        hidden: { opacity: 0, ...getDirectionValues() },
        visible: { 
          opacity: 1, 
          ...(direction === 'up' || direction === 'down' ? { y: 0 } : { x: 0 }),
          transition: { duration: 0.6 }
        }
      }}
      className={className}
      {...rest}
    >
      {children}
    </motion.div>
  );
};

// Öğeleri bir araya getiren index dosyası
export {
  fadeIn,
  fadeInUp,
  fadeInDown,
  fadeInLeft,
  fadeInRight,
  staggerContainer,
  defaultViewThreshold,
  cardHover,
  buttonHover
}; 