import {
  Button,
  ButtonGroup,
  Field as FieldComponent,
  FieldLabel,
  ModalBody,
  ModalFooter,
  ModalHeader,
  TextInput,
} from "@contentstack/venus-components"
import { ChangeEvent, useEffect, useState } from "react"

import FocalPointPicker from "~/shared-components/FocalPointPicker"
import type { FocalPoint, ImageVaultAsset } from "~/types/imagevault"

type ImageEditModalProps = {
  fieldData: ImageVaultAsset
  setData: (data: ImageVaultAsset) => void
  closeModal: () => void
}

export default function ImageEditModal({
  fieldData,
  closeModal,
  setData,
}: ImageEditModalProps) {
  const [altText, setAltText] = useState("")
  const [caption, setCaption] = useState("")
  const [focalPoint, setFocalPoint] = useState<FocalPoint>({ x: 50, y: 50 })

  const assetUrl = fieldData.url

  useEffect(() => {
    if (fieldData.meta.alt) {
      setAltText(fieldData.meta.alt)
    }
    if (fieldData.meta.caption) {
      setCaption(fieldData.meta.caption)
    }
  }, [fieldData.meta])

  useEffect(() => {
    if (fieldData.focalPoint) {
      setFocalPoint(fieldData.focalPoint)
    }
  }, [fieldData.focalPoint])

  function handleSave() {
    const newData = {
      ...fieldData,
      meta: {
        alt: altText,
        caption,
      },
      focalPoint,
    }
    setData(newData)
    closeModal()
  }

  function changeFocalPoint(focalPoint: FocalPoint) {
    setFocalPoint(focalPoint)
  }

  return (
    <>
      <ModalHeader title="Update image" closeModal={closeModal} />
      <ModalBody
        style={{
          display: "grid",
          gridTemplateColumns: "1fr minmax(max-content, 250px)",
          gap: "1rem",
          alignItems: "center",
          width: "auto",
          maxHeight: "calc(90dvh - 124px)", // 124px is the height of the header and footer
          maxWidth: "90dvw",
          overflow: "auto",
        }}
      >
        <FocalPointPicker
          imageSrc={assetUrl}
          focalPoint={focalPoint}
          onChange={changeFocalPoint}
        />
        <div>
          <FieldComponent>
            <FieldLabel htmlFor="alt">Alt text</FieldLabel>
            <TextInput
              value={altText}
              placeholder="Alt text for image"
              name="alt"
              onChange={(e: ChangeEvent<HTMLInputElement>) =>
                setAltText(e.target.value)
              }
            />
          </FieldComponent>

          <FieldComponent>
            <FieldLabel htmlFor="caption">Caption</FieldLabel>
            <TextInput
              value={caption}
              placeholder="Caption for image..."
              name="caption"
              onChange={(e: ChangeEvent<HTMLInputElement>) =>
                setCaption(e.target.value)
              }
            />
          </FieldComponent>

          <FieldComponent>
            <FieldLabel htmlFor="focalPoint">Focal Point</FieldLabel>
            <TextInput
              value={`X: ${focalPoint.x}, Y: ${focalPoint.y}`}
              name="focalPoint"
              disabled
            />
          </FieldComponent>

          <FieldComponent>
            <FieldLabel htmlFor="imageVaultId">Imagevault Id</FieldLabel>
            <TextInput
              value={fieldData.imageVaultId}
              name="imageVaultId"
              disabled
            />
          </FieldComponent>
        </div>
      </ModalBody>
      <ModalFooter>
        <ButtonGroup>
          <Button onClick={closeModal} buttonType="light">
            Cancel
          </Button>
          <Button onClick={handleSave} icon="SaveWhite">
            Save
          </Button>
        </ButtonGroup>
      </ModalFooter>
    </>
  )
}
