package eu.eudat.logic.managers;

import eu.eudat.data.dao.entities.DatasetDao;
import eu.eudat.data.dao.entities.DatasetProfileDao;
import eu.eudat.exceptions.datasetprofile.DatasetProfileWithDatasetsExeption;
import eu.eudat.logic.builders.entity.DatasetProfileBuilder;
import eu.eudat.models.data.entities.xmlmodels.datasetprofiledefinition.ViewStyleModel;
import eu.eudat.models.data.admin.composite.DatasetProfile;
import eu.eudat.logic.services.ApiContext;
import eu.eudat.logic.utilities.builders.ModelBuilder;
import eu.eudat.logic.utilities.builders.XmlBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

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

public class AdminManager {

    public static eu.eudat.data.entities.DatasetProfile generateViewStyleDefinition(DatasetProfile profile, ApiContext apiContext) {
        ViewStyleModel viewStyleModel = new ViewStyleModel();
        viewStyleModel.setSections(new ModelBuilder().toViewStyleDefinition(profile.getSections(), eu.eudat.models.data.entities.xmlmodels.datasetprofiledefinition.Section.class));
        viewStyleModel.setPages(new ModelBuilder().toViewStyleDefinition(profile.getPages(), eu.eudat.models.data.entities.xmlmodels.datasetprofiledefinition.Page.class));
        Document viewStyleDoc = XmlBuilder.getDocument();
        Element elementViewStyle = viewStyleModel.toXml(viewStyleDoc);
        viewStyleDoc.appendChild(elementViewStyle);
        String xml = XmlBuilder.generateXml(viewStyleDoc);

        if (profile.getDescription() == null) {
            profile.setDescription("");
        }

        if (profile.getLanguage() == null) {
            profile.setLanguage("en");
        }

        eu.eudat.data.entities.DatasetProfile datasetProfile = apiContext.getOperationsContext().getBuilderFactory().getBuilder(DatasetProfileBuilder.class).definition(xml).label(profile.getLabel())
                .status(profile.getStatus()).created(new Date()).description(profile.getDescription()).language(profile.getLanguage())
                .build();

        if (datasetProfile.getGroupId() == null) {
            datasetProfile.setGroupId(UUID.randomUUID());
        }

        if (datasetProfile.getVersion() == null) {
            datasetProfile.setVersion((short)1);
        }

        return datasetProfile;
    }

    public static eu.eudat.models.data.admin.composite.DatasetProfile generateDatasetProfileModel(eu.eudat.data.entities.DatasetProfile profile) {
        Document viewStyleDoc = XmlBuilder.fromXml(profile.getDefinition());
        Element root = viewStyleDoc.getDocumentElement();
        eu.eudat.models.data.entities.xmlmodels.datasetprofiledefinition.ViewStyleModel viewstyle = new eu.eudat.models.data.entities.xmlmodels.datasetprofiledefinition.ViewStyleModel().fromXml(root);

        eu.eudat.models.data.admin.composite.DatasetProfile datasetprofile = new eu.eudat.models.data.admin.composite.DatasetProfile();
        datasetprofile.buildProfile(viewstyle);
        return datasetprofile;
    }


    public static eu.eudat.data.entities.DatasetProfile inactivate(DatasetProfileDao datasetProfileRepository, DatasetDao datasetDao, String id) {
        eu.eudat.data.dao.criteria.DatasetCriteria datasetsForThatDatasetProfile = new eu.eudat.data.dao.criteria.DatasetCriteria();
        datasetsForThatDatasetProfile.setProfileDatasetId(UUID.fromString(id));
        if (datasetDao.getWithCriteria(datasetsForThatDatasetProfile).count() == 0) {
            eu.eudat.data.entities.DatasetProfile detasetProfile = datasetProfileRepository.find(UUID.fromString(id));
            detasetProfile.setStatus(eu.eudat.data.entities.DatasetProfile.Status.DELETED.getValue());
            detasetProfile = datasetProfileRepository.createOrUpdate(detasetProfile);
            return detasetProfile;
        } else {
            throw new DatasetProfileWithDatasetsExeption("This profile can not deleted, because Datasets are associated with it");
        }
    }
}