package eu.eudat.logic.security.repositorydeposit.mapper;

import eu.eudat.data.entities.*;
import eu.eudat.depositinterface.models.*;
import org.springframework.http.ResponseEntity;

import java.io.File;
import java.util.stream.Collectors;

public class DMPToDepositMapper {

    public static DMPDepositModel fromDMP(DMP entity, File pdfFile, String fileName, ResponseEntity<byte[]> jsonFile, String previousDOI) {
        DMPDepositModel deposit = new DMPDepositModel();
        deposit.setId(entity.getId());
        deposit.setVersion(entity.getVersion());
        deposit.setLabel(entity.getLabel());
        deposit.setDescription(entity.getDescription());
        deposit.setPublic(entity.isPublic());
        deposit.setUsers(entity.getUsers().stream().map(DMPToDepositMapper::fromUserDMP).collect(Collectors.toSet()));
        deposit.setOrganisations(entity.getOrganisations().stream().map(DMPToDepositMapper::fromOrganisation).collect(Collectors.toSet()));
        deposit.setResearchers(entity.getResearchers().stream().map(DMPToDepositMapper::fromResearcher).collect(Collectors.toSet()));
        deposit.setGrant(fromGrant(entity.getGrant()));

        deposit.setPdfFile(pdfFile);
        deposit.setPdfFileName(fileName);
        deposit.setRdaJson(jsonFile);
        deposit.setPreviousDOI(previousDOI);

        deposit.setExtraProperties(entity.getExtraProperties());
        return deposit;
    }

    private static UserDMPDepositModel fromUserDMP(UserDMP entity){
        UserDMPDepositModel deposit = new UserDMPDepositModel();
        deposit.setUser(fromUserInfo(entity.getUser()));
        deposit.setRole(entity.getRole());
        return deposit;
    }

    private static UserInfoDepositModel fromUserInfo(UserInfo entity){
        UserInfoDepositModel deposit = new UserInfoDepositModel();
        deposit.setName(entity.getName());
        deposit.setEmail(entity.getEmail());
        return deposit;
    }

    private static OrganisationDepositModel fromOrganisation(Organisation entity){
        OrganisationDepositModel deposit = new OrganisationDepositModel();
        deposit.setLabel(entity.getLabel());
        return deposit;
    }

    private static ResearcherDepositModel fromResearcher(Researcher entity){
        ResearcherDepositModel deposit = new ResearcherDepositModel();
        deposit.setLabel(entity.getLabel());
        deposit.setReference(entity.getReference());
        return deposit;
    }

    private static GrantDepositModel fromGrant(Grant entity){
        GrantDepositModel deposit = new GrantDepositModel();
        deposit.setId(entity.getId());
        deposit.setReference(entity.getReference());
        deposit.setFunder(fromFunder(entity.getFunder()));
        return deposit;
    }

    private static FunderDepositModel fromFunder(Funder entity){
        FunderDepositModel deposit = new FunderDepositModel();
        deposit.setLabel(entity.getLabel());
        return deposit;
    }
}
