package eu.eudat.publicapi.request.dmp;

import eu.eudat.data.entities.DMP;
import eu.eudat.data.query.PaginationService;
import eu.eudat.data.query.definition.TableQuery;
import eu.eudat.publicapi.criteria.dmp.DataManagementPlanPublicCriteria;
import eu.eudat.queryable.QueryableList;
import eu.eudat.queryable.types.FieldSelectionType;
import eu.eudat.queryable.types.SelectionField;

import java.util.Arrays;
import java.util.Date;
import java.util.UUID;

public class DataManagmentPlanPublicTableRequest extends TableQuery<DataManagementPlanPublicCriteria, DMP, UUID> {

    public QueryableList<DMP> applyCriteria() {
        QueryableList<DMP> query = this.getQuery();
        query.where((builder, root) -> builder.equal(root.get("isPublic"), true));
        if (this.getCriteria().getLike() != null && !this.getCriteria().getLike().isEmpty())
            query.where((builder, root) -> builder.or(
                    builder.like(builder.upper(root.get("label")), "%" + this.getCriteria().getLike().toUpperCase() + "%"),
                    builder.like(builder.upper(root.get("description")), "%" + this.getCriteria().getLike().toUpperCase() + "%")));
        if (this.getCriteria().getPeriodStart() != null)
            query.where((builder, root) -> builder.greaterThan(root.get("created"), this.getCriteria().getPeriodStart()));
        if (this.getCriteria().getPeriodEnd() != null)
            query.where((builder, root) -> builder.lessThan(root.get("created"), this.getCriteria().getPeriodEnd()));
        if (this.getCriteria().getGrants() != null && !this.getCriteria().getGrants().isEmpty())
            query.where(((builder, root) -> root.get("grant").get("id").in(this.getCriteria().getGrants())));
        if (this.getCriteria().getFunders() != null && !this.getCriteria().getFunders().isEmpty())
            query.where(((builder, root) -> root.get("grant").get("funder").get("id").in(this.getCriteria().getFunders())));

        //query.where((builder, root) -> builder.lessThan(root.get("grant").get("enddate"), new Date())); // GrantStateType.FINISHED
        query.where((builder, root) ->
                builder.or(builder.greaterThan(root.get("grant").get("enddate"), new Date())
                        , builder.isNull(root.get("grant").get("enddate")))); // GrantStateType.ONGOING

        if (this.getCriteria().getDatasetTemplates() != null && !this.getCriteria().getDatasetTemplates().isEmpty())
            query.where((builder, root) -> root.join("dataset").get("id").in(this.getCriteria().getDatasetTemplates()));
        if (this.getCriteria().getDmpOrganisations() != null && !this.getCriteria().getDmpOrganisations().isEmpty())
            query.where(((builder, root) -> root.join("organisations").get("reference").in(this.getCriteria().getDmpOrganisations())));
        if (this.getCriteria().getCollaborators() != null && !this.getCriteria().getCollaborators().isEmpty())
            query.where(((builder, root) -> root.join("researchers").get("id").in(this.getCriteria().getCollaborators())));
        if (!this.getCriteria().getAllVersions()) {
            query.initSubQuery(String.class).where((builder, root) -> builder.equal(root.get("version"),
                    query.<String>subQueryMax((builder1, externalRoot, nestedRoot) -> builder1.and(builder1.equal(externalRoot.get("groupId"),
                            nestedRoot.get("groupId")), builder1.equal(nestedRoot.get("isPublic"), true)), Arrays.asList(new SelectionField(FieldSelectionType.FIELD, "version")), String.class)));
        }
        if (this.getCriteria().getGroupIds() != null && !this.getCriteria().getGroupIds().isEmpty()) {
            query.where((builder, root) -> root.get("groupId").in(this.getCriteria().getGroupIds()));
        }
        return query;
    }

    @Override
    public QueryableList<DMP> applyPaging(QueryableList<DMP> items) {
        return PaginationService.applyPaging(items, this);
    }
}
