--  BigReactor Control
--  by jaranvil aka jared314
--
--  feel free to use and/or modify this code
--
-----------------------------------------------
--Reactor Control - Version History
--
--  Version 2.3 - April 23/15
--    - First update in awhile 
--    - lots of minor fixes and improvments 
--    - New loading and setup search with automated peripheral search 
--    - Changes to the update methods
--    - Except new updates soon


-----------------------------------------------

local version = 2.3
--is auto power enabled 
local auto_string = false 
--auto on value
local on = 0 
--auto off value
local off = 99 
--is auto control rods enabled 
local auto_rods = false 
--control rod auto value
local auto_rf = 0 

--peripherals
local reactor
local mon

--monitor size
local monX
local monY

term.clear()
-------------------FORMATTING-------------------------------
function clear()
  mon.setBackgroundColor(colors.black)
  mon.clear()
  mon.setCursorPos(1,1)
end

--display text on computer's terminal screen
function draw_text_term(x, y, text, text_color, bg_color)
  term.setTextColor(text_color)
  term.setBackgroundColor(bg_color)
  term.setCursorPos(x,y)
  write(text)
end

--display text text on monitor, "mon" peripheral
function draw_text(x, y, text, text_color, bg_color)
  mon.setBackgroundColor(bg_color)
  mon.setTextColor(text_color)
  mon.setCursorPos(x,y)
  mon.write(text)
end

--draw line on computer terminal
function draw_line(x, y, length, color)
    mon.setBackgroundColor(color)
    mon.setCursorPos(x,y)
    mon.write(string.rep(" ", length))
end

--draw line on computer terminal
function draw_line_term(x, y, length, color)
    term.setBackgroundColor(color)
    term.setCursorPos(x,y)
    term.write(string.rep(" ", length))
end

--create progress bar
--draws two overlapping lines
--background line of bg_color 
--main line of bar_color as a percentage of minVal/maxVal
function progress_bar(x, y, length, minVal, maxVal, bar_color, bg_color)
  draw_line(x, y, length, bg_color) --backgoround bar
  local barSize = math.floor((minVal/maxVal) * length) 
  draw_line(x, y, barSize, bar_color) --progress so far
end

--same as above but on the computer terminal
function progress_bar_term(x, y, length, minVal, maxVal, bar_color, bg_color)
  draw_line_term(x, y, length, bg_color) --backgoround bar
  local barSize = math.floor((minVal/maxVal) * length) 
  draw_line_term(x, y, barSize, bar_color)  --progress so far
end

--create button on monitor
function button(x, y, length, text, txt_color, bg_color)
  draw_line(x, y, length, bg_color)
  draw_text((x+2), y, text, txt_color, bg_color)
end

--header and footer bars on monitor
function menu_bar()
  draw_line(1, 1, monX, colors.blue)
  draw_text(2, 1, "Power    Tools    Settings", colors.white, colors.blue)
  draw_line(1, 19, monX, colors.blue)
  draw_text(2, 19, "     Reactor Control", colors.white, colors.blue)
end

--dropdown menu for power options
function power_menu()
  draw_line(1, 2, 9, colors.gray)
  draw_line(1, 3, 9, colors.gray)
  draw_line(1, 4, 9, colors.gray)
  if active then 
    draw_text(2, 2, "ON", colors.lightGray, colors.gray)
    draw_text(2, 3, "OFF", colors.white, colors.gray)
  else
    draw_text(2, 2, "ON", colors.white, colors.gray)
    draw_text(2, 3, "OFF", colors.lightGray, colors.gray)
  end
  draw_text(2, 4, "Auto", colors.white, colors.gray)
end

--dropbox menu for tools
function tools_menu()
  draw_line(10, 2, 14, colors.gray)
  draw_line(10, 3, 14, colors.gray)
  draw_line(10, 4, 14, colors.gray)
  draw_line(10, 5, 14, colors.gray)
  draw_text(11, 2, "Control Rods", colors.white, colors.gray)
  draw_text(11, 3, "Efficiency", colors.white, colors.gray) 
  draw_text(11, 4, "Fuel", colors.white, colors.gray)
  draw_text(11, 5, "Waste", colors.white, colors.gray)
end

