package org.firstinspires.ftc.teamcode;

import com.qualcomm.robotcore.util.ReadWriteFile;

import org.firstinspires.ftc.robotcore.internal.system.AppUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.UUID;

public class LogThread implements Runnable {
    // NOTE: got static problems? welcome to the static capital of the world
    public ArrayList<String> queue = new ArrayList<>();
    public boolean isRunning = true;
    private String fileName;

    public LogThread(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public void run() {
        File logFile = AppUtil.getInstance().getSettingsFile("log-" + fileName + ".log");
        while (isRunning) {
            try {
                String elem = queue.remove(0);
                ReadWriteFile.writeFile(logFile, ReadWriteFile.readFile(logFile) + "\n\n" + elem);
            } catch (IndexOutOfBoundsException e) { /* no empty catch block here! */ }
        }

        for (String entry : queue) {
            ReadWriteFile.writeFile(logFile, ReadWriteFile.readFile(logFile) + "\n\n" + entry);
        }
    }
}
