import { DetailsRadioOption } from '@iqser/common-ui';
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 { RemoveRedactionData } from './dialog-types';

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

export const RedactOrHintOptions = {
    ONLY_HERE: 'ONLY_HERE',
    IN_DOSSIER: 'IN_DOSSIER',
} as const;

export type RedactOrHintOption = keyof typeof RedactOrHintOptions;

export const ResizeOptions = RedactOrHintOptions;
export type ResizeRedactionOption = RedactOrHintOption;

export const RemoveRedactionOptions = {
    ONLY_HERE: 'ONLY_HERE',
    IN_DOSSIER: 'IN_DOSSIER',
    FALSE_POSITIVE: 'FALSE_POSITIVE',
    DO_NOT_RECOMMEND: 'DO_NOT_RECOMMEND',
} as const;
export const RemoveAnnotationOptions = RemoveRedactionOptions;

export type RemoveRedactionOption = keyof typeof RemoveRedactionOptions;
export type RemoveAnnotationOption = RemoveRedactionOption;

export const getEditRedactionOptions = (
    dossierName: string,
    applyToAllDossiers: boolean,
    dossierDictionaryOnly: boolean,
    isModifyDictionary: boolean,
): DetailsRadioOption<RedactOrHintOption>[] => {
    const options: DetailsRadioOption<RedactOrHintOption>[] = [
        {
            label: editRedactionTranslations.onlyHere.label,
            description: editRedactionTranslations.onlyHere.description,
            icon: PIN_ICON,
            value: ResizeOptions.ONLY_HERE,
        },
    ];
    if (isModifyDictionary) {
        options.push({
            label: editRedactionTranslations.inDossier.label,
            description: editRedactionTranslations.inDossier.description,
            descriptionParams: { dossierName: dossierName },
            icon: FOLDER_ICON,
            value: ResizeOptions.IN_DOSSIER,
            extraOption: {
                label: editRedactionTranslations.inDossier.extraOptionLabel,
                checked: applyToAllDossiers,
                hidden: dossierDictionaryOnly,
                disabled: true,
            },
        });
    }
    return options;
};

export const getRedactOrHintOptions = (
    dossier: Dossier,
    applyToAllDossiers: boolean,
    isApprover: boolean,
    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: ResizeOptions.ONLY_HERE,
        });
    }

    if (isRss) {
        return options;
    }

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

    return options;
};

export const getResizeRedactionOptions = (
    redaction: AnnotationWrapper,
    dossier: Dossier,
    isRss: boolean,
    applyToAllDossiers: boolean,
    isApprover: boolean,
): DetailsRadioOption<ResizeRedactionOption>[] => {
    const translations = resizeRedactionTranslations;
    const options: DetailsRadioOption<ResizeRedactionOption>[] = [
        {
            label: translations.onlyHere.label,
            description: translations.onlyHere.description,
            icon: PIN_ICON,
            value: RedactOrHintOptions.ONLY_HERE,
        },
    ];

    if (isRss) {
        return options;
    }

    const dictBasedType = redaction.isModifyDictionary;
    options.push({
        label: translations.inDossier.label,
        description: translations.inDossier.description,
        descriptionParams: { dossierName: dossier.dossierName },
        disabled: !dictBasedType,
        tooltip: !dictBasedType ? translations.inDossier.tooltip : null,
        icon: FOLDER_ICON,
        value: RedactOrHintOptions.IN_DOSSIER,
        extraOption: {
            label: translations.inDossier.extraOptionLabel,
            checked: applyToAllDossiers,
            hidden: !isApprover,
        },
    });
    return options;
};

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

    const options: DetailsRadioOption<RemoveRedactionOption>[] = [];
    if (permissions.canRemoveOnlyHere) {
        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].type,
            },
            icon: PIN_ICON,
            value: RemoveRedactionOptions.ONLY_HERE,
        });
    }
    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].type },
            icon: FOLDER_ICON,
            value: RemoveRedactionOptions.IN_DOSSIER,
            extraOption: !isDocumine
                ? {
                      label: translations.IN_DOSSIER.extraOptionLabel,
                      checked: applyToAllDossiers ?? true,
                      hidden: !isApprover,
                  }
                : null,
        });
    }
    if (permissions.canMarkAsFalsePositive) {
        if (data.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].type,
                    context: falsePositiveContext[0],
                },
                icon: FOLDER_ICON,
                value: RemoveRedactionOptions.DO_NOT_RECOMMEND,
                extraOption: !isDocumine
                    ? {
                          label: translations.DO_NOT_RECOMMEND.extraOptionLabel,
                          checked: applyToAllDossiers ?? true,
                          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].type,
                    context: falsePositiveContext[0],
                },
                icon: REMOVE_FROM_DICT_ICON,
                value: RemoveRedactionOptions.FALSE_POSITIVE,
                extraOption: !isDocumine
                    ? {
                          label: translations.FALSE_POSITIVE.extraOptionLabel,
                          checked: applyToAllDossiers ?? true,
                          hidden: !isApprover,
                      }
                    : null,
            });
        }
    }
    return options;
};