--dropdown menu for settings
function settings_menu()
  draw_line(12, 2, 18, colors.gray)
  draw_line(12, 3, 18, colors.gray)
  draw_text(13, 2, "Check for Updates", colors.white, colors.gray)
  draw_text(13, 3, "Reset peripherals", colors.white, colors.gray)
end

--basic popup screen with title bar and exit button 
function popup_screen(y, title, height)
  clear()
  menu_bar()

  draw_line(4, y, 22, colors.blue)
  draw_line(25, y, 1, colors.red)

  for counter = y+1, height+y do
    draw_line(4, counter, 22, colors.white)
  end

  draw_text(25, y, "X", colors.white, colors.red)
  draw_text(5, y, title, colors.white, colors.blue)
end

--write settings to config file
function save_config()
  sw = fs.open("config.txt", "w")   
    sw.writeLine(version)
    sw.writeLine(auto_string)
    sw.writeLine(on)
    sw.writeLine(off)
    sw.writeLine(auto_rods)
    sw.writeLine(auto_rf)
  sw.close()
end

--read settings from file
function load_config()
  sr = fs.open("config.txt", "r")
    version = tonumber(sr.readLine())
    auto_string = sr.readLine()
    on = tonumber(sr.readLine())
    off = tonumber(sr.readLine())
    auto_rods = sr.readLine()
    auto_rf = tonumber(sr.readLine())
  sr.close()
end

------------------------END FORMATTING--------------------------

