/**
 * Copyright (C) 2004-2011 Jive Software. All rights reserved.
 *
 * 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.jivesoftware.sparkimpl.plugin.transcripts;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * A single chat transcript mapped to one JID.
 *
 * @author Derek DeMoro
 */
public class ChatTranscript {
    private List<HistoryMessage> messages = new ArrayList<>();

    /**
     * Add a <code>HistoryMessage</code> to users chat transcript.
     */
    public void addHistoryMessage(HistoryMessage entry) {
        messages.add(entry);
    }

    /**
     * Returns all messages.
     */
    public List<HistoryMessage> getMessages() {
        return messages;
    }

    /**
     * Returns a specified number of last messages.
     *
     * @param number the number of messages to return.
     * @return the specified number of messages, or all messages if number is greater than the current amount.
     */
    public Collection<HistoryMessage> getNumberOfEntries(int number) {
        int listSize = messages.size();
        if (listSize <= number) {
            return messages;
        }
        int start = listSize - number;
        return messages.subList(start, listSize);
    }

    /**
     * Returns messages that included search keywords.
     * 
     * @param text search keywords.If the search keywords is null, return all message.
     * @return the messages that included search keywords.
     */
    public List<HistoryMessage> getMessage(String text) {
        if (text == null || text.isEmpty()) {
            return messages;
        }
        String textNeedle = text.toLowerCase();
            List<HistoryMessage> searchResult = new ArrayList<>();
            Set<Integer> lines = new HashSet<>();
            HistoryMessage previous = null;
            int iter = -1;
            for(HistoryMessage message : messages) {
                iter++;
                if (message.getBody().toLowerCase().contains(textNeedle)) {
                    if (previous != null && !lines.contains(iter - 1)) {
                        searchResult.add(previous);
                        lines.add(iter - 1);
                    }
                    if (!lines.contains(iter)) {
                        searchResult.add(message);
                        lines.add(iter);
                    }
                    if (iter + 1 <= messages.size() - 1
                            && !lines.contains(iter + 1)) {
                        searchResult.add(messages.get(iter + 1));
                        lines.add(iter + 1);
                    }
                }
                previous = message;
            }
            return searchResult;
    }

    /**
     * Clears the Message History if it's not needed anymore
     */
    public void release() {
	messages.clear();
    }

    /**
     * Set the new List for the chat transcript
     * @param newList The list with the history messages
     */
    public void setList(List<HistoryMessage> newList){
    	this.messages = newList;
    }

    /**
     * Return the size of the message list
     */
    public int size() {
        return messages.size();
    }

    /**
     * Return the HistoryMessage by the given index
     * @return the history message, null if out of bounds
     */
    public HistoryMessage getMessage(int i) {
        return i >= 0 && i < messages.size() ? messages.get(i) : null;
    }
}
