import { FC } from 'react';
import { ButtonGroup, Button } from '@servicetitan/design-system';
import { Confirm } from '@servicetitan/confirm';
import { ActionButtonArchive } from '../action-button/action-button-archive';
import { ActionButtonUnarchive } from '../action-button/action-button-unarchive';
import { ActionsButton } from './actions-button';
import { ActionButtonClone } from '../action-button/action-button-clone';
import { ActionButtonEdit } from '../action-button/action-button-edit';

interface CampaignActionsProps {
    archivedDisabledTooltip?: string;
    onClickEdit?(): void;
    onClickStop?(): void;
    onClickClone?(): void;
    onClickArchive?(): void;
    onClickUnarchive?(): void;
}

export const CampaignActions: FC<CampaignActionsProps> = ({
    archivedDisabledTooltip,
    onClickEdit,
    onClickStop,
    onClickClone,
    onClickArchive,
    onClickUnarchive,
}) => {
    const stopAction = (handleClick: () => void) => (
        <Button outline onClick={handleClick} small negative className="qa-campaign-actions-stop">
            Stop
        </Button>
    );

    const archiveAction = (handleClick: () => void) => (
        <ActionButtonArchive onClick={handleClick} />
    );

    const unarchiveAction = (handleClick: () => void) => (
        <ActionButtonUnarchive onClick={handleClick} />
    );

    return (
        <ButtonGroup>
            {onClickStop && (
                <Confirm
                    onConfirm={onClickStop}
                    title="Confirm stop"
                    message="Are you sure you want to stop this campaign?"
                    negative
                >
                    {stopAction}
                </Confirm>
            )}
            <ActionsButton>
                {onClickClone && <ActionButtonClone onClick={onClickClone} />}
                {onClickArchive ? (
                    <Confirm
                        onConfirm={onClickArchive}
                        title="Confirm archive"
                        message="Are you sure you want to archive this campaign?"
                        negative={false}
                    >
                        {archiveAction}
                    </Confirm>
                ) : (
                    <ActionButtonArchive disabled tooltip={archivedDisabledTooltip} />
                )}
                {onClickUnarchive && (
                    <Confirm
                        onConfirm={onClickUnarchive}
                        title="Confirm unarchive"
                        message="Are you sure you want to unarchive this campaign?"
                        negative={false}
                    >
                        {unarchiveAction}
                    </Confirm>
                )}
                {onClickEdit && <ActionButtonEdit onClick={onClickEdit} />}
            </ActionsButton>
        </ButtonGroup>
    );
};
