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

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.file.FileModel;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.file.ProcessingStatus;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.file.WorkflowStatus;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class FileStatusFilter {

    private List<ProcessingStatus> processingStatusList = new ArrayList<>();
    private List<WorkflowStatus> workflowStatusList = new ArrayList<>();
    private boolean includeSoftDeletedFiles;
    private boolean includeHardDeletedFiles;


    public FileStatusFilter(List<ProcessingStatus> processingStatusList, List<WorkflowStatus> workflowStatusList) {

        this.processingStatusList = processingStatusList;
        this.workflowStatusList = workflowStatusList;
        this.includeSoftDeletedFiles = false;
        this.includeHardDeletedFiles = false;
    }


    public Predicate<FileModel> asPredicate() {

        if (this.getProcessingStatusList() == null) {
            this.setProcessingStatusList(new ArrayList<>());
        }

        if (this.getWorkflowStatusList() == null) {
            this.setWorkflowStatusList(new ArrayList<>());
        }

        return fileStatus -> (this.getProcessingStatusList().isEmpty() || this.getProcessingStatusList().contains(fileStatus.getProcessingStatus()))
                             && (this.getWorkflowStatusList().isEmpty() || this.getWorkflowStatusList().contains(fileStatus.getWorkflowStatus()))
                             && (this.isIncludeSoftDeletedFiles() || fileStatus.getDeleted() == null)
                             && (this.isIncludeHardDeletedFiles() || fileStatus.getHardDeletedTime() == null);
    }

}
