package com.example.Repositories;

import com.example.Models.Program.ProgramState;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import com.example.Exceptions.BaseException;

public class Repository implements IRepository {
    private List<ProgramState> programStates;
    private String logFilePath;

    public Repository() {
        this.programStates = new ArrayList<ProgramState>();
        this.logFilePath = "log.txt";
    }

    public Repository(String logFilePath) {
        this.programStates = new ArrayList<ProgramState>();
        this.logFilePath = logFilePath;
    }

    public void addProgramState(ProgramState programState) throws BaseException {
        programState.typeCheck();
        programStates.clear();
        programStates.add(programState);
    }

    public List<ProgramState> getProgramStates() {
        return programStates;
    }

    public void setProgramStates(List<ProgramState> programStates) {
        this.programStates = programStates;
    }

    public void logProgramStateExecution(ProgramState state) throws BaseException{
        try{
            var logFile = new PrintWriter(new BufferedWriter(new FileWriter(this.logFilePath, true))); 
            logFile.println(state.toString());
            logFile.close();
        }
        catch(Exception exception){
            throw new BaseException(exception.getMessage());
        }
    }
}
