import { DetailsRadioOption } from '@common-ui/inputs/details-radio/details-radio-option';
import { AnnotationWrapper } from '@models/file/annotation.wrapper';
import { Dossier } from '@red/domain';
import { addHintTranslations } from '@translations/add-hint-translations';
import { editRedactionTranslations, redactTextTranslations } from '@translations/redact-text-translations';
import { removeAnnotationTranslations } from '@translations/remove-annotation-translations';
import { removeRedactionTranslations } from '@translations/remove-redaction-translations';
import { resizeRedactionTranslations } from '@translations/resize-redaction-translations';
import {
    EditRedactionOption,
    ForceAnnotationOption,
    RectangleRedactOption,
    RectangleRedactOptions,
    RedactOrHintOption,
    RedactOrHintOptions,
    RemoveRedactionData,
    RemoveRedactionOption,
    RemoveRedactionOptions,
    ResizeOptions,
    ResizeRedactionOption,
} from './dialog-types';
import {
    editRectangleTranslations,
    rectangleRedactTranslations,
    removeRectangleTranslations,
} from '@translations/rectangle-redact-translations';

const PIN_ICON = 'red:push-pin';
const DOCUMENT_ICON = 'iqser:document';
const FOLDER_ICON = 'red:folder';
const REMOVE_FROM_DICT_ICON = 'red:remove-from-dict';

export const getEditRedactionOptions = (hint: boolean): DetailsRadioOption<EditRedactionOption>[] => {
    if (hint) {
        return [];
    }

    return [
        {
            label: editRedactionTranslations.onlyHere.label,
            description: editRedactionTranslations.onlyHere.description,
            icon: PIN_ICON,
            value: RedactOrHintOptions.ONLY_HERE,
        },
        {
            label: editRedactionTranslations.inDocument.label,
            description: editRedactionTranslations.inDocument.description,
            icon: DOCUMENT_ICON,
            value: RedactOrHintOptions.IN_DOCUMENT,
        },
    ];
};

export const getRedactOrHintOptions = (
    dossier: Dossier,
    applyToAllDossiers: boolean,
    isApprover: boolean,
    isPageExcluded = false,
    isRecommendation = false,
    isHint = false,
    isRss = false,
): DetailsRadioOption<RedactOrHintOption>[] => {
    const translations = isHint ? addHintTranslations : redactTextTranslations;
    const options: DetailsRadioOption<RedactOrHintOption>[] = [];

    if (!isRecommendation) {
        options.push({
            label: translations.onlyHere.label,
            description: translations.onlyHere.description,
            icon: PIN_ICON,
            value: RedactOrHintOptions.ONLY_HERE,
        });
    }

    if (isRss) {
        return options;
    }

    if (!isHint) {
        options.push({
            label: redactTextTranslations.inDocument.label,
            description: redactTextTranslations.inDocument.description,
            icon: DOCUMENT_ICON,
            value: RedactOrHintOptions.IN_DOCUMENT,
        });
    }

    options.push({
        label: translations.inDossier.label,
        description: translations.inDossier.description,
        descriptionParams: { dossierName: dossier.dossierName },
        icon: FOLDER_ICON,
        value: RedactOrHintOptions.IN_DOSSIER,
        disabled: isPageExcluded,
        additionalCheck: {
            label: translations.inDossier.extraOptionLabel,
            checked: applyToAllDossiers,
            hidden: !isApprover,
        },
    });

    return options;
};

export const getRectangleRedactOptions = (action: 'add' | 'edit' | 'remove' = 'add'): DetailsRadioOption<RectangleRedactOption>[] => {
    const translations =
        action === 'add' ? rectangleRedactTranslations : action === 'edit' ? editRectangleTranslations : removeRectangleTranslations;
    return [
        {
            label: translations.onlyThisPage.label,
            description: translations.onlyThisPage.description,
            icon: PIN_ICON,
            value: RectangleRedactOptions.ONLY_THIS_PAGE,
        },
        {
            label: translations.multiplePages.label,
            description: translations.multiplePages.description,
            icon: DOCUMENT_ICON,
            value: RectangleRedactOptions.MULTIPLE_PAGES,
            additionalInput: {
                label: translations.multiplePages.extraOptionLabel,
                description: translations.multiplePages.extraOptionDescription,
                placeholder: translations.multiplePages.extraOptionPlaceholder,
                value: '',
                errorCode: 'invalidRange',
            },
        },
    ];
};

export const getResizeRedactionOptions = (
    redaction: AnnotationWrapper,
    dossier: Dossier,
    isRss: boolean,
    applyToAllDossiers: boolean,
    isApprover: boolean,
    canResizeInDictionary: boolean,
): DetailsRadioOption<ResizeRedactionOption>[] => {
    if (isRss || !canResizeInDictionary) {
        return [];
    }

    const translations = resizeRedactionTranslations;
    const dictBasedType = redaction.isModifyDictionary;

    return [
        {
            label: translations.onlyHere.label,
            description: translations.onlyHere.description,
            icon: PIN_ICON,
            value: ResizeOptions.ONLY_HERE,
        },
        {
            label: translations.inDossier.label,
            description: translations.inDossier.description,
            descriptionParams: { dossierName: dossier.dossierName },
            disabled: !dictBasedType || redaction.hasBeenRecategorized,
            tooltip: !dictBasedType ? translations.inDossier.tooltip : null,
            icon: FOLDER_ICON,
            value: ResizeOptions.IN_DOSSIER,
            additionalCheck: {
                label: translations.inDossier.extraOptionLabel,
                checked: applyToAllDossiers,
                hidden: !isApprover,
            },
        },
    ];
};

