/*
 * Copyright 2016 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.data;

import android.text.TextUtils;

import org.matrix.androidsdk.MXSession;

import org.matrix.androidsdk.rest.callback.ApiCallback;
import org.matrix.androidsdk.rest.model.Event;
import org.matrix.androidsdk.rest.model.MatrixError;
import org.matrix.androidsdk.rest.model.RoomResponse;

import java.util.Map;

/**
 * The `RoomEmailInvitation` gathers information for displaying the preview of a room that is unknown for the user.
 * Such room can come from an email invitation link or a link to a room.
 */
public class RoomPreviewData {

    private static final String LOG_TAG = "RoomPreviewData";

    // The id of the room to preview.
    private String mRoomId;

    // the room Alias
    private String mRoomAlias;

    // the id of the event to preview
    private String mEventId;

    // In case of email invitation, the information extracted from the email invitation link.
    private RoomEmailInvitation mRoomEmailInvitation;

    // preview information
    // comes from the email invitation or retrieve from an initialSync
    private String mRoomName;
    private String mRoomAvatarUrl;

    // the room state
    private RoomState mRoomState;

    // the initial sync data
    private RoomResponse mRoomResponse;

    // the session
    private MXSession mSession;

    /**
     * Create an RoomPreviewData instance
     * @param session the session.
     * @param roomId the room Id to preview
     * @param eventId the event Id to preview (optional)
     * @param emailInvitationParams the email invitation parameters (optional)
     */
    public RoomPreviewData(MXSession session, String roomId, String eventId, String roomAlias, Map<String, String> emailInvitationParams) {
        mSession = session;
        mRoomId = roomId;
        mRoomAlias = roomAlias;
        mEventId = eventId;

        if (null != emailInvitationParams) {
            mRoomEmailInvitation = new RoomEmailInvitation(emailInvitationParams);
            mRoomName = mRoomEmailInvitation.roomName;
            mRoomAvatarUrl = mRoomEmailInvitation.roomAvatarUrl;
        }
    }

    /**
     * @return the room state
     */
    public RoomState getRoomState() {
        return mRoomState;
    }

    /**
     * Update the room state.
     * @param roomState the new roomstate
     */
    public void setRoomState(RoomState roomState) {
        mRoomState = roomState;
    }

    /**
     * @return the room name
     */
    public String getRoomName() {
        return mRoomName;
    }

    /**
     * Set the room name.
     * @param aRoomName the new room name
     */
    public void setRoomName(String aRoomName) {
        mRoomName = aRoomName;
    }

    /**
     * @return the room avatar URL
     */
    public String getRoomAvatarUrl() {
        return mRoomAvatarUrl;
    }

    /**
     * @return the room id
     */
    public String getRoomId() {
        return mRoomId;
    }

    /**
     * @return the room id or the alias (alias is preferred)
     */
    public String getRoomIdOrAlias() {
        if (!TextUtils.isEmpty(mRoomAlias)) {
            return mRoomAlias;
        } else {
            return mRoomId;
        }
    }

    /**
     * @return the event id.
     */
    public String getEventId() {
        return mEventId;
    }

    /**
     * @return the session
     */
    public MXSession getSession() {
        return mSession;
    }

    /**
     * @return the initial sync response
     */
    public RoomResponse getRoomResponse() {
        return mRoomResponse;
    }

    /**
     * @return the room invitation
     */
    public RoomEmailInvitation getRoomEmailInvitation() {
        return mRoomEmailInvitation;
    }

    /**
     * Attempt to get more information from the homeserver about the room.
     * @param apiCallback the callback when the operation is done.
     */
    public void fetchPreviewData(final ApiCallback<Void> apiCallback) {
        mSession.getRoomsApiClient().initialSync(mRoomId, new ApiCallback<RoomResponse>() {
            @Override
            public void onSuccess(RoomResponse roomResponse) {
                // save the initial sync response
                mRoomResponse = roomResponse;

                mRoomState = new RoomState();
                mRoomState.roomId = mRoomId;

                for(Event event : roomResponse.state) {
                    mRoomState.applyState(event, EventTimeline.Direction.FORWARDS);
                }

                mRoomName = mRoomState.getDisplayName(mSession.getMyUserId());
                mRoomAvatarUrl = mRoomState.getAvatarUrl();

                apiCallback.onSuccess(null);
            }

            @Override
            public void onNetworkError(Exception e) {
                mRoomState = new RoomState();
                mRoomState.roomId = mRoomId;
                apiCallback.onNetworkError(e);
            }

            @Override
            public void onMatrixError(MatrixError e) {
                mRoomState = new RoomState();
                mRoomState.roomId = mRoomId;
                apiCallback.onMatrixError(e);
            }

            @Override
            public void onUnexpectedError(Exception e) {
                mRoomState = new RoomState();
                mRoomState.roomId = mRoomId;
                apiCallback.onUnexpectedError(e);
            }
        });
    }
}

@@ -3085,25 +3085,159 @@
         
-return mR
+String roomName = mRoomName;%0A%0A        if (TextUtils.isEmpty(roomName)) %7B%0A            roomName = getRoomIdOrAlias();%0A        %7D%0A%0A        return r
 oomName;

fix https://github.com/vector-im/vector-android/issues/194