package net.torvald.terrarum.modulecomputers.virtualcomputer.peripheral

import org.luaj.vm2.Globals
import net.torvald.terrarum.modulecomputers.virtualcomputer.computer.TerrarumComputer
import org.luaj.vm2.LuaTable
import org.luaj.vm2.LuaValue
import org.luaj.vm2.lib.OneArgFunction
import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.URL

/**
 * Provides internet access.
 *
 * Created by minjaesong on 2016-09-24.
 */
internal class PeripheralInternet(val host: net.torvald.terrarum.modulecomputers.virtualcomputer.computer.TerrarumComputer)
: net.torvald.terrarum.modulecomputers.virtualcomputer.peripheral.Peripheral("internet"){

    override val memSize = 1024

    override fun loadLib(globals: Globals) {
        globals["internet"] = LuaTable()
        globals["internet"]["fetch"] = FetchWebPage()
    }

    class FetchWebPage() : OneArgFunction() {
        override fun call(urlstr: LuaValue): LuaValue {
            val url = URL(urlstr.checkjstring())
            val inputstream = BufferedReader(InputStreamReader(url.openStream()))

            var inline = ""
            var readline = inputstream.readLine()
            while (readline != null) {
                inline += readline
                readline = inputstream.readLine()
            }
            inputstream.close()

            return LuaValue.valueOf(inline)
        }
    }

}