// Copyright 2015 Eivind Vegsundvåg
//
// 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 ninja.eivind.hotsreplayuploader.models;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import ninja.eivind.hotsreplayuploader.providers.Provider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Repesents a physical replay file, which can be parsed and uploaded to {@link Provider}s.<br>
 * Also keeps track of this file's {@link UploadStatus}.
 */
@DatabaseTable(tableName = "ReplayFile")
public class ReplayFile implements Serializable {
    private static final Logger LOG = LoggerFactory.getLogger(ReplayFile.class);
    private static final long serialVersionUID = 5416365418274150142L;
    private final BooleanProperty failedProperty = new SimpleBooleanProperty(null, "failed", false);
    @DatabaseField(generatedId = true)
    private long id;
    private File file;
    @DatabaseField(width = 1023, unique = true)
    private String fileName;

    @ForeignCollectionField(eager = true)
    private Collection<UploadStatus> uploadStatuses = new ArrayList<>();

    public ReplayFile() {}

    public ReplayFile(final File file) {
        this.file = file;
        this.fileName = file.toString();
    }

    public static List<ReplayFile> fromDirectory(File file) {
        final List<ReplayFile> replayFiles = new ArrayList<>();
        final File[] children = file.listFiles((dir, name) -> name.endsWith(".StormReplay"));
        for (final File child : children) {
            if (child.isDirectory()) {
                replayFiles.addAll(fromDirectory(child));
            } else {
                replayFiles.add(new ReplayFile(child));
            }
        }

        return replayFiles;
    }

    public long getId() {
        return id;
    }

    public void setId(final long id) {
        this.id = id;
    }

    public BooleanProperty getFailedProperty() {
        return failedProperty;
    }

    @Override
    public String toString() {
        return getFile().getName();
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        final ReplayFile that = (ReplayFile) o;

        return !(fileName != null ? !fileName.equals(that.fileName) : that.fileName != null);

    }

    @Override
    public int hashCode() {
        return file != null ? file.hashCode() : 0;
    }

    @JsonIgnore
    public Status getStatus() {
        // TODO MAKE MULTIPROVIDER-FRIENDLY
        if (uploadStatuses.isEmpty()) {
            return Status.NEW;
        }
        return uploadStatuses.iterator().next().getStatus();
    }

    public File getFile() {
        if (file == null && fileName != null) {
            file = new File(fileName);
        }
        return file;
    }

    public void addStatuses(final List<UploadStatus> list) {
        uploadStatuses.addAll(list);
    }

    public Collection<UploadStatus> getUploadStatuses() {
        return uploadStatuses;
    }

    public boolean hasExceptions() {
        return uploadStatuses.stream()
                .filter(uploadStatus -> uploadStatus.getStatus() == Status.EXCEPTION)
                .findAny()
                .isPresent();
    }

    public String getFileName() {
        return fileName;
    }

    public UploadStatus getUploadStatusForProvider(String provider) {
        return getUploadStatuses().stream()
                .filter(status -> status.getHost().equals(provider))
                .findFirst()
                .orElse(null);
    }
}

@@ -1080,28 +1080,149 @@
 java.io.
-Serializable
+IOException;%0Aimport java.io.Serializable;%0Aimport java.nio.file.FileVisitOption;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path
 ;%0Aimport
@@ -1294,16 +1294,52 @@
 il.List;
+%0Aimport java.util.stream.Collectors;
 %0A%0A/**%0A *
@@ -2331,389 +2331,262 @@
     
-final List%3CReplayFile%3E replayFiles = new ArrayList%3C%3E();%0A        final File%5B%5D children = file.listFiles((dir, name) -%3E name.endsWith(%22.StormReplay%22));%0A        for (final File child : children) %7B%0A            if (child.isDirectory()) %7B%0A                replayFiles.addAll(fromDirectory(child));%0A            %7D else %7B%0A                replayFiles.add(new ReplayFile(child));%0A            %7D
+try %7B%0A            return Files.walk(file.toPath(), FileVisitOption.FOLLOW_LINKS)%0A                    .map(Path::toFile)%0A                    .map(ReplayFile::new)%0A                    .collect(Collectors.toList());%0A        %7D catch (IOException exception) %7B
 %0A   
@@ -2594,38 +2594,60 @@
     
-%7D%0A%0A
     
-    return replayFiles;
+throw new RuntimeException(exception);%0A        %7D
 %0A   

Use Files#walk as directory travsersal rather than custom implementation.