import { motion } from "framer-motion";
import { forwardRef } from "react";

export const Hexagone = forwardRef((props: any, ref) => {
  const { shape } = props;
  const GradientType =
    shape?.gradient?.type === "radialGradient"
      ? "radialGradient"
      : "linearGradient";
  return (
    <svg
      ref={ref as any}
      width={props?.width ?? 303}
      height={props?.height ?? 303}
      viewBox="0 0 272 305"
      fill="none"
      id={props?.id ?? undefined}
      xmlns="http://www.w3.org/2000/svg"
    >
      <defs>
        {shape?.image?.enabled && shape?.image?.urls?.length > 0 && (
          <pattern
            id={`shape-gradient-${shape?.component ?? "null"}`}
            x="0"
            y="0"
            patternUnits="userSpaceOnUse"
            width={303}
            height={303}
          >
            <image
              href={shape?.image?.urls[0]}
              width="100%"
              height="100%"
              preserveAspectRatio="xMidYMid slice"
            />
          </pattern>
        )}
        <GradientType
          id={`shape-gradient-${shape?.component ?? "null"}`}
          gradientTransform={`rotate(${shape?.gradient?.angle ?? "90"})`}
        >
          {shape?.gradient?.enabled ? (
            <>
              {shape?.gradient?.colours?.map((color: any, c: number) => (
                <stop
                  key={c}
                  offset={`${color.left}%`}
                  stopColor={color.value}
                />
              ))}
            </>
          ) : (
            <stop offset="0%" stopColor={shape?.colour ?? "#fff"} />
          )}
        </GradientType>
      </defs>
      <defs>
        <mask id={`mask-${shape?.component ?? "null"}`} fill="black">
          <rect width="100%" height="100%" fill="white" />
          <g
            style={{
              transformOrigin: "center",
            }}
            opacity={100}
          >
            {shape?.transparency && props?.children && props.children}
          </g>
        </mask>
      </defs>
      <g fill={`url(#${`shape-gradient-${shape?.component ?? "null"}`})`}>
        <g mask={`url(#${`mask-${shape?.component ?? "null"}`})`}>
          <motion.path
            d="M256.123 64.864L151.077 4.114a30.713 30.713 0 00-30.715 0L15.337 64.865A30.714 30.714 0 000 91.482v121.172a30.709 30.709 0 0015.358 26.618l104.984 60.688a30.722 30.722 0 0030.715 0l105.025-60.688a30.722 30.722 0 0015.358-26.618V91.462a30.705 30.705 0 00-15.317-26.598z"
            animate={{
              scaleX:
                shape?.border?.width > 0 ? 1 - shape?.border?.width / 350 : 1,
              scaleY:
                shape?.border?.width > 0 ? 1 - shape?.border?.width / 350 : 1,
              rotate: shape?.position?.angle ?? 0,
            }}
            stroke={shape?.border?.colour}
            strokeLinejoin="round"
            strokeWidth={shape?.border?.width}
            onClick={props?.onClick ? props.onClick : () => {}}
          />
        </g>
      </g>
      {!shape?.transparency && props?.children && props.children}
    </svg>
  );
});