--
function homepage()
  while true do
    clear()
    menu_bar()
    terminal_screen()

    energy_stored = reactor.getEnergyStored()
    
    --------POWER STAT--------------
    draw_text(2, 3, "Power:", colors.yellow, colors.black)
    active = reactor.getActive()
    if active then
      draw_text(10, 3, "ONLINE", colors.lime, colors.black)
    else
      draw_text(10, 3, "OFFLINE", colors.red, colors.black)
    end

    -----------FUEL---------------------
    draw_text(2, 5, "Fuel Level:", colors.yellow, colors.black)
    local maxVal = reactor.getFuelAmountMax()
    local minVal = reactor.getFuelAmount() 
    local percent = math.floor((minVal/maxVal)*100)
    draw_text(15, 5, percent.."%", colors.white, colors.black)

    if percent < 25 then
    progress_bar(2, 6, monX-2, minVal, maxVal, colors.red, colors.gray)
    else if percent < 50 then
    progress_bar(2, 6, monX-2, minVal, maxVal, colors.orange, colors.gray)
    else if percent < 75 then 
    progress_bar(2, 6, monX-2, minVal, maxVal, colors.yellow, colors.gray)
    else if percent <= 100 then
    progress_bar(2, 6, monX-2, minVal, maxVal, colors.lime, colors.gray)
    end
    end
    end
    end

    -----------ROD HEAT---------------
    draw_text(2, 8, "Fuel Temp:", colors.yellow, colors.black)
    local maxVal = 2000
    local minVal = math.floor(reactor.getFuelTemperature())

    if minVal < 500 then
    progress_bar(2, 9, monX-2, minVal, maxVal, colors.lime, colors.gray)
    else if minVal < 1000 then
    progress_bar(2, 9, monX-2, minVal, maxVal, colors.yellow, colors.gray)
    else if minVal < 1500 then  
    progress_bar(2, 9, monX-2, minVal, maxVal, colors.orange, colors.gray)
    else if minVal < 2000 then
    progress_bar(2, 9, monX-2, minVal, maxVal, colors.red, colors.gray)
    else if minVal >= 2000 then
      progress_bar(2, 9, monX-2, 2000, maxVal, colors.red, colors.gray)
    end
    end
    end
    end
    end

    draw_text(15, 8, math.floor(minVal).."/"..maxVal, colors.white, colors.black)

    -----------CASING HEAT---------------
    draw_text(2, 11, "Casing Temp:", colors.yellow, colors.black)
    local maxVal = 2000
    local minVal = math.floor(reactor.getCasingTemperature())
    if minVal < 500 then
    progress_bar(2, 12, monX-2, minVal, maxVal, colors.lime, colors.gray)
    else if minVal < 1000 then
    progress_bar(2, 12, monX-2, minVal, maxVal, colors.yellow, colors.gray)
    else if minVal < 1500 then  
    progress_bar(2, 12, monX-2, minVal, maxVal, colors.orange, colors.gray)
    else if minVal < 2000 then
    progress_bar(2, 12, monX-2, minVal, maxVal, colors.red, colors.gray)
    else if minVal >= 2000 then
      progress_bar(2, 12, monX-2, 2000, maxVal, colors.red, colors.gray)
    end
    end
    end
    end
    end
    draw_text(15, 11, math.floor(minVal).."/"..maxVal, colors.white, colors.black)

    -------------OUTPUT-------------------
    if reactor.isActivelyCooled() then

      draw_text(2, 14, "mB/tick:", colors.yellow, colors.black)
      mbt = math.floor(reactor.getHotFluidProducedLastTick())
      draw_text(13, 14, mbt.." mB/t", colors.white, colors.black)

    else

      draw_text(2, 14, "RF/tick:", colors.yellow, colors.black)
      rft = math.floor(reactor.getEnergyProducedLastTick())
      draw_text(13, 14, rft.." RF/T", colors.white, colors.black)

    end

    ------------STORAGE------------
    if reactor.isActivelyCooled() then 

      draw_text(2, 15, "mB Stored:", colors.yellow, colors.black)
      fluid_stored = reactor.getHotFluidAmount()
      fluid_max = reactor.getHotFluidAmountMax()
      fluid_stored_percent = math.floor((fluid_stored/fluid_max)*100)
      draw_text(13, 15, fluid_stored_percent.."% ("..fluid_stored.." mB)", colors.white, colors.black)

    else

      draw_text(2, 15, "RF Stored:", colors.yellow, colors.black)
      energy_stored_percent = math.floor((energy_stored/10000000)*100)
      draw_text(13, 15, energy_stored_percent.."% ("..energy_stored.." RF)", colors.white, colors.black)


    end

    -------------AUTO CONTROL RODS-----------------------
    auto_rods_bool = auto_rods == "true"
    insertion_percent = reactor.getControlRodLevel(0)

    if reactor.isActivelyCooled() then
      draw_text(2, 16, "Control Rods:", colors.yellow, colors.black)
      draw_text(16, 16, insertion_percent.."%", colors.white, colors.black)
    else

      if auto_rods_bool then
        if active then
          if rft > auto_rf+50 then
            reactor.setAllControlRodLevels(insertion_percent+1)
          else if rft < auto_rf-50 then
            reactor.setAllControlRodLevels(insertion_percent-1)
          end
          end
        end

        draw_text(2, 16, "Control Rods:", colors.yellow, colors.black)
        draw_text(16, 16, insertion_percent.."%", colors.white, colors.black)
        draw_text(21, 16, "(Auto)", colors.red, colors.black)
      
      else
        draw_text(2, 16, "Control Rods:", colors.yellow, colors.black)
        draw_text(16, 16, insertion_percent.."%", colors.white, colors.black)
      end
    end


    -------------AUTO SHUTOFF--------------------------
    if reactor.isActivelyCooled() then

      --i dont know what I should do here


    else
      auto = auto_string == "true"
      if auto then
        if active then
          draw_text(2, 17, "Auto off:", colors.yellow, colors.black)
          draw_text(13, 17, off.."% RF Stored", colors.white, colors.black)
          if energy_stored_percent >= off then
            reactor.setActive(false)
            call_homepage()
          end
        else
          draw_text(2, 17, "Auto on:", colors.yellow, colors.black)
          draw_text(13, 17, on.."% RF Stored", colors.white, colors.black)
          if energy_stored_percent <= on then
            reactor.setActive(true)
            call_homepage()
          end
        end
      else
        draw_text(2, 17, "Auto power:", colors.yellow, colors.black)
        draw_text(14, 17, "disabled", colors.red, colors.black)
      end
    end

    sleep(0.5)
  end
end

--------------MENU SCREENS--------------

