/*
 *   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.bausystem.tracer.record;

public class RecordStateMachine {
    private RecordStateMachine() {
    }

    private static final TraceAutoHandler autoHandler = new TraceAutoHandler();

    private static RecordStatus recordStatus = RecordStatus.IDLE;
    private static Recorder recorder = null;

    public static void commandStart() {
        autoHandler.disable();
        recordStart();
        recordStatus = RecordStatus.RECORD;
    }

    public static void commandStop() {
        autoHandler.disable();
        recordStop();
        recordStatus = RecordStatus.IDLE;
    }

    public static void commandAuto() {
        if (recordStatus.isTracing())
            return;

        if (recordStatus == RecordStatus.IDLE_AUTO) {
            recordStatus = RecordStatus.IDLE;
            autoHandler.disable();
        } else {
            recordStatus = RecordStatus.IDLE_AUTO;
            autoHandler.enable();
        }
    }

    static void autoRecord() {
        recordStart();
        recordStatus = RecordStatus.RECORD_AUTO;
    }

    static void autoIdle() {
        recordStop();
        recordStatus = RecordStatus.IDLE_AUTO;
    }

    private static void recordStart() {
        if (recordStatus.isTracing()) return;
        recorder = new Recorder();
    }

    private static void recordStop() {
        if (!recordStatus.isTracing()) return;
        recorder.stopRecording();
    }

    public static RecordStatus getRecordStatus() {
        return recordStatus;
    }

    public static int size() {
        if (recorder == null) return 0;
        return recorder.size();
    }

    public static long getStartTime() {
        if (recorder == null) return 0;
        return recorder.getStartTime();
    }

    public static void postClear() {
        if (recorder == null) return;
        recorder.postClear();
    }
}
