/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.fightsystem.record;

import de.steamwar.fightsystem.Config;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.zip.GZIPInputStream;

public class FileSource extends PacketSource {

    public static File replayFile(int fightId) {
        return new File("/mnt/storage/replays", fightId + ".replay");
    }

    public static void startReplay() {
        if(Config.replayserver()) {
            try {
                new LiveServer();
            } catch (IOException e) {
                throw new SecurityException("Could not start replayserver", e);
            }
            return;
        }

        if(Config.ReplayID > 0) {
            try {
                new FileSource(replayFile(Config.ReplayID));
            } catch (IOException e) {
                throw new SecurityException("Could not start replay", e);
            }
        }
    }

    public FileSource(File fightFile) throws IOException {
        super(new GZIPInputStream(Files.newInputStream(fightFile.toPath())));
        new PacketProcessor(this);
    }

    @Override
    boolean isClosed() {
        try{
            return available() == 0;
        }catch (IOException e){
            return true;
        }
    }

    @Override
    boolean async() {
        return false;
    }
}