--auto power menu
function auto_off()

  auto = auto_string == "true"
  if auto then --auto power enabled

    popup_screen(3, "Auto Power", 11)
    draw_text(5, 5, "Enabled", colors.lime, colors.white)
    draw_text(15, 5, " disable ", colors.white, colors.black)
    
    draw_text(5, 7, "ON when storage =", colors.gray, colors.white)
    draw_text(5, 8, " - ", colors.white, colors.black)
    draw_text(13, 8, on.."% RF", colors.black, colors.white)
    draw_text(22, 8, " + ", colors.white, colors.black)

    draw_text(5, 10, "OFF when storage =", colors.gray, colors.white)
    draw_text(5, 11, " - ", colors.white, colors.black)
    draw_text(13, 11, off.."% RF", colors.black, colors.white)
    draw_text(22, 11, " + ", colors.white, colors.black)

    draw_text(11, 13, " Save ", colors.white, colors.black)

    local event, side, xPos, yPos = os.pullEvent("monitor_touch")

    --disable auto
    if yPos == 5 then
      if xPos >= 15 and xPos <= 21 then
        auto_string = "false"
        save_config()
        auto_off()
      else
        auto_off()
      end
    end

    --increase/decrease auto on %
    if yPos == 8 then
      if xPos >= 5 and xPos <= 8 then
        previous_on = on
        on = on-1
      end
      if xPos >= 22 and xPos <= 25 then
        previous_on = on
        on = on+1
      end
    end

    --increase/decrease auto off %
    if yPos == 11 then
      if xPos >= 5 and xPos <= 8 then
        previous_off = off
        off = off-1
      end
      if xPos >= 22 and xPos <= 25 then
        previous_off = off
        off = off+1
      end
    end

    if on < 0 then on = 0 end
    if off >99 then off = 99 end

    if on == off or on > off then
      on = previous_on
      off = previous_off
      popup_screen(5, "Error", 6)
      draw_text(5, 7, "Auto On value must be", colors.black, colors.white)
      draw_text(5, 8, "lower then auto off", colors.black, colors.white)
      draw_text(11, 10, "Okay", colors.white, colors.black)
      local event, side, xPos, yPos = os.pullEvent("monitor_touch")

      auto_off()
    end

    --Okay button
    if yPos == 13 and xPos >= 11 and xPos <= 17 then 
      save_config()
      call_homepage()
    end

    --Exit button
    if yPos == 3 and xPos == 25 then 
      call_homepage()
    end

    auto_off()
  else
    popup_screen(3, "Auto Power", 5)
    draw_text(5, 5, "Disabled", colors.red, colors.white)
    draw_text(15, 5, " enable ", colors.white, colors.gray)
    draw_text(11, 7, "Okay", colors.white, colors.black)

    local event, side, xPos, yPos = os.pullEvent("monitor_touch")

    --Okay button
    if yPos == 7 and xPos >= 11 and xPos <= 17 then 
      call_homepage()
    end

    if yPos == 5 then
      if xPos >= 15 and xPos <= 21 then
        auto_string = "true"
        save_config()
        auto_off()
      else
        auto_off()
      end
    else
      auto_off()
    end
  end
end

--efficiency menu
function efficiency()
  popup_screen(3, "Efficiency", 12)
  fuel_usage = reactor.getFuelConsumedLastTick()
  rft = math.floor(reactor.getEnergyProducedLastTick())

  rfmb = rft / fuel_usage

  draw_text(5, 5, "Fuel Consumption: ", colors.lime, colors.white)
  draw_text(5, 6, fuel_usage.." mB/t", colors.black, colors.white)
  draw_text(5, 8, "Energy per mB: ", colors.lime, colors.white)
  draw_text(5, 9, rfmb.." RF/mB", colors.black, colors.white)

  draw_text(5, 11, "RF/tick:", colors.lime, colors.white)
  draw_text(5, 12, rft.." RF/T", colors.black, colors.white)

  draw_text(11, 14, " Okay ", colors.white, colors.black)

  local event, side, xPos, yPos = os.pullEvent("monitor_touch")

  --Okay button
  if yPos == 14 and xPos >= 11 and xPos <= 17 then 
    call_homepage()
  end

  --Exit button
  if yPos == 3 and xPos == 25 then 
    call_homepage()
  end

  efficiency()
end


