Unnamed

os.loadAPI("./LuaUtils.lua")
os.loadAPI("./button.lua")
print("Util loaded")
local ws,err = http.websocket("ws://b64af8815a46.ngrok.io")
local mon = peripheral.wrap("top")
print("Websocket and monitor cakked")
mon.clear()
button.setMonitor(mon)
print("monitor cleared")

local function resultMenu(user,coin)
    print("Coin Data:")
    print("User: "..data[1].."/ Coins: "..data[2])
    mon.clear()
    mon.setCursorPos(1,1)
    mon.write("User: "..data[1])
    mon.setCursorPos(1,2)
    mon.write("Coins: "..data[2])
    disk.setLabel("bottom","Credit Card - "..data[2].."$")
    local exitButton = button.create("Exit")
    local depositButton = button.create("Deposit")
    exitButton.setPos(1,3)
    depositButton.setPos(1,4)
    exitButton.onClick(function() 
        turtle.suckDown()
        turtle.drop()
        mon.clear()
        mon.setCursorPos(1,1)
        mon.write("Thank you for using us!")
        sleep(4)
        mon.clear()
    end)
    depositButton.onClick(function()
        returnMenu(user,coin)
    end)
    button.await(exitButton)
    else
        turtle.suckDown()
        turtle.drop()
        sleep(4)
    end
end

local function returnMenu(user,coin)
    resultMenu(user,coin)
end

if ws then
    while true do
        mon.setCursorPos(1,1)
        mon.write("Please drop your card to the turtle")
        local succ = turtle.suck()
        if succ then
            turtle.dropDown()
            if fs.exists("disk/carddata.txt") then
                local file = fs.open("disk/carddata.txt", "r")
                local fileData = file.readAll()
                ws.send("@coin/"..fileData)
                local message,binary = ws.receive()
                local data = string.split(message,",")
                resultMenu(data[1],data[2])
        end
    end
    
    ws.close()
end