export const getRemoveRedactionOptions = (
    data: RemoveRedactionData,
    applyToAllDossiers: boolean,
    isDocumine: boolean = false,
): DetailsRadioOption<RemoveRedactionOption>[] => {
    const translations = isDocumine ? removeAnnotationTranslations : removeRedactionTranslations;
    const { permissions, redactions, isApprover, falsePositiveContext } = data;
    const isBulk = redactions.length > 1;
    const isImage = redactions.reduce((acc, next) => acc && next.isImage, true);

    const options: DetailsRadioOption<RemoveRedactionOption>[] = [];
    if (permissions.canRemoveOnlyHere && !isImage) {
        options.push({
            label: translations.ONLY_HERE.label,
            description: isBulk ? translations.ONLY_HERE.descriptionBulk : translations.ONLY_HERE.description,
            descriptionParams: {
                value: redactions[0].value,
                type: redactions[0].HINT ? 'hint' : redactions[0].typeLabel,
                isImage: redactions[0].isImage ? 'image' : redactions[0].typeLabel,
            },
            icon: PIN_ICON,
            value: RemoveRedactionOptions.ONLY_HERE,
        });

        const isHint = redactions.reduce((acc, next) => acc && next.isHint, true);
        if (!isHint) {
            options.push({
                label: removeRedactionTranslations.IN_DOCUMENT.label,
                description: removeRedactionTranslations.IN_DOCUMENT.description,
                icon: DOCUMENT_ICON,
                value: RemoveRedactionOptions.IN_DOCUMENT,
            });
        }
    }
    if (permissions.canRemoveFromDictionary) {
        options.push({
            label: isBulk ? translations.IN_DOSSIER.labelBulk : translations.IN_DOSSIER.label,
            description: isBulk ? translations.IN_DOSSIER.descriptionBulk : translations.IN_DOSSIER.description,
            descriptionParams: {
                value: redactions[0].value,
                type: redactions[0].HINT ? 'hint' : redactions[0].typeLabel,
                isImage: redactions[0].isImage ? 'image' : redactions[0].typeLabel,
            },
            icon: FOLDER_ICON,
            value: RemoveRedactionOptions.IN_DOSSIER,
            additionalCheck: !isDocumine
                ? {
                      label: translations.IN_DOSSIER.extraOptionLabel,
                      checked: applyToAllDossiers,
                      hidden: !isApprover,
                  }
                : null,
        });
    }
    if (permissions.canMarkAsFalsePositive) {
        if (redactions[0].isRecommendation) {
            options.push({
                label: translations.DO_NOT_RECOMMEND.label,
                description: isBulk ? translations.DO_NOT_RECOMMEND.descriptionBulk : translations.DO_NOT_RECOMMEND.description,
                descriptionParams: {
                    value: redactions[0].value,
                    type: redactions[0].typeLabel,
                    context: falsePositiveContext[0],
                    isImage: redactions[0].isImage ? 'image' : redactions[0].typeLabel,
                },
                icon: FOLDER_ICON,
                value: RemoveRedactionOptions.DO_NOT_RECOMMEND,
                additionalCheck: !isDocumine
                    ? {
                          label: translations.DO_NOT_RECOMMEND.extraOptionLabel,
                          checked: applyToAllDossiers,
                          hidden: !isApprover,
                      }
                    : null,
            });
        } else {
            options.push({
                label: translations.FALSE_POSITIVE.label,
                description: isBulk ? translations.FALSE_POSITIVE.descriptionBulk : translations.FALSE_POSITIVE.description,
                descriptionParams: {
                    value: redactions[0].value,
                    type: redactions[0].typeLabel,
                    context: falsePositiveContext[0],
                    isImage: redactions[0].isImage ? 'image' : redactions[0].typeLabel,
                },
                icon: REMOVE_FROM_DICT_ICON,
                value: RemoveRedactionOptions.FALSE_POSITIVE,
                additionalCheck: !isDocumine
                    ? {
                          label: translations.FALSE_POSITIVE.extraOptionLabel,
                          checked: applyToAllDossiers,
                          hidden: !isApprover,
                          description: translations.FALSE_POSITIVE.extraOptionDescription,
                      }
                    : null,
            });
        }
    }
    return options;
};

export const getForceAnnotationOptions = (
    isDocumine: boolean,
    isHint: boolean,
    isImage: boolean,
): DetailsRadioOption<ForceAnnotationOption>[] => {
    if (isDocumine || isHint || isImage) {
        return [];
    }

    return [
        {
            label: redactTextTranslations.onlyHere.label,
            description: redactTextTranslations.onlyHere.description,
            icon: PIN_ICON,
            value: ResizeOptions.ONLY_HERE,
        },
        {
            label: redactTextTranslations.inDocument.label,
            description: redactTextTranslations.inDocument.description,
            icon: DOCUMENT_ICON,
            value: ResizeOptions.IN_DOCUMENT,
        },
    ];
};