function fuel()
  popup_screen(3, "Fuel", 9)

  fuel_max = reactor.getFuelAmountMax()
  fuel_level = reactor.getFuelAmount()
  fuel_reactivity = math.floor(reactor.getFuelReactivity())

  draw_text(5, 5, "Fuel Level: ", colors.lime, colors.white)
  draw_text(5, 6, fuel_level.."/"..fuel_max, colors.black, colors.white)

  draw_text(5, 8, "Reactivity: ", colors.lime, colors.white)
  draw_text(5, 9, fuel_reactivity.."%", colors.black, colors.white)

  draw_text(11, 11, " Okay ", colors.white, colors.black)

  local event, side, xPos, yPos = os.pullEvent("monitor_touch")


  --Okay button
  if yPos == 11 and xPos >= 11 and xPos <= 17 then 
    call_homepage()
  end

  --Exit button
  if yPos == 3 and xPos == 25 then 
    call_homepage()
  end

  fuel()
end

function waste()
  popup_screen(3, "Waste", 8)

  waste_amount = reactor.getWasteAmount()
  draw_text(5, 5, "Waste Amount: ", colors.lime, colors.white)
  draw_text(5, 6, waste_amount.." mB", colors.black, colors.white)
  draw_text(8, 8, " Eject Waste ", colors.white, colors.red)
  draw_text(11, 10, " Close ", colors.white, colors.black)

  local event, side, xPos, yPos = os.pullEvent("monitor_touch")

  --eject button
  if yPos == 8 and xPos >= 8 and xPos <= 21 then 
    reactor.doEjectWaste()
    popup_screen(5, "Waste Eject", 5)
    draw_text(5, 7, "Waste Ejeceted.", colors.black, colors.white)
    draw_text(11, 9, " Close ", colors.white, colors.black)
    local event, side, xPos, yPos = os.pullEvent("monitor_touch")
    --Okay button
    if yPos == 7 and xPos >= 11 and xPos <= 17 then 
      call_homepage()
    end

    --Exit button
    if yPos == 3 and xPos == 25 then 
      call_homepage()
    end
  end

  --Okay button
  if yPos == 10 and xPos >= 11 and xPos <= 17 then 
    call_homepage()
  end

  --Exit button
  if yPos == 3 and xPos == 25 then 
    call_homepage()
  end
  waste()
end

function set_auto_rf()
  popup_screen(5, "Auto Adjust", 11)
    draw_text(5, 7, "Try to maintain:", colors.black, colors.white)

    draw_text(13, 9, " ^ ", colors.white, colors.gray)
    draw_text(10, 11, auto_rf.." RF/t", colors.black, colors.white)
    draw_text(13, 13, " v ", colors.white, colors.gray)
    draw_text(11, 15, " Okay ", colors.white, colors.gray)

    local event, side, xPos, yPos = os.pullEvent("monitor_touch")

    --increase button
    if yPos == 9 then 
      auto_rf = auto_rf + 100
      save_config()
      set_auto_rf()
    end

    --decrease button
    if yPos == 13 then 
      auto_rf = auto_rf - 100
      if auto_rf < 0 then auto_rf = 0 end
      save_config()
      set_auto_rf()
    end

    if yPos == 15 then 
      control_rods()
    end

    set_auto_rf()
end

