/*
 * Copyright 2015 OpenMarket Ltd
 *
 * 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 org.matrix.androidsdk.rest.client;

import org.matrix.androidsdk.HomeServerConnectionConfig;
import org.matrix.androidsdk.RestClient;
import org.matrix.androidsdk.data.Pusher;
import org.matrix.androidsdk.rest.api.PushersApi;
import org.matrix.androidsdk.rest.callback.ApiCallback;
import org.matrix.androidsdk.rest.callback.RestAdapterCallback;
import org.matrix.androidsdk.rest.model.PushersResponse;

import java.util.HashMap;

/**
 * REST client for the Pushers API.
 */
public class PushersRestClient extends RestClient<PushersApi> {
    private static final String LOG_TAG = "PushersRestClient";

    private static final String PUSHER_KIND_HTTP = "http";
    private static final String DATA_KEY_HTTP_URL = "url";

    public PushersRestClient(HomeServerConnectionConfig hsConfig) {
        super(hsConfig, PushersApi.class, RestClient.URI_API_PREFIX_PATH_R0, true);
    }

    /** Add a new HTTP pusher.
     * @param pushkey the pushkey
     * @param appId the appplication id
     * @param profileTag the profile tag
     * @param lang the language
     * @param appDisplayName a human-readable application name
     * @param deviceDisplayName a human-readable device name
     * @param url the URL that should be used to send notifications
     * @param append append the pusher
     */
    public void addHttpPusher(
            final String pushkey, final String appId,
            final String profileTag, final String lang,
            final String appDisplayName, final String deviceDisplayName,
            final String url, boolean append, final ApiCallback<Void> callback) {
        manageHttpPusher(pushkey, appId, profileTag, lang, appDisplayName, deviceDisplayName, url, append, callback, true);
    }

    /** remove a new HTTP pusher.
     * @param pushkey the pushkey
     * @param appId the appplication id
     * @param profileTag the profile tag
     * @param lang the language
     * @param appDisplayName a human-readable application name
     * @param deviceDisplayName a human-readable device name
     * @param url the URL that should be used to send notifications
     */
    public void removeHttpPusher(
            final String pushkey, final String appId,
            final String profileTag, final String lang,
            final String appDisplayName, final String deviceDisplayName,
            final String url, final ApiCallback<Void> callback) {
        manageHttpPusher(pushkey, appId, profileTag, lang, appDisplayName, deviceDisplayName, url, false, callback, false);
    }


    /** add/remove a new HTTP pusher.
     * @param pushkey the pushkey
     * @param appId the appplication id
     * @param profileTag the profile tag
     * @param lang the language
     * @param appDisplayName a human-readable application name
     * @param deviceDisplayName a human-readable device name
     * @param url the URL that should be used to send notifications
     * @param addPusher true to add the pusher / false to remove it
     */
    private void manageHttpPusher(
            final String pushkey, final String appId,
            final String profileTag, final String lang,
            final String appDisplayName, final String deviceDisplayName,
            final String url, final Boolean append, final ApiCallback<Void> callback, final boolean addPusher) {
        Pusher pusher = new Pusher();
        pusher.pushkey = pushkey;
        pusher.appId = appId;
        pusher.profileTag = profileTag;
        pusher.lang = lang;
        pusher.kind = addPusher ? PUSHER_KIND_HTTP : null;
        pusher.appDisplayName= appDisplayName;
        pusher.deviceDisplayName = deviceDisplayName;
        pusher.data = new HashMap<>();
        pusher.data.put(DATA_KEY_HTTP_URL, url);
        if (addPusher) {
            pusher.append = append;
        }

        final String description = "manageHttpPusher";

        mApi.set(pusher, new RestAdapterCallback<Void>(description, mUnsentEventsManager, callback, new RestAdapterCallback.RequestRetryCallBack() {
            @Override
            public void onRetry() {
                manageHttpPusher(pushkey, appId, profileTag, lang, appDisplayName, deviceDisplayName, url, append, callback, addPusher);
            }
        }));
    }

    /**
     * Retrieve the pushers list
     * @param callback the callback
     */
    public void getPushers(final ApiCallback<PushersResponse> callback) {
        final String description = "getPushers";

        mApi.get(new RestAdapterCallback<PushersResponse>(description, mUnsentEventsManager, callback, new RestAdapterCallback.RequestRetryCallBack() {
            @Override
            public void onRetry() {
                getPushers(callback);
            }
        }));
    }
}

@@ -1565,33 +1565,32 @@
 am appId the app
-p
 lication id%0A    
@@ -1577,32 +1577,32 @@
  application id%0A
+
      * @param pr
@@ -1889,16 +1889,77 @@
  pusher%0A
+     * @param withEventIdOnly true to limit the push content%0A
      */%0A
@@ -2209,32 +2209,57 @@
  boolean append,
+ boolean withEventIdOnly,
  final ApiCallba
@@ -2377,32 +2377,55 @@
 me, url, append,
+ withEventIdOnly, true,
  callback, true)
@@ -2417,22 +2417,16 @@
 callback
-, true
 );%0A    %7D
@@ -2514,33 +2514,32 @@
 am appId the app
-p
 lication id%0A    
@@ -3200,23 +3200,30 @@
 se, 
-callback
+false
 , false
+, callback
 );%0A 
@@ -3598,32 +3598,93 @@
 d notifications%0A
+     * @param withEventIdOnly true to limit the push content%0A
      * @param ad
@@ -3997,17 +3997,17 @@
 , final 
-B
+b
 oolean a
@@ -4011,32 +4011,100 @@
 n append, final 
+boolean withEventIdOnly,%0A            final boolean addPusher, final 
 ApiCallback%3CVoid
@@ -4117,33 +4117,8 @@
 back
-, final boolean addPusher
 ) %7B%0A
@@ -4535,16 +4535,17 @@
 , url);%0A
+%0A
         
@@ -4604,24 +4604,122 @@
 %0A        %7D%0A%0A
+        if (withEventIdOnly) %7B%0A            pusher.data.put(%22format%22, %22event_id_only%22);%0A        %7D%0A%0A
         fina
@@ -5084,24 +5084,31 @@
 append, 
-callback
+withEventIdOnly
 , addPus
@@ -5110,16 +5110,26 @@
 ddPusher
+, callback
 );%0A     

Update the push regisration method to manage the restrict mode