package com.iqser.red.service.persistence.service.v1.api.shared.model.redactionlog;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import com.iqser.red.service.persistence.service.v1.api.shared.model.annotations.AnnotationStatus;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@Deprecated(forRemoval = true)
@AllArgsConstructor
@EqualsAndHashCode
public class RedactionLogEntry {

    private String id;
    private String type;
    private String value;
    private String reason;
    private String matchedRule;
    private boolean rectangle;
    private String legalBasis;

    private boolean imported;

    private boolean redacted;
    private boolean isHint;
    private boolean isRecommendation;
    private boolean isFalsePositive;

    private String section;
    private float[] color;

    @Builder.Default
    private List<Rectangle> positions = new ArrayList<>();
    private int sectionNumber;

    private String textBefore;
    private String textAfter;

    @Builder.Default
    private List<RedactionLogComment> comments = new ArrayList<>();

    private int startOffset;
    private int endOffset;

    private boolean isImage;
    private boolean imageHasTransparency;

    private boolean isDictionaryEntry;
    private boolean isDossierDictionaryEntry;

    private boolean excluded;

    private String sourceId;

    @EqualsAndHashCode.Exclude
    @Builder.Default
    private List<Change> changes = new ArrayList<>();

    @EqualsAndHashCode.Exclude
    @Builder.Default
    private List<ManualChange> manualChanges = new ArrayList<>();

    @Builder.Default
    private Set<Engine> engines = new HashSet<>();

    @Builder.Default
    private Set<String> reference = new HashSet<>();

    @Builder.Default
    private Set<String> importedRedactionIntersections = new HashSet<>();


    public boolean lastChangeIsRemoved() {

        return last(changes).map(c -> c.getType() == ChangeType.REMOVED).orElse(false);
    }


    public boolean isManuallyRemoved() {

        return manualChanges != null && manualChanges.stream()
                .anyMatch(mc -> mc.getManualRedactionType() == ManualRedactionType.REMOVE_LOCALLY && mc.getAnnotationStatus() == AnnotationStatus.APPROVED);
    }


    private <T> Optional<T> last(List<T> list) {

        return list == null || list.isEmpty() ? Optional.empty() : Optional.of(list.get(list.size() - 1));
    }

}
