import { Textfit } from "react-textfit";
import {
  TextElementData,
  ElementComponent,
  ElementProps,
  ElementTypeName,
  ShowHideDefaultVisibility,
} from "./types";
import useBaseElementStyles from "./hooks/useBaseElementStyles";
import useEditableFeatures from "./hooks/useEditableFeatures";

function TextLegacy(props: ElementProps<TextElementData>) {
  const { resizeHandles, moveListeners, currentDimensions } =
    useEditableFeatures(props);
  const { baseClasses, baseStyles } = useBaseElementStyles(
    currentDimensions,
    props.scaleFactor,
    props.isEditable,
  );

  const { content, color } = props.element;

  return (
    <div
      className={baseClasses}
      style={{
        ...baseStyles,
      }}
      {...moveListeners}
    >
      <Textfit
        mode="multi"
        style={{
          position: "absolute",
          width: "100%",
          height: "100%",
          color,
          wordBreak: "break-all",
        }}
      >
        {content}
      </Textfit>
      {resizeHandles}
    </div>
  );
}

const TextLegacyForExport =
  TextLegacy as unknown as ElementComponent<TextElementData>;

TextLegacyForExport.elementName = "LEGACY Text";
TextLegacyForExport.description =
  "A legacy plain-text field.  New presentations should NOT use this element.";
TextLegacyForExport.icon = "deprecated";
TextLegacyForExport.blankFactory = (newId, newZ, data) => ({
  type: ElementTypeName.TextLegacy,
  name: "New LEGACY Text",

  id: newId,
  x: data.metadata.width / 2 - 250,
  y: data.metadata.height / 2 - 150,
  z: newZ,

  isLocked: false,
  width: 500,
  height: 300,

  content:
    "You should not use this element in new presentations!  Use the rich text element instead.",
  color: "#000000",

  legacy: true,

  visible: {
    default: ShowHideDefaultVisibility.Show,
    except: [],
  },
});
TextLegacyForExport.clone = (source, newId, newName, newZ) => ({
  type: ElementTypeName.TextLegacy,
  name: newName,

  id: newId,
  x: source.x,
  y: source.y,
  z: newZ,

  isLocked: source.isLocked,
  width: source.width,
  height: source.height,

  content: source.content,
  color: source.color,

  legacy: true,

  visible: source.visible,
});

export default TextLegacyForExport;
