kernel = require("kernel")
reactor = peripheral.find("fissionReactorLogicAdapter")
turbine = peripheral.find("turbineValve")
turbineVent = peripheral.find("turbineVent")
monitor = peripheral.find("monitor")
internalEnvironment = peripheral.wrap("environmentDetector_0")
externalEnvironment = peripheral.wrap("environmentDetector_1")
modem = peripheral.find("modem")
controlRoomSpeakers = peripheral.find("speaker")

tempDriver = kernel.addDriver("temperature_driver")
coolantDriver = kernel.addDriver("coolant_driver")
statusDriver = kernel.addDriver("status_driver")
turbineDriver = kernel.addDriver("turbine_driver")
burnRateDriver = kernel.addDriver("burnrate_driver")
environmentDriver = kernel.addDriver("environment_driver")
speakerDriver = kernel.addDriver("speaker_driver")

isErrorState = false
reactorStatus = false

function setNewLine()
        local x,y = monitor.getCursorPos()
        monitor.setCursorPos(1, y + 1)
        monitor.clearLine()
end

local function runMonitors()
    if(reactorStatus) then
        parallel.waitForAll(
        tempDriver.watch, 
        coolantDriver.watch, 
        statusDriver.watch,
        turbineDriver.watch,
        burnRateDriver.watch,
        environmentDriver.watch)
    else
    end
end

local function runDisplay()
    monitor.clear()
    monitor.setTextScale(1)
    while true do
        monitor.setCursorPos(1, 0)
        setNewLine()
        monitor.setBackgroundColor(colors.blue)
        monitor.write("Reactor Readings:")
        local drivers = {
            { Label = "Status", driver = statusDriver},
            { Label = "Temperature", driver = tempDriver },
            { Label = "Coolant", driver = coolantDriver },
            { Label = "Turbine", driver = turbineDriver },
            { Label = "Burn Rate", driver = burnRateDriver },
            { Label = "Radiation Level", driver = environmentDriver }
        }


        for i, item in ipairs(drivers) do
            item.driver.report()
        end

        sleep(0.05)  -- Update every tenth second
    end
end

local function startup()
    parallel.waitForAll(tempDriver.startup,
    coolantDriver.startup,
    statusDriver.startup,
    turbineDriver.startup,
    burnRateDriver.startup,
    environmentDriver.startup)
end

local function shutDown()
    parallel.waitForAll(tempDriver.shutDown,
    coolantDriver.shutDown,
    statusDriver.shutDown,
    turbineDriver.shutDown,
    burnRateDriver.shutDown,
    environmentDriver.shutDown)
end



local function runSafe()
    while not reactor do
        print("Waiting for reactor signal...")
        sleep(1)
    end
    while not turbine do
        print("Waiting for turbine signal...")
        sleep(1)
    end
    while not monitor do
        print("Waiting for monitor signal...")
        sleep(1)
    end

    startup();
    monitor.clear();
    monitor.setBackgroundColor(colors.black)
    -- local names = peripheral.getNames()
    -- for index, value in ipairs(names) do
    --     print(index, value)
    --     read()
    -- end
    parallel.waitForAll(runMonitors, runDisplay)
end

local function run()
    local success, err = pcall(runSafe)
    if not success then
        monitor.setTextColor(colors.red)
        monitor.write("Error: " .. err)
        isErrorState = true
        modem.open(500)
        modem.transmit(500, 500, "Error: " .. err)
        os.reboot()
    end
end



return { run = run }
