/*
 * Copyright (c) 2015 LingoChamp Inc.
 *
 * 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.liulishuo.filedownloader;

import com.liulishuo.filedownloader.message.MessageSnapshot;
import com.liulishuo.filedownloader.message.MessageSnapshotFlow;
import com.liulishuo.filedownloader.model.FileDownloadStatus;
import com.liulishuo.filedownloader.util.FileDownloadLog;

import java.util.List;

/**
 * The message snapshot gate beyond the downloader service.
 */

public class MessageSnapshotGate implements MessageSnapshotFlow.MessageReceiver {

    private boolean transmitMessage(List<BaseDownloadTask.IRunningTask> taskList,
                                    MessageSnapshot snapshot) {

        if (taskList.size() > 1 && snapshot.getStatus() == FileDownloadStatus.completed) {
            for (BaseDownloadTask.IRunningTask task : taskList) {
                if (task.getMessageHandler().updateMoreLikelyCompleted(snapshot)) {
                    return true;
                }
            }
        }

        for (BaseDownloadTask.IRunningTask task : taskList) {
            if (task.getMessageHandler().updateKeepFlow(snapshot)) {
                return true;
            }
        }

        if (FileDownloadStatus.warn == snapshot.getStatus()) {
            for (BaseDownloadTask.IRunningTask task : taskList) {
                if (task.getMessageHandler().updateSameFilePathTaskRunning(snapshot)) {
                    return true;
                }
            }
        }

        //noinspection SimplifiableIfStatement
        if (taskList.size() == 1) {
            // Cover the most case for restarting from the low memory status.
            return taskList.get(0).getMessageHandler().updateKeepAhead(snapshot);
        }

        return false;
    }

    @Override
    public void receive(MessageSnapshot snapshot) {

        final String updateSyncLock = Integer.toString(snapshot.getId());
        synchronized (updateSyncLock.intern()) {
            final List<BaseDownloadTask.IRunningTask> taskList = FileDownloadList.getImpl().
                    getDownloadingList(snapshot.getId());


            if (taskList.size() > 0) {
                final BaseDownloadTask topOriginTask = taskList.get(0).getOrigin();

                if (FileDownloadLog.NEED_LOG) {
                    FileDownloadLog.d(this, "~~~callback %s old[%s] new[%s] %d",
                            snapshot.getId(), topOriginTask.getStatus(), snapshot.getStatus(),
                            taskList.size());
                }

                if (!transmitMessage(taskList, snapshot)) {

                    String log = "The flow callback did not consumed, id:" + snapshot.getId() + " status:"
                            + snapshot.getStatus() + " task-count:" + taskList.size();
                    for (BaseDownloadTask.IRunningTask task : taskList) {
                        log += " | " + task.getOrigin().getStatus();
                    }
                    FileDownloadLog.w(this, log);
                }


            } else {
                FileDownloadLog.w(this, "callback event transfer %d," +
                        " but is contains false", snapshot.getStatus());
            }

        }
    }
}

@@ -3142,28 +3142,18 @@
 The 
-flow callback did no
+event isn'
 t co
@@ -3481,25 +3481,25 @@
 DownloadLog.
-w
+i
 (this, log);
@@ -3576,17 +3576,17 @@
 Log.
-w
+i
 (this, %22
 call
@@ -3585,35 +3585,66 @@
 s, %22
-callback event transfer %25d,
+Receive the event %25d, but there isn't any running task in 
 %22 +%0A
@@ -3672,30 +3672,23 @@
    %22
- but is contains false
+the upper layer
 %22, s

correct the non-consumed log and downgrade the relate logs Closes #398