package eu.eudat.models.data.listingmodels;

import eu.eudat.data.entities.Dataset;
import eu.eudat.models.DataModel;
import eu.eudat.models.data.dataset.DataRepository;
import eu.eudat.models.data.dataset.Service;
import eu.eudat.logic.utilities.helpers.LabelBuilder;
import eu.eudat.models.data.datasetprofile.DatasetProfileOverviewModel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


public class DatasetListingModel implements DataModel<Dataset, DatasetListingModel> {
    private String id;
    private String label;
    private String grant;
    private String dmp;
    private String dmpId;
    private DatasetProfileOverviewModel profile;
    private String dataRepositories;
    private String registries;
    private String services;
    private int status;
    private Date created;
    private Date modified;
    private String description;
    private String grantAbbreviation;
    private String grantId;
    private Date finalizedAt;
    private Date dmpPublishedAt;
    private int version;
    private List<UserInfoListingModel> users;
    private Boolean isPublic;
    private Boolean isProfileLatestVersion;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    public String getLabel() {
        return label;
    }
    public void setLabel(String label) {
        this.label = label;
    }

    public String getGrant() {
        return grant;
    }
    public void setGrant(String grant) {
        this.grant = grant;
    }

    public String getDmp() {
        return dmp;
    }
    public void setDmp(String dmp) {
        this.dmp = dmp;
    }

    public String getDmpId() {
        return dmpId;
    }
    public void setDmpId(String dmpId) {
        this.dmpId = dmpId;
    }

    public DatasetProfileOverviewModel getProfile() {
        return profile;
    }

    public void setProfile(DatasetProfileOverviewModel profile) {
        this.profile = profile;
    }

    public String getDataRepositories() {
        return dataRepositories;
    }
    public void setDataRepositories(String dataRepositories) {
        this.dataRepositories = dataRepositories;
    }

    public String getRegistries() {
        return registries;
    }
    public void setRegistries(String registries) {
        this.registries = registries;
    }

    public String getServices() {
        return services;
    }
    public void setServices(String services) {
        this.services = services;
    }

    public int getStatus() {
        return status;
    }
    public void setStatus(int status) {
        this.status = status;
    }

    public Date getCreated() {
        return created;
    }
    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getModified() {
        return modified;
    }
    public void setModified(Date modified) {
        this.modified = modified;
    }

    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }

    public String getGrantAbbreviation() {
        return grantAbbreviation;
    }
    public void setGrantAbbreviation(String grantAbbreviation) {
        this.grantAbbreviation = grantAbbreviation;
    }

    public String getGrantId() {
        return grantId;
    }
    public void setGrantId(String grantId) {
        this.grantId = grantId;
    }

    public Date getFinalizedAt() {
        return finalizedAt;
    }
    public void setFinalizedAt(Date finalizedAt) {
        this.finalizedAt = finalizedAt;
    }

    public Date getDmpPublishedAt() {
        return dmpPublishedAt;
    }
    public void setDmpPublishedAt(Date dmpPublishedAt) {
        this.dmpPublishedAt = dmpPublishedAt;
    }

    public int getVersion() {
        return version;
    }
    public void setVersion(int version) {
        this.version = version;
    }

    public List<UserInfoListingModel> getUsers() {
        return users;
    }

    public void setUsers(List<UserInfoListingModel> users) {
        this.users = users;
    }

    public Boolean getPublic() {
        return isPublic;
    }

    public void setPublic(Boolean aPublic) {
        isPublic = aPublic;
    }

    public Boolean getProfileLatestVersion() {
        return isProfileLatestVersion;
    }

    public void setProfileLatestVersion(Boolean profileLatestVersion) {
        isProfileLatestVersion = profileLatestVersion;
    }

    @Override
    public DatasetListingModel fromDataModel(Dataset entity) {
        this.id = entity.getId() != null ? entity.getId().toString() : "";
        this.label = entity.getLabel();
        this.created = entity.getCreated();
        this.modified = entity.getModified();
        this.grant = entity.getDmp() != null ? entity.getDmp().getGrant().getLabel() : "";
        this.dmp = entity.getDmp() != null ? entity.getDmp().getLabel() : "";
        this.dmpId = entity.getDmp() != null ? entity.getDmp().getId().toString() : "";
        this.profile = entity.getProfile() != null ? new DatasetProfileOverviewModel().fromDataModel(entity.getProfile()) : null;
        this.description = entity.getDescription();
        this.status = entity.getStatus();
        this.grantAbbreviation = entity.getDmp() != null ? entity.getDmp().getGrant().getAbbreviation() : "";
        this.grantId = entity.getDmp() != null ? entity.getDmp().getGrant().getId().toString() : "";
        this.registries = LabelBuilder.getLabel(entity.getRegistries().stream().map(item -> new eu.eudat.models.data.dataset.Registry().fromDataModel(item)).collect(Collectors.toList()));
        this.dataRepositories = LabelBuilder.getLabel(entity.getDatasetDataRepositories().stream().map(item -> new DataRepository().fromDataModel(item.getDataRepository())).collect(Collectors.toList()));
        this.services = LabelBuilder.getLabel(entity.getServices().stream().map(item -> new Service().fromDataModel(item.getService())).collect(Collectors.toList()));
        if (entity.getFinalizedAt() == null && entity.getStatus() == Dataset.Status.FINALISED.getValue()) {
            this.finalizedAt = entity.getDmp().getFinalizedAt();
        } else {
            this.finalizedAt = entity.getFinalizedAt();
        }
        this.dmpPublishedAt = entity.getDmp().getPublishedAt();
        this.version = entity.getDmp().getVersion();
        this.users = entity.getDmp() != null ? entity.getDmp().getUsers().stream().map(x -> new UserInfoListingModel().fromDataModel(x)).collect(Collectors.toList()) : new ArrayList<>();
        this.isPublic = entity.getDmp() != null ? entity.getDmp().isPublic() : false;
        return this;
    }

    @Override
    public Dataset toDataModel() {
        return null;
    }

    @Override
    public String getHint() {
        return "datasetListingModel";
    }
}
