import React, { useEffect } from 'react';
import { animated, useSpring } from 'react-spring';
import { useTheme } from 'styled-components';
import { usePrevious } from '../hooks/';

type AnimatedFavoriteIconProps = {
  active: boolean;
  variant?: 'default' | 'light';
  scaleBy?: number;
  color?: string;
  width?: number;
  height?: number;
  onAnimationComplete?: () => void;
};

const STEP_DURATION_MS = 200;
export const TOTAL_DURATION_MS = STEP_DURATION_MS * 2;

const AnimatedFavoriteIcon = ({
  active,
  variant = 'default',
  color = '#000000',
  width = 24,
  height = 24,
  scaleBy = 1.4,
  onAnimationComplete,
}: AnimatedFavoriteIconProps) => {
  const { colors } = useTheme();
  const prevActive = usePrevious(active);

  useEffect(() => {
    if (active && !prevActive) {
      setTimeout(() => {
        onAnimationComplete?.();
      }, TOTAL_DURATION_MS);
    }
  }, [active, prevActive]);

  const fadeInAfterAnimationComplete = useSpring({
    opacity: active ? 1 : 0,
    delay: TOTAL_DURATION_MS,
    config: { duration: TOTAL_DURATION_MS },
  });

  const outlineAnimation = useSpring({
    fill: active
      ? colors.primary[variant === 'light' ? 400 : 700]
      : colors.neutral[600],
    config: { duration: STEP_DURATION_MS },
  });

  const growAnimationFirst = useSpring({
    transform: active ? 'scale(1)' : 'scale(0)',
    config: { duration: STEP_DURATION_MS },
  });

  const growAnimationSecond = useSpring({
    transform: active ? 'scale(1)' : 'scale(0)',
    delay: STEP_DURATION_MS,
    config: { duration: STEP_DURATION_MS },
  });

  const { keyframe } = useSpring({
    keyframe: active ? 1 : 0,
    delay: 0,
    config: { duration: active ? TOTAL_DURATION_MS : 0 },
  });

  return (
    <animated.svg
      width={width}
      height={height}
      fill="none"
      xmlns="http://www.w3.org/2000/svg"
      viewBox="0 0 24 24"
      style={{
        transformOrigin: '50% 50%',
        transform: keyframe
          .to([0, 0.3, 0.6, 1], [1, scaleBy, 1, 1])
          .to((x) => `scale(${x})`),
      }}
    >
      {!active ? (
        <path
          d="M19.6485 9.29464L14.8085 8.87464L12.9185 4.42464C12.5785 3.61464 11.4185 3.61464 11.0785 4.42464L9.18849 8.88464L4.35849 9.29464C3.47849 9.36464 3.11849 10.4646 3.78849 11.0446L7.45849 14.2246L6.35849 18.9446C6.15849 19.8046 7.08849 20.4846 7.84849 20.0246L11.9985 17.5246L16.1485 20.0346C16.9085 20.4946 17.8385 19.8146 17.6385 18.9546L16.5385 14.2246L20.2085 11.0446C20.8785 10.4646 20.5285 9.36464 19.6485 9.29464ZM11.9985 15.6546L8.23849 17.9246L9.23849 13.6446L5.91849 10.7646L10.2985 10.3846L11.9985 6.35464L13.7085 10.3946L18.0885 10.7746L14.7685 13.6546L15.7685 17.9346L11.9985 15.6546Z"
          fill={color}
        />
      ) : (
        <>
          {/* First growing star [lowest z-index] - don't show on the light variant */}
          {variant == 'default' && (
            <animated.path
              d="M12 17.5195L16.15 20.0295C16.91 20.4895 17.8399 19.8095 17.6399 18.9495L16.54 14.2295L20.2099 11.0495C20.8799 10.4695 20.5199 9.36952 19.6399 9.29952L14.81 8.88952L12.92 4.42952C12.58 3.61952 11.42 3.61952 11.08 4.42952L9.18995 8.87952L4.35995 9.28952C3.47995 9.35952 3.11995 10.4595 3.78995 11.0395L7.45995 14.2195L6.35995 18.9395C6.15995 19.7995 7.08995 20.4795 7.84995 20.0195L12 17.5195Z"
              fill={colors.primary[500]}
              style={{ transformOrigin: '50% 50%' }}
              {...growAnimationFirst}
            />
          )}

          {/* Star outline */}
          <animated.path
            d="M19.6485 9.29464L14.8085 8.87464L12.9185 4.42464C12.5785 3.61464 11.4185 3.61464 11.0785 4.42464L9.18849 8.88464L4.35849 9.29464C3.47849 9.36464 3.11849 10.4646 3.78849 11.0446L7.45849 14.2246L6.35849 18.9446C6.15849 19.8046 7.08849 20.4846 7.84849 20.0246L11.9985 17.5246L16.1485 20.0346C16.9085 20.4946 17.8385 19.8146 17.6385 18.9546L16.5385 14.2246L20.2085 11.0446C20.8785 10.4646 20.5285 9.36464 19.6485 9.29464ZM11.9985 15.6546L8.23849 17.9246L9.23849 13.6446L5.91849 10.7646L10.2985 10.3846L11.9985 6.35464L13.7085 10.3946L18.0885 10.7746L14.7685 13.6546L15.7685 17.9346L11.9985 15.6546Z"
            {...outlineAnimation}
          />

          {/* Second growing star */}
          <animated.path
            d="M12 17.5195L16.15 20.0295C16.91 20.4895 17.8399 19.8095 17.6399 18.9495L16.54 14.2295L20.2099 11.0495C20.8799 10.4695 20.5199 9.36952 19.6399 9.29952L14.81 8.88952L12.92 4.42952C12.58 3.61952 11.42 3.61952 11.08 4.42952L9.18995 8.87952L4.35995 9.28952C3.47995 9.35952 3.11995 10.4595 3.78995 11.0395L7.45995 14.2195L6.35995 18.9395C6.15995 19.7995 7.08995 20.4795 7.84995 20.0195L12 17.5195Z"
            fill={colors.primary[variant === 'light' ? 500 : 700]}
            style={{ transformOrigin: '50% 50%' }}
            {...growAnimationSecond}
          />

          {/* Final filled star [highest z-index] */}
          <animated.path
            d="M12 17.5195L16.15 20.0295C16.91 20.4895 17.8399 19.8095 17.6399 18.9495L16.54 14.2295L20.2099 11.0495C20.8799 10.4695 20.5199 9.36952 19.6399 9.29952L14.81 8.88952L12.92 4.42952C12.58 3.61952 11.42 3.61952 11.08 4.42952L9.18995 8.87952L4.35995 9.28952C3.47995 9.35952 3.11995 10.4595 3.78995 11.0395L7.45995 14.2195L6.35995 18.9395C6.15995 19.7995 7.08995 20.4795 7.84995 20.0195L12 17.5195Z"
            fill={color}
            {...fadeInAfterAnimationComplete}
          />
        </>
      )}
    </animated.svg>
  );
};

export default AnimatedFavoriteIcon;
