import dayjs from "dayjs";
import advancedFormat from "dayjs/plugin/advancedFormat";
import localizedFormat from "dayjs/plugin/localizedFormat";
import timezonePlugin from "dayjs/plugin/timezone";
import utc from "dayjs/plugin/utc";
import weekOfYear from "dayjs/plugin/weekOfYear";
import weekYear from "dayjs/plugin/weekYear";
import { useMemo } from "react";
import { Textfit } from "react-textfit";
import {
  Alignment,
  ClockElementData,
  ElementComponent,
  ElementProps,
  ElementTypeName,
  ShowHideDefaultVisibility,
} from "./types";
import useBaseElementStyles from "./hooks/useBaseElementStyles";
import useEditableFeatures from "./hooks/useEditableFeatures";

dayjs.extend(localizedFormat);
dayjs.extend(weekOfYear);
dayjs.extend(weekYear);
dayjs.extend(advancedFormat);
dayjs.extend(utc);
dayjs.extend(timezonePlugin);

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

  const { color, fstring, timezone, align } = props.element;

  const format = fstring.length ? fstring : "llll";

  const justify = useMemo(() => {
    switch (align) {
      case Alignment.LEFT:
        return "flex-start";
      case Alignment.CENTER:
        return "center";
      case Alignment.RIGHT:
        return "flex-end";
      default:
        return "center";
    }
  }, [align]);

  return (
    <div
      className={baseClasses}
      style={{
        ...baseStyles,
      }}
      {...moveListeners}
    >
      <Textfit
        mode="single"
        forceSingleModeWidth={false}
        style={{
          position: "absolute",
          width: "100%",
          height: "100%",
          display: "flex",
          alignItems: "center",
          justifyContent: justify,
          textAlign: "center",
          color,
        }}
      >
        {fstring.trim().length
          ? dayjs(props.viewTime).tz(timezone).format(format)
          : "Invalid format"}
      </Textfit>
      {resizeHandles}
    </div>
  );
}

const ClockForExport = Clock as unknown as ElementComponent<ClockElementData>;

ClockForExport.elementName = "Clock";
ClockForExport.description =
  "A dynamic text clock which can be customized to show the time, date, and much more.";
ClockForExport.icon = "clock";
ClockForExport.blankFactory = (newId, newZ, data) => ({
  type: ElementTypeName.Clock,
  name: "New Clock",

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

  isLocked: false,
  width: 300,
  height: 100,

  fstring: "ddd, MMM D, YYYY h:mm A",
  timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
  color: "#000000",
  align: Alignment.CENTER,

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

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

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

  fstring: source.fstring,
  timezone: source.timezone,
  color: source.color,
  align: source.align,

  visible: source.visible,
});

export default ClockForExport;
