package org.gcube.application.geoportal.service.profiledDocuments;

import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.gcube.application.cms.serialization.Serialization;
import org.gcube.application.geoportal.common.model.document.Project;
import org.gcube.application.geoportal.common.model.document.identification.SpatialReference;
import org.gcube.application.geoportal.common.model.document.lifecycle.LifecycleInformation;
import org.gcube.application.geoportal.common.model.rest.RegisterFileSetRequest;
import org.gcube.application.geoportal.common.model.rest.StepExecutionRequest;
import org.gcube.application.geoportal.common.model.useCaseDescriptor.Field;
import org.gcube.application.geoportal.common.utils.StorageUtils;
import org.gcube.application.geoportal.service.utils.UserUtils;
import org.junit.Test;

import javax.ws.rs.client.WebTarget;
import java.time.LocalDateTime;

import static org.junit.Assert.assertTrue;

@Slf4j
public class ProfiledConcessioniTests extends AbstractProfiledDocumentsTests{

    

    @Override
    protected WebTarget baseTarget() {
        String testProfileId="profiledConcessioni";
        return super.baseTarget().path(testProfileId);
    }

    @Test
    public void testSDI() throws Exception {
        UserUtils.DEFAULT_ROLES.add("Data-Manager");
        // Create new

        Document theDoc=Document.parse("{\n" +
                "\"posizionamentoScavo\" :{\n" +
                "\t\"titolo\" : \"mio titolo\"}}");

        theDoc.put("startTime", LocalDateTime.now());

        log.debug("Sending {}",theDoc);
        log.debug("AS JSON : {}",Serialization.write(theDoc));

        Project doc = createNew(theDoc);

        log.debug("Received : {}",doc);
        log.debug("AS JSON : {}",Serialization.write(doc));


        // register filesets
        doc = upload(
                new StorageUtils(),
                doc.getId(),
                "$.posizionamentoScavo",
                "fileset",
                "$.posizionamentoScavo."+ Field.CHILDREN+"[?(@.fileset)]",
                null,
                RegisterFileSetRequest.ClashOptions.MERGE_EXISTING,
                "pos.shp");


        System.out.println("Registered posizionamento, result is "+ Serialization.write(doc));
        // invoke step SUBMIT-FOR-REVIEW
        StepExecutionRequest submitReq=new StepExecutionRequest();
        submitReq.setStepID("SUBMIT-FOR-REVIEW");
//        doc=step(doc.getId(),submitReq);
        doc=step("630f912755e2947b0278c1be",submitReq);
        System.out.println(doc);
        assertTrue(doc.getLifecycleInformation().getLastOperationStatus().equals(LifecycleInformation.Status.OK));


        // invoke step Publish

        StepExecutionRequest approveDraftReq=new StepExecutionRequest();
        approveDraftReq.setStepID("APPROVE-SUBMITTED");
        doc=step(doc.getId(),approveDraftReq);
        System.out.println(doc);
        assertTrue(doc.getLifecycleInformation().getLastOperationStatus().equals(LifecycleInformation.Status.OK));
        assertTrue(doc.getIdentificationReferenceByType(SpatialReference.SPATIAL_REFERENCE_TYPE).size()==1);
        System.out.println("Project "+doc.getId()+" published with spatial reference "+doc.getIdentificationReferenceByType(SpatialReference.SPATIAL_REFERENCE_TYPE).get(0));
    }

    @Test
    public void delete() throws Exception {
        Document theDoc=Document.parse("{\n" +
                "\"posizionamentoScavo\" :{\n" +
                "\t\"titolo\" : \"mio titolo\"}}");

        theDoc.put("startTime", LocalDateTime.now());

        log.debug("sendi");

        Project doc = createNew(theDoc);



    }
}
