/*
 * Copyright (c) 2016 Qiscus.
 *
 * 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 com.qiscus.sdk.ui;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.qiscus.nirmana.Nirmana;
import com.qiscus.sdk.Qiscus;
import com.qiscus.sdk.R;
import com.qiscus.sdk.data.model.QiscusChatRoom;
import com.qiscus.sdk.data.model.QiscusRoomMember;

import java.util.Date;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created on : November 24, 2016
 * Author     : zetbaitsu
 * Name       : Zetra
 * GitHub     : https://github.com/zetbaitsu
 */
public class QiscusGroupChatActivity extends QiscusChatActivity {

    protected String subtitle;

    public static Intent generateIntent(Context context, QiscusChatRoom qiscusChatRoom) {
        qiscusChatRoom.setGroup(true);
        Intent intent = new Intent(context, QiscusGroupChatActivity.class);
        intent.putExtra(CHAT_ROOM_DATA, qiscusChatRoom);
        return intent;
    }

    @Override
    protected void binRoomData() {
        tvTitle.setText(qiscusChatRoom.getName());
        generateSubtitle();
        tvSubtitle.setText(subtitle);
        tvSubtitle.setVisibility(View.VISIBLE);
        showRoomImage();
    }

    protected void generateSubtitle() {
        subtitle = "";
        int count = 0;
        for (QiscusRoomMember member : qiscusChatRoom.getMember()) {
            if (!member.getEmail().equalsIgnoreCase(Qiscus.getQiscusAccount().getEmail())) {
                count++;
                subtitle += member.getUsername().split(" ")[0];
                if (count < qiscusChatRoom.getMember().size() - 1) {
                    subtitle += ", ";
                }
            }
            if (count >= 10) {
                break;
            }
        }
        subtitle += String.format(" %s", getString(R.string.qiscus_group_member_closing));
        if (count == 0) subtitle = getString(R.string.qiscus_group_member_only_you);
    }

    @Override
    protected void showRoomImage() {
        Nirmana.getInstance().get().load(qiscusChatRoom.getAvatarUrl())
                .error(R.drawable.ic_qiscus_avatar)
                .placeholder(R.drawable.ic_qiscus_avatar)
                .dontAnimate()
                .into(ivAvatar);
    }

    @Override
    public void onUserStatusChanged(String user, boolean online, Date lastActive) {

    }

    @Override
    public void onUserTyping(String user, boolean typing) {
        if (typing) {
            Observable.from(qiscusChatRoom.getMember())
                    .filter(qiscusRoomMember -> qiscusRoomMember.getEmail().equals(user))
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .compose(bindToLifecycle())
                    .subscribe(qiscusRoomMember -> tvSubtitle.setText(getString(R.string.qiscus_group_member_typing,
                            qiscusRoomMember.getUsername())), throwable -> {
                    });
        } else {
            tvSubtitle.setText(subtitle);
        }
    }
}

@@ -888,42 +888,162 @@
 scus
-RoomMember;%0A%0Aimport java.util.Date
+Comment;%0Aimport com.qiscus.sdk.data.model.QiscusRoomMember;%0A%0Aimport java.io.File;%0Aimport java.util.ArrayList;%0Aimport java.util.Date;%0Aimport java.util.List
 ;%0A%0Ai
@@ -1663,32 +1663,880 @@
  intent;%0A    %7D%0A%0A
+    public static Intent generateIntent(Context context, QiscusChatRoom qiscusChatRoom,%0A                                        String startingMessage, File shareFile,%0A                                        boolean autoSendExtra, List%3CQiscusComment%3E comments,%0A                                        QiscusComment scrollToComment) %7B%0A        Intent intent = new Intent(context, QiscusGroupChatActivity.class);%0A        intent.putExtra(CHAT_ROOM_DATA, qiscusChatRoom);%0A        intent.putExtra(EXTRA_STARTING_MESSAGE, startingMessage);%0A        intent.putExtra(EXTRA_SHARING_FILE, shareFile);%0A        intent.putExtra(EXTRA_AUTO_SEND, autoSendExtra);%0A        intent.putParcelableArrayListExtra(EXTRA_FORWARD_COMMENTS, (ArrayList%3CQiscusComment%3E) comments);%0A        intent.putExtra(EXTRA_SCROLL_TO_COMMENT, scrollToComment);%0A        return intent;%0A    %7D%0A%0A
     @Override%0A  

Add intent genartor for group chat