package com.mparticle.sdk.samples;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mparticle.sdk.MessageProcessor;
import com.mparticle.sdk.model.MessageSerializer;
import com.mparticle.sdk.model.eventprocessing.*;
import com.mparticle.sdk.model.registration.*;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public final class SlackMessageForwarder extends MessageProcessor {

    @Override
    public ModuleRegistrationResponse processRegistrationRequest(ModuleRegistrationRequest request) {

        ModuleRegistrationResponse response = new ModuleRegistrationResponse("SlackMessageForwarder", "1.0");

        Permissions permissions = new Permissions();

        List<DeviceIdentityPermission> deviceIds = Arrays.asList(
                new DeviceIdentityPermission(DeviceIdentity.Type.ANDROID_ID, Identity.Encoding.RAW),
                new DeviceIdentityPermission(DeviceIdentity.Type.IOS_VENDOR_ID, Identity.Encoding.RAW)
        );

        permissions.setDeviceIdentities(deviceIds);

        List<UserIdentityPermission> userIds = Arrays.asList(
                new UserIdentityPermission(UserIdentity.Type.FACEBOOK, Identity.Encoding.RAW),
                new UserIdentityPermission(UserIdentity.Type.TWITTER, Identity.Encoding.RAW),
                new UserIdentityPermission(UserIdentity.Type.EMAIL, Identity.Encoding.RAW)
        );

        permissions.setUserIdentities(userIds);

        permissions.setAllowAccessLocation(true);

        response.setPermissions(permissions);

        EventProcessingRegistration eventProcessingRegistration = new EventProcessingRegistration();

        eventProcessingRegistration.setDescription("Slack Event Forwarder");

        List<RuntimeEnvironment.Type> runtimeEnvironments = Arrays.asList(RuntimeEnvironment.Type.values());
        eventProcessingRegistration.setSupportedRuntimeEnvironments(runtimeEnvironments);

        List<Setting> accountSettings = new ArrayList<>();

        TextSetting endpointUrl = new TextSetting("endpointUrl", "Endpoint Url");
        endpointUrl.setIsRequired(true);
        accountSettings.add(endpointUrl);

        TextSetting channelName = new TextSetting("channelName", "Channel Name");
        channelName.setIsRequired(true);
        accountSettings.add(channelName);

        eventProcessingRegistration.setAccountSettings(accountSettings);

        List<Event.Type> supportedEventTypes = Arrays.asList(Event.Type.values());

        eventProcessingRegistration.setSupportedEventTypes(supportedEventTypes);

        eventProcessingRegistration.setMaxDataAgeHours(24);

        response.setEventProcessingRegistration(eventProcessingRegistration);

        return response;
    }

    @Override
    public EventProcessingResponse processEventProcessingRequest(EventProcessingRequest request) throws IOException {
        EventProcessingResponse response = new EventProcessingResponse();
        postMessage(request.getAccount(), serializer.serialize(request));
        return response;
    }

    private MessageSerializer serializer = new MessageSerializer();

    private static class SlackPayload
    {
        public String channel;
        public String username;
        public String text;
        public String icon_url;
    }

    private void postMessage(Account account, String messageText) throws IOException {

        String endpointUrl = account.getStringSetting("endpointUrl", true, null);
        String channelName = account.getStringSetting("channelName", true, null);

        SlackPayload payload = new SlackPayload();
        payload.channel = channelName;
        payload.username = "LambdaForwarder";
        payload.icon_url = "http://static.mparticle.com/public/mp-icon.png";
        payload.text = messageText;

        try(CloseableHttpClient httpClient = HttpClients.createDefault()){

            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(payload);

            HttpPost request = new HttpPost(endpointUrl);
            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
            request.setEntity(entity);

            try(CloseableHttpResponse response = httpClient.execute(request)) {
                if (response.getStatusLine().getStatusCode() != 200) {
                    throw new RuntimeException("Failed: HTTP error code: "
                            + response.getStatusLine().getStatusCode());
                }
            }
        }
    }
}

@@ -170,24 +170,350 @@
 Serializer;%0A
+import com.mparticle.sdk.model.audienceprocessing.AudienceMembershipChangeRequest;%0Aimport com.mparticle.sdk.model.audienceprocessing.AudienceMembershipChangeResponse;%0Aimport com.mparticle.sdk.model.audienceprocessing.AudienceSubscriptionRequest;%0Aimport com.mparticle.sdk.model.audienceprocessing.AudienceSubscriptionResponse;%0A
 import com.m
@@ -3354,335 +3354,1294 @@
     
-return response;%0A    %7D%0A%0A    @Override%0A    public EventProcessingResponse processEventProcessingRequest(EventProcessingRequest request) throws IOException %7B%0A        EventProcessingResponse response = new EventProcessingResponse();%0A        postMessage(request.getAccount(), serializer.serialize(request));%0A        return response
+AudienceProcessingRegistration audienceProcessingRegistration = new AudienceProcessingRegistration();%0A        audienceProcessingRegistration.setDescription(%22Slack Audience Forwarder%22);%0A        audienceProcessingRegistration.setAccountSettings(accountSettings);%0A%0A        response.setAudienceProcessingRegistration(audienceProcessingRegistration);%0A%0A        return response;%0A    %7D%0A%0A    @Override%0A    public EventProcessingResponse processEventProcessingRequest(EventProcessingRequest request) throws IOException %7B%0A        EventProcessingResponse response = new EventProcessingResponse();%0A        postMessage(request.getAccount(), serializer.serialize(request));%0A        return response;%0A    %7D%0A%0A    @Override%0A    public AudienceMembershipChangeResponse processAudienceMembershipChangeRequest(AudienceMembershipChangeRequest request) throws IOException %7B%0A        postMessage(request.getAccount(), serializer.serialize(request));%0A        return super.processAudienceMembershipChangeRequest(request);%0A    %7D%0A%0A    @Override%0A    public AudienceSubscriptionResponse processAudienceSubscriptionRequest(AudienceSubscriptionRequest request) throws IOException %7B%0A        postMessage(request.getAccount(), serializer.serialize(request));%0A        return super.processAudienceSubscriptionRequest(request)
 ;%0A  

add audience handlers to slack forwarder