import { Button, Col, Popover, Row } from 'antd'
import classNames from 'classnames'
import DragDropButton from 'components/Button/DragDropButton'
import ConfirmDeleteModal from 'components/ConfirmDeleteModal'
import ImageSkeleton from 'components/ImageSkeleton'
import LabelTooltip from 'components/LabelTooltip/LabelTooltip'
import { useWindowSize } from 'hooks'
import { MediaGalleryItem } from 'models/talent/talent-profile-module.model'
import ItemVisibilityToggle from 'pages/Profile/ModuleEditor/ItemVisibilityToggle'
import React, { useMemo, useState } from 'react'
import { DraggableProvidedDragHandleProps } from 'react-beautiful-dnd'
import { BaseModuleProps } from 'redux/Common/types'
import CardModuleAction from '../CardModuleAction'
import { Icon } from '../Icon'
import './MediaGalleryCardModule.scss'
import { isIframeVideo } from '../../pages/Profile/Modules/ModuleMediaGallery'
import { FLAGS, useFeatureIsOn } from '@komi-app/flags-sdk'

interface Props extends BaseModuleProps {
  index?: number
  item: MediaGalleryItem
  loading?: boolean
  disableEdit?: boolean
  disableDelete?: boolean
  disableDrag?: boolean
  titleShortcut?: boolean
  onEdit?: (item: MediaGalleryItem) => void
  onDelete?: (item: MediaGalleryItem) => void
  dragHandleProps?: DraggableProvidedDragHandleProps
}

const MediaGalleryCardModule: React.FC<Props> = ({
  item,
  disableDrag = false,
  dragHandleProps,
  onEdit,
  onDelete,
  module
}) => {
  const [showDeleteConfirm, setShowDeleteConfirm] = useState(false)
  const [visibleAction, setVisibleAction] = useState(false)

  const { width } = useWindowSize()
  const isMobileStyleChangesEnabled = width && width <= 768

  const onDeleteConfirm = () => {
    typeof onDelete === 'function' && onDelete(item)
    setShowDeleteConfirm(false)
  }

  const isVideoEnabled = useFeatureIsOn(
    FLAGS.FEAT_SB_637_ADD_VIDEOS_TO_MEDIA_GALLERY
  )

  const thumbnailSrc = useMemo(() => {
    if (!isVideoEnabled) {
      return item.thumbnailSrc || item.src
    }

    if (isIframeVideo(item.src)) {
      const url = new URL(item.thumbnailSrc || '')
      url.searchParams.set('height', '220')
      return url.toString()
    }

    return item.thumbnailSrc || item.src
  }, [item.src, item.thumbnailSrc, isVideoEnabled])

  return (
    <React.Fragment>
      <ConfirmDeleteModal
        show={showDeleteConfirm}
        toggleModal={() => setShowDeleteConfirm(false)}
        cancelLabel="No Thanks!"
        confirmLabel="Delete"
        title="Are you sure you want to delete this link?"
        description="This link will be deleted and you can’t undo this action."
        handleConfirm={onDeleteConfirm}
      />

      <div className="border-box d--flex media-gallery-card border__radius--8">
        <Row wrap={false} className="full-width">
          <Col className="media-gallery-card__picture">
            <ImageSkeleton src={thumbnailSrc} />
          </Col>
          <Col flex={'1'}>
            <Row
              align="middle"
              className="media-gallery-card__content p__x--16 p__y--16"
              wrap={false}
            >
              <Col flex="1">
                <div>
                  <LabelTooltip
                    preset="semibold16"
                    className={classNames('d--block', {
                      'media-gallery-card__title': !isMobileStyleChangesEnabled
                    })}
                    label={item.caption || item.originalFilename}
                    ellipsis={true}
                    maxLength={
                      item.caption?.length || item.originalFilename?.length
                    }
                    showTooltip={
                      (item.caption?.length || item.originalFilename?.length) >
                      36
                    }
                  />
                  {item.url && (
                    <LabelTooltip
                      preset="regular14"
                      className="d--block m__t--4 text--grey4"
                      label={item.url}
                      ellipsis={{ rows: 1 }}
                      maxLength={item.url.length}
                      showTooltip={item.url.length > 36}
                    />
                  )}
                </div>
              </Col>
              <Col className="p__l--24">
                <Row wrap={false} gutter={16} align="middle">
                  <Col>
                    <ItemVisibilityToggle module={module} item={item} />
                  </Col>
                  <Col>
                    <Popover
                      visible={visibleAction}
                      placement="bottomRight"
                      overlayClassName="module-editor-overlay"
                      arrowContent={null}
                      content={
                        <CardModuleAction
                          onEdit={() => onEdit?.(item)}
                          onDelete={() => setShowDeleteConfirm(true)}
                          toggleVisible={setVisibleAction}
                        />
                      }
                      trigger={['click']}
                      onVisibleChange={setVisibleAction}
                    >
                      <Button
                        className="p--0 d--flex align__items--center height--24 min-h-auto"
                        type="link"
                        onClick={() => setVisibleAction(true)}
                      >
                        <Icon name="more" />
                      </Button>
                    </Popover>
                  </Col>
                  <Col>
                    <DragDropButton
                      className="p__x--0 p__y--0"
                      dragType="vertical"
                      dragHandleProps={dragHandleProps}
                      disabled={disableDrag}
                    />
                  </Col>
                </Row>
              </Col>
            </Row>
          </Col>
        </Row>
      </div>
    </React.Fragment>
  )
}

export default MediaGalleryCardModule