function control_rods()

  if reactor.isActivelyCooled() then 

    popup_screen(3, "Control Rods", 13)
    insertion_percent = reactor.getControlRodLevel(0)

    draw_text(5, 5, "Inserted: "..insertion_percent.."%", colors.black, colors.white)
    progress_bar(5, 7, 20, insertion_percent, 100, colors.yellow, colors.gray)

    draw_text(5, 9, " << ", colors.white, colors.black)
    draw_text(10, 9, " < ", colors.white, colors.black)
    draw_text(17, 9, " > ", colors.white, colors.black)
    draw_text(21, 9, " >> ", colors.white, colors.black)

    draw_text(5, 11, "Auto:", colors.black, colors.white)
    draw_text(5, 13, "unavilable for", colors.red, colors.white)
    draw_text(5, 14, "active cooling", colors.red, colors.white)

    draw_text(11, 16, " Close ", colors.white, colors.gray)

    local event, side, xPos, yPos = os.pullEvent("monitor_touch")

    if yPos == 9 and xPos >= 5 and xPos <= 15 then 
      reactor.setAllControlRodLevels(insertion_percent-10)
    end

    if yPos == 9 and xPos >= 10 and xPos <= 13 then 
      reactor.setAllControlRodLevels(insertion_percent-1)
    end

    if yPos == 9 and xPos >= 17 and xPos <= 20 then 
      reactor.setAllControlRodLevels(insertion_percent+1)
    end

    if yPos == 9 and xPos >= 21 and xPos <= 25 then 
      reactor.setAllControlRodLevels(insertion_percent+10)
    end

    ------Close button-------
    if yPos == 16 and xPos >= 11 and xPos <= 17 then 
      call_homepage()
    end

    ------Exit button------------
    if yPos == 5 and xPos == 25 then 
      call_homepage()
    end
    control_rods()

  else

    popup_screen(3, "Control Rods", 13)
    insertion_percent = reactor.getControlRodLevel(0)

    draw_text(5, 5, "Inserted: "..insertion_percent.."%", colors.black, colors.white)
    progress_bar(5, 7, 20, insertion_percent, 100, colors.yellow, colors.gray)

    draw_text(5, 9, " << ", colors.white, colors.black)
    draw_text(10, 9, " < ", colors.white, colors.black)
    draw_text(17, 9, " > ", colors.white, colors.black)
    draw_text(21, 9, " >> ", colors.white, colors.black)

    draw_text(5, 11, "Auto:", colors.black, colors.white)
    draw_text(16, 11, " disable ", colors.white, colors.black)

    auto_rods_bool = auto_rods == "true"
    if auto_rods_bool then
      
      draw_text(5, 13, "RF/t: "..auto_rf, colors.black, colors.white)
      draw_text(18, 13, " set ", colors.white, colors.black)
    else
      draw_text(16, 11, " enable ", colors.white, colors.black)
      draw_text(5, 13, "disabled", colors.red, colors.white)
    end

    draw_text(11, 15, " Close ", colors.white, colors.gray)

    local event, side, xPos, yPos = os.pullEvent("monitor_touch")

    -----manual adjust buttons------------
    if yPos == 9 and xPos >= 5 and xPos <= 15 then 
      reactor.setAllControlRodLevels(insertion_percent-10)
    end

    if yPos == 9 and xPos >= 10 and xPos <= 13 then 
      reactor.setAllControlRodLevels(insertion_percent-1)
    end

    if yPos == 9 and xPos >= 17 and xPos <= 20 then 
      reactor.setAllControlRodLevels(insertion_percent+1)
    end

    if yPos == 9 and xPos >= 21 and xPos <= 25 then 
      reactor.setAllControlRodLevels(insertion_percent+10)
    end


    ------auto buttons-----------------
    if yPos == 11 and xPos >= 16 then 
      if auto_rods_bool then
        auto_rods = "false"
        save_config()
        control_rods()
      else
        auto_rods = "true"
        save_config()
        control_rods()
      end
    end

    if yPos == 13 and xPos >= 18 then 
      set_auto_rf()
    end

    ------Close button-------
    if yPos == 15 and xPos >= 11 and xPos <= 17 then 
      call_homepage()
    end

    ------Exit button------------
    if yPos == 5 and xPos == 25 then 
      call_homepage()
    end
    control_rods()

  end
end

-----------------------Settings--------------------------------


function rf_mode()
  wait = read()
end

function steam_mode()
  wait = read()
end

function install_update(program, pastebin)
    clear()
    draw_line(4, 5, 22, colors.blue)

    for counter = 6, 10 do
      draw_line(4, counter, 22, colors.white)
    end

    draw_text(5, 5, "Updating...", colors.white, colors.blue)
    draw_text(5, 7, "Open computer", colors.black, colors.white)
    draw_text(5, 8, "terminal.", colors.black, colors.white)

    if fs.exists("install") then fs.delete("install") end
    shell.run("pastebin get p4zeq7Ma install")
    shell.run("install")
end

