/*
 * Copyright 2018 New Vector 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.util;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import org.matrix.androidsdk.rest.model.Event;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Useful methods to create permalink
 */
public class PermalinkUtils {

    private static final String MATRIX_TO_URL_BASE = "https://matrix.to/#/";

    /**
     * Creates a permalink for an event.
     * Ex: https://matrix.to/#/!nbzmcXAqpxBXjAdgoX:matrix.org/$1531497316352799BevdV:matrix.org
     *
     * @param event the event
     * @return the permalink, or null in case of error
     */
    @Nullable
    public static String createPermalink(Event event) {
        if (event == null) {
            return null;
        }

        return createPermalink(event.roomId, event.eventId);
    }

    /**
     * Creates a permalink for an id (can be a user Id, Room Id, etc.).
     * Ex: https://matrix.to/#/@benoit:matrix.org
     *
     * @param id the id
     * @return the permalink, or null in case of error
     */
    @Nullable
    public static String createPermalink(String id) {
        if (TextUtils.isEmpty(id)) {
            return null;
        }

        try {
            return MATRIX_TO_URL_BASE + URLEncoder.encode(id, "utf-8");
        } catch (UnsupportedEncodingException e) {
            // Should not happen
            Log.e("PermalinkUtils", "UnsupportedEncodingException", e);
        }

        return null;
    }

    /**
     * Creates a permalink for an event. If you have an event you can use {@link #createPermalink(Event)}
     * Ex: https://matrix.to/#/!nbzmcXAqpxBXjAdgoX:matrix.org/$1531497316352799BevdV:matrix.org
     *
     * @param roomId  the id of the room
     * @param eventId the id of the event
     * @return the permalink
     */
    @Nullable
    public static String createPermalink(@NonNull String roomId, @NonNull String eventId) {
        try {
            return MATRIX_TO_URL_BASE + URLEncoder.encode(roomId, "utf-8") + "/" + URLEncoder.encode(eventId, "utf-8");
        } catch (UnsupportedEncodingException e) {
            // Should not happen
            Log.e("PermalinkUtils", "UnsupportedEncodingException", e);
        }

        return null;
    }
}

@@ -800,82 +800,8 @@
 t;%0A%0A
-import java.io.UnsupportedEncodingException;%0Aimport java.net.URLEncoder;%0A%0A
 /**%0A
@@ -1764,34 +1764,16 @@
      %7D%0A%0A
-        try %7B%0A    
         
@@ -1804,226 +1804,18 @@
 E + 
-URLEncoder.encode(id, %22utf-8%22);%0A        %7D catch (UnsupportedEncodingException e) %7B%0A            // Should not happen%0A            Log.e(%22PermalinkUtils%22, %22UnsupportedEncodingException%22, e);%0A        %7D%0A%0A        return null
+escape(id)
 ;%0A  
@@ -2266,26 +2266,8 @@
 ) %7B%0A
-        try %7B%0A    
     
@@ -2302,274 +2302,287 @@
 E + 
-URLEncoder.encode(roomId, %22utf-8%22) + %22/%22 + URLEncoder.encode(eventId, %22utf-8%22);%0A        %7D catch (UnsupportedEncodingException e) %7B
+escape(roomId) + %22/%22 + escape(eventId);%0A    %7D%0A%0A    /**%0A     * Escape '/' in id, because it is used as a separator
 %0A     
+*%0A
      
-  // Should not happen%0A            Log.e(%22PermalinkUtils%22, %22UnsupportedEncodingException%22, e);%0A        %7D%0A%0A        return null
+* @param id the id to escape%0A     * @return the escaped id%0A     */%0A    private static String escape(String id) %7B%0A        return id.replaceAll(%22/%22, %22%252F%22)
 ;%0A  

Fix regression on matrix.to permalink