/*
Copyright 2023 New Vector Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import React, { memo, ReactNode, RefObject, useRef } from "react";
import { EventTypes, Handler, useDrag } from "@use-gesture/react";
import { SpringValue, to } from "@react-spring/web";

import { ChildrenProperties } from "./VideoGrid";

interface Props<T> {
  id: string;
  onDragRef: RefObject<
    (
      tileId: string,
      state: Parameters<Handler<"drag", EventTypes["drag"]>>[0]
    ) => void
  >;
  targetWidth: number;
  targetHeight: number;
  data: T;
  opacity: SpringValue<number>;
  scale: SpringValue<number>;
  shadow: SpringValue<number>;
  shadowSpread: SpringValue<number>;
  zIndex: SpringValue<number>;
  x: SpringValue<number>;
  y: SpringValue<number>;
  width: SpringValue<number>;
  height: SpringValue<number>;
  children: (props: ChildrenProperties<T>) => ReactNode;
}

/**
 * A wrapper around a tile in a video grid. This component exists to decouple
 * child components from the grid.
 */
export const TileWrapper = memo(
  ({
    id,
    onDragRef,
    targetWidth,
    targetHeight,
    data,
    opacity,
    scale,
    shadow,
    shadowSpread,
    zIndex,
    x,
    y,
    width,
    height,
    children,
  }) => {
    const ref = useRef<HTMLElement | null>(null);

    useDrag((state) => onDragRef?.current!(id, state), {
      target: ref,
      filterTaps: true,
      preventScroll: true,
    });

    return (
      <>
        {children({
          ref,
          style: {
            opacity,
            scale,
            zIndex,
            x,
            y,
            width,
            height,
            boxShadow: to(
              [shadow, shadowSpread],
              (s, ss) => `rgba(0, 0, 0, 0.5) 0px ${s}px ${2 * s}px ${ss}px`
            ),
          },
          targetWidth,
          targetHeight,
          data,
        })}
      </>
    );
  }
  // We pretend this component is a simple function rather than a
  // NamedExoticComponent, because that's the only way we can fit in a type
  // parameter
) as <T>(props: Props<T>) => JSX.Element;