function update()
  popup_screen(5, "Updates", 4)
  draw_text(5, 7, "Connecting to", colors.black, colors.white)
  draw_text(5, 8, "pastebin...", colors.black, colors.white)

  sleep(0.5)
  
  shell.run("pastebin get MkF2QQjH current_version.txt")
  sr = fs.open("current_version.txt", "r")
  current_version = tonumber(sr.readLine())
  sr.close()
  fs.delete("current_version.txt")
  terminal_screen()

  if current_version > version then

    popup_screen(5, "Updates", 7)
    draw_text(5, 7, "Update Available!", colors.black, colors.white)
    draw_text(11, 9, " Install ", colors.white, colors.black)
    draw_text(11, 11, " Ignore ", colors.white, colors.black)

    local event, side, xPos, yPos = os.pullEvent("monitor_touch")

    --Instatll button
    if yPos == 9 and xPos >= 11 and xPos <= 17 then 
      install_update()
    end

    --Exit button
    if yPos == 5 and xPos == 25 then 
      call_homepage()
    end
    call_homepage()

  else
    popup_screen(5, "Updates", 5)
    draw_text(5, 7, "You are up to date!", colors.black, colors.white)
    draw_text(11, 9, " Okay ", colors.white, colors.black)

    local event, side, xPos, yPos = os.pullEvent("monitor_touch")

    --Okay button
    if yPos == 9 and xPos >= 11 and xPos <= 17 then 
      call_homepage()
    end

    --Exit button
    if yPos == 5 and xPos == 25 then 
      call_homepage()
    end
    call_homepage()
  end

  

end

function reset_peripherals()
  clear()
  draw_line(4, 5, 22, colors.blue)

  for counter = 6, 10 do
    draw_line(4, counter, 22, colors.white)
  end

  draw_text(5, 5, "Reset Peripherals", colors.white, colors.blue)
  draw_text(5, 7, "Open computer", colors.black, colors.white)
  draw_text(5, 8, "terminal.", colors.black, colors.white)
  setup_wizard()

end

--stop running status screen if monitors was touched
function stop()
  while true do
    local event, side, xPos, yPos = os.pullEvent("monitor_touch")
      x = xPos
      y = yPos
      stop_function = "monitor_touch"
    return
  end 
end

function mon_touch()
  --when the monitor is touch on the homepage
  if y == 1 then 
      if x < monX/3 then
        power_menu()
        local event, side, xPos, yPos = os.pullEvent("monitor_touch")
        if xPos < 9 then
          if yPos == 2 then
            reactor.setActive(true)
            timer = 0 --reset anytime the reactor is turned on/off
            call_homepage()
          else if yPos == 3 then
            reactor.setActive(false)
            timer = 0 --reset anytime the reactor is turned on/off
            call_homepage()
          else if yPos == 4 then
            auto_off()
          else
            call_homepage()
          end
          end
          end
        else
          call_homepage()
        end
        
      else if x < 20 then
        tools_menu()
        local event, side, xPos, yPos = os.pullEvent("monitor_touch")
        if xPos < 25 and xPos > 10 then
          if yPos == 2 then
            control_rods()
          else if yPos == 3 then
            efficiency()
          else if yPos == 4 then
            fuel()
          else if yPos == 5 then
            waste()
          else
            call_homepage()
          end
          end
          end
          end
        else
          call_homepage()
        end
      else if x < monX then
        settings_menu()
        local event, side, xPos, yPos = os.pullEvent("monitor_touch")
        if xPos > 13 then
          if yPos == 2 then
            update()
          else if yPos == 3 then
            reset_peripherals() 
          else
            call_homepage()
          end
          end
        else
          call_homepage()
        end
      end
      end
      end
    else
      call_homepage()
    end
end

function terminal_screen()
  term.clear()
  draw_line_term(1, 1, 55, colors.blue)
  draw_text_term(13, 1, "BigReactor Controls", colors.white, colors.blue)
  draw_line_term(1, 19, 55, colors.blue)
  draw_text_term(13, 19, "by jaranvil aka jared314", colors.white, colors.blue)

  draw_text_term(1, 3, "Current program:", colors.white, colors.black)
  draw_text_term(1, 4, "Reactor Control v"..version, colors.blue, colors.black)
  
  draw_text_term(1, 6, "Installer:", colors.white, colors.black)
  draw_text_term(1, 7, "pastebin.com/p4zeq7Ma", colors.blue, colors.black)

  draw_text_term(1, 9, "Please give me your feedback, suggestions,", colors.white, colors.black)
  draw_text_term(1, 10, "and errors!", colors.white, colors.black)

  draw_text_term(1, 11, "reddit.com/r/br_controls", colors.blue, colors.black)
end

--run both homepage() and stop() until one returns
function call_homepage()
  clear()
  parallel.waitForAny(homepage, stop) 

  if stop_function == "terminal_screen" then 
    stop_function = "nothing"
    setup_wizard()
  else if stop_function == "monitor_touch" then
    stop_function = "nothing"
    mon_touch()
  end
  end
