/**
 * Copyright 2024 Phenix Real Time Solutions, Inc. Confidential and Proprietary. All Rights Reserved.
 */
import React, {useState} from 'react';
import {IconDefinition} from '@fortawesome/free-solid-svg-icons';

import {IconMenu} from 'components/icon-menu';
import {IconMenuPosition} from 'components/icon-menu/types';

import ForkChannelModal from './fork-channel';
import DeleteChannelModal from './delete-channel';
import KillChannelModal from './kill-channel';

interface IChannelIconMenu {
  data: {
    name: string;
    channelId: string;
    applicationId: string;
    alias: string;
  };
  redirect?: () => void;
  showTail?: boolean;
  position?: IconMenuPosition;
  icon?: IconDefinition;
  margin?: number;
}

export const ChannelIconMenu = (props: IChannelIconMenu): React.JSX.Element => {
  const {data, icon, redirect, showTail, position, margin} = props;
  const {alias, channelId} = data;
  const [isForkModalOpen, setIsForkModalOpen] = useState(false);
  const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
  const [isKillModalOpen, setIsKillModalOpen] = useState(false);
  const items = [
    {
      value: 'delete',
      title: 'Delete',
      className: 'testId-deleteMenuItem',
      onClick: () => setIsDeleteModalOpen(true)
    },
    {
      value: 'kill',
      title: 'Kill',
      className: 'testId-killMenuItem',
      onClick: () => setIsKillModalOpen(true)
    },
    {
      value: 'fork',
      title: 'Fork',
      className: 'testId-forkMenuItem',
      onClick: () => setIsForkModalOpen(true)
    }
  ];

  return (
    <>
      <IconMenu icon={icon} items={items} showTail={showTail} position={position} margin={margin} />
      {isForkModalOpen && <ForkChannelModal isOpen={isForkModalOpen} setIsOpen={setIsForkModalOpen} channelId={channelId} alias={alias} />}
      {isDeleteModalOpen && (
        <DeleteChannelModal isOpen={isDeleteModalOpen} setIsOpen={setIsDeleteModalOpen} channelId={channelId} alias={alias} redirect={redirect} />
      )}
      {isKillModalOpen && <KillChannelModal isOpen={isKillModalOpen} setIsOpen={setIsKillModalOpen} channelId={channelId} alias={alias} />}
    </>
  );
};
