import { Button, Col, Popover, Row } from "antd";
import DragDropButton from "components/Button/DragDropButton";
import ConfirmDeleteModal from "components/ConfirmDeleteModal";
import ImageSkeleton from "components/ImageSkeleton";
import LabelTooltip from "components/LabelTooltip/LabelTooltip";
import {
  BaseItem,
  PodcastItem,
} 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 "./PodcastCardModule.scss";

interface Props extends BaseModuleProps {
  index?: number;
  item: PodcastItem;
  loading?: boolean;
  disableDragDrop?: boolean;
  titleShortcut?: boolean;
  disableDrag?: boolean;
  deleteTitle?: string;
  deleteSubTitle?: string;
  onEdit?: (item: PodcastItem) => void;
  onDelete?: (item: PodcastItem) => void;
  onCopySmartLink?: (item: PodcastItem) => void;
  dragHandleProps?: DraggableProvidedDragHandleProps;
  rightContent?: React.ReactNode;
}

const PodcastCardModule: React.FC<Props> = ({
  item,
  titleShortcut = false,
  disableDrag = false,
  dragHandleProps,
  onEdit,
  onDelete,
  deleteTitle,
  deleteSubTitle,
  onCopySmartLink,
  rightContent,
  module,
}) => {
  const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
  const [visibleAction, setVisibleAction] = useState(false);
  const data = item.metadata || item;
  const subTitle = useMemo(() => {
    if (data?.artists?.length) {
      return data.artists.map((item: any) => item.name).join(", ");
    }
    return data.author || data?.publisher || item?.links?.[0]?.url || "";
  }, [data, item?.links]);

  const onDeleteConfirm = () => {
    typeof onDelete === "function" && onDelete(item);
    setShowDeleteConfirm(false);
  };
  const image = data?.images?.[0];
  return (
    <React.Fragment>
      <ConfirmDeleteModal
        show={showDeleteConfirm}
        toggleModal={() => setShowDeleteConfirm(false)}
        cancelLabel="No Thanks!"
        confirmLabel="Delete"
        title={deleteTitle || "Are you sure you want to delete this track?"}
        description={
          deleteSubTitle ||
          "This track will be deleted and you can’t undo this action."
        }
        handleConfirm={onDeleteConfirm}
      />

      <div className="border-box d--flex podcast-card border__radius--8">
        <Row wrap={false} className="podcast-card__content">
          <Col className="podcast-card__picture">
            <ImageSkeleton src={image?.url || image} />
          </Col>
          <Col flex={"1"}>
            <Row align="middle" className="p__y--16 p__x--16" wrap={false}>
              <Col flex="1">
                <LabelTooltip
                  preset="semibold16"
                  className="d--block "
                  label={data?.name || data?.trackName || ""}
                  ellipsis={{ rows: 1 }}
                  maxLength={(data?.name || data?.trackName || "")?.length}
                  showTooltip={
                    (data?.name || data?.trackName || "")?.length > 36
                  }
                />
                <LabelTooltip
                  preset="regular14"
                  className="d--block m__t--4 text--grey4"
                  label={subTitle as string}
                  ellipsis={{ rows: 1 }}
                  maxLength={(subTitle as string).length}
                  showTooltip={(subTitle as string).length > 36}
                />
              </Col>
              <Col className="p__l--24 height--24">
                {rightContent ? (
                  rightContent
                ) : (
                  <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
                            copySmartLink={!!item.id}
                            onCopyLink={() => onCopySmartLink?.(item)}
                            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 PodcastCardModule;