end

--test if the entered monitor and reactor can be wrapped
  function test_configs()
  term.clear()

  draw_line_term(1, 1, 55, colors.blue)
  draw_text_term(10, 1, "BigReactors Controls", colors.white, colors.blue)
  
  draw_line_term(1, 19, 55, colors.blue)
  draw_text_term(10, 19, "by jaranvil aka jared314", colors.white, colors.blue)

  draw_text_term(1, 3, "Searching for a peripherals...", colors.white, colors.black)
  sleep(1)

  reactor = reactorSearch()
  mon = monitorSearch()
  
  
  draw_text_term(2, 5, "Connecting to reactor...", colors.white, colors.black)
  sleep(0.5)
  if reactor == null then
      draw_text_term(1, 8, "Error:", colors.red, colors.black)
      draw_text_term(1, 9, "Could not connect to reactor", colors.red, colors.black)
      draw_text_term(1, 10, "Reactor must be connected with networking cable", colors.white, colors.black)
      draw_text_term(1, 11, "and modems or the computer is directly beside", colors.white, colors.black)
       draw_text_term(1, 12,"the reactors computer port.", colors.white, colors.black)
      draw_text_term(1, 14, "Press Enter to continue...", colors.gray, colors.black)
      wait = read()
      setup_wizard()
  else
      draw_text_term(27, 5, "success", colors.lime, colors.black)
      sleep(0.5)
  end
  
  draw_text_term(2, 6, "Connecting to monitor...", colors.white, colors.black)
  sleep(0.5)
  if mon == null then
      draw_text_term(1, 7, "Error:", colors.red, colors.black)
      draw_text_term(1, 8, "Could not connect to a monitor. Place a 3x3 advanced monitor", colors.red, colors.black)
      draw_text_term(1, 11, "Press Enter to continue...", colors.gray, colors.black)
      wait = read()
      setup_wizard()
  else
      monX, monY = mon.getSize()
      draw_text_term(27, 6, "success", colors.lime, colors.black)
      sleep(0.5)
  end
    draw_text_term(2, 7, "saving configuration...", colors.white, colors.black)  

    save_config()

    sleep(0.1)
    draw_text_term(1, 9, "Setup Complete!", colors.lime, colors.black) 
    sleep(1)

    auto = auto_string == "true"
    call_homepage() 

end
----------------SETUP-------------------------------

function setup_wizard()
  
  term.clear()

  
     draw_text_term(1, 1, "BigReactor Controls v"..version, colors.lime, colors.black)
     draw_text_term(1, 2, "Peripheral setup", colors.white, colors.black)
     draw_text_term(1, 4, "Step 1:", colors.lime, colors.black)
     draw_text_term(1, 5, "-Place 3x3 advanced monitors next to computer.", colors.white, colors.black)
     draw_text_term(1, 7, "Step 2:", colors.lime, colors.black)
     draw_text_term(1, 8, "-Place a wired modem on this computer and on the ", colors.white, colors.black)
     draw_text_term(1, 9, " computer port of the reactor.", colors.white, colors.black)
     draw_text_term(1, 10, "-connect modems with network cable.", colors.white, colors.black)
     draw_text_term(1, 11, "-right click modems to activate.", colors.white, colors.black)
     draw_text_term(1, 13, "Press Enter when ready...", colors.gray, colors.black)
    
     wait = read()
      test_configs()

   
end

-- peripheral searching thanks to /u/kla_sch
-- http://pastebin.com/gTEBHv3D
function reactorSearch()
   local names = peripheral.getNames()
   local i, name
   for i, name in pairs(names) do
      if peripheral.getType(name) == "BigReactors-Reactor" then
         return peripheral.wrap(name)
      else
         --return null
      end
   end
end

function monitorSearch()
   local names = peripheral.getNames()
   local i, name
   for i, name in pairs(names) do
      if peripheral.getType(name) == "monitor" then
        test = name
         return peripheral.wrap(name)
      else
         --return null
      end
   end
end

function start()
  --if configs exists, load values and test
  if fs.exists("config.txt") then
      load_config()
      test_configs()
  else
    setup_wizard()
  end
end

start()