/*
 * Copyright Amherst College
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.amherst.acdc.trellis.spi;

import static java.util.stream.Collectors.toList;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.rdf.api.IRI;

/**
 * A structure used for serializing an Event into an ActivityStream 2.0 JSON object
 *
 * @see <a href="https://www.w3.org/TR/activitystreams-core/">Activity Streams 2.0</a>
 *
 * @author acoburn
 */
class ActivityStreamMessage {

    static class EventResource {
        @JsonProperty("id")
        public String id;

        @JsonProperty("type")
        public List<String> type;

        public EventResource(final String id, final List<String> type) {
            this.id = id;
            this.type = type;
        }
    }

    @JsonProperty("id")
    public String id;

    @JsonProperty("type")
    public List<String> type;

    @JsonProperty("inbox")
    public String inbox;

    @JsonProperty("actor")
    public List<String> actor;

    @JsonProperty("object")
    public EventResource object;

    @JsonProperty("@context")
    public String context = "https://www.w3.org/ns/activitystreams";

    /**
     * Populate a ActivityStreamMessage from an Event
     * @param event The event
     * @return an ActivityStreamMessage
     */
    public static ActivityStreamMessage from(final Event event) {

        final ActivityStreamMessage msg = new ActivityStreamMessage();

        msg.id = event.getIdentifier().getIRIString();
        msg.type = event.getTypes().stream().map(IRI::getIRIString).collect(toList());
        msg.actor = event.getAgents().stream().map(IRI::getIRIString).collect(toList());

        event.getInbox().map(IRI::getIRIString).ifPresent(inbox -> msg.inbox = inbox);

        event.getTarget().map(IRI::getIRIString).ifPresent(target -> {
            msg.object = new EventResource(target,
                    event.getTargetTypes().stream().map(IRI::getIRIString).collect(toList()));
        });

        return msg;
    }
}

@@ -1075,84 +1075,26 @@
     
-@JsonProperty(%22id%22)%0A        public String id;%0A%0A        @JsonProperty(%22type%22)
+public String id;%0A
 %0A   
@@ -1279,187 +1279,83 @@
     
-@JsonProperty(%22id%22)%0A    public String id;%0A%0A    @JsonProperty(%22type%22)%0A    public List%3CString%3E type;%0A%0A    @JsonProperty(%22inbox%22)%0A    public String inbox;%0A%0A    @JsonProperty(%22actor%22)
+public String id;%0A%0A    public List%3CString%3E type;%0A%0A    public String inbox;%0A
 %0A   
@@ -1387,36 +1387,8 @@
 r;%0A%0A
-    @JsonProperty(%22object%22)%0A
     
@@ -2115,17 +2115,16 @@
 inbox);%0A
-%0A
         
@@ -2183,18 +2183,16 @@
 arget -%3E
- %7B
 %0A       
@@ -2328,27 +2328,16 @@
 List()))
-;%0A        %7D
 );%0A%0A    

simplify AS Message