local component = require("component")
local computer = require("computer")
local event = require("event")
local gpu = component.gpu
local os = require("os")
local unicode = require("unicode")

function resetGPU(w, h)
	gpu.setResolution(w, h)
	gpu.setBackground(0, false)
	gpu.setForeground(16777215, false)
	gpu.fill(1, 1, w, h, " ")
end

active = 1
function onKeyDown(name, addr, char, key, player)
	active = 0
	return false
end
event.listen("key_down", onKeyDown)

resetGPU(160, 50)
local ticks = 0

function getColor(ticks, mul)
	local ctr = ((ticks & 127) / 128) * 6
	local x = math.floor(mul * (ctr % 1))
	local y = mul
	local z = math.floor(mul * (1 - (ctr % 1)))
	if ctr >= 5 then
		return (y << 16) | z
    end
	if ctr >= 4 then
		return (x << 16) | y
    end
	if ctr >= 3 then
		return (z << 8) | y
    end
	if ctr >= 2 then
		return (y << 8) | x
    end
	if ctr >= 1 then
		return (z << 16) | (y << 8)
    end
	return (y << 16) | (x << 8)
end

local data = "WE ARE ALL CONNECTED"
local greets = "receiving data...          greetings: sangar * greaser * dss"

function getColorL(ticks, mul)
	local bp = math.floor((ticks >> 4) / 7)
	if bp >= #data then
		return 0xFF
	end
	if (ticks % 16) >= 11 then
		local x = mul
		local datapos = ticks >> 4
		local bitpos = datapos % 7
		local bytepos = math.floor(datapos / 7)
		local v = string.byte(data, bytepos + 1, bytepos + 1) >> bitpos
		if (v & 1) == 0 then
			x = x >> 2
		end
		return (x << 16) | (x << 8) | x
	end
	return getColorR(ticks, mul)
end

local lastSleep = computer.uptime()
function sleep(time)
	local target = lastSleep + time
	while computer.uptime() < target do
		os.sleep(0.05)
	end
	lastSleep = target
end

gpu.setBackground(0x999999, false)
gpu.setForeground(0xCCCCCC, false)
gpu.fill(1, 43, 160, 1, unicode.char(0x2580))
gpu.setBackground(0x444444, false)
gpu.setForeground(0x676767, false)
gpu.fill(1, 44, 160, 1, unicode.char(0x2580))
gpu.setBackground(0x333333, false)
gpu.fill(1, 45, 160, 6, " ")

local obarx = 80
while active > 0 do
	local barsy = 1
	local barsh = 49
	gpu.setBackground(0x000000, false)
	gpu.fill(1, barsy, 160, barsh + 1, " ")
	-- gpu.setBackground(getColor(ticks, 0x80), false)
	-- gpu.setForeground(getColor(ticks, 0xFF), false)
	gpu.setBackground(0x664080, false)
	gpu.setForeground(0xCCB6FF, false)
    for bary = barsh,barsy,-2 do
		local barx = math.sin((ticks * (2.17845 + math.cos(ticks * 0.02844) * 0.5) + bary)
					* (0.037 + math.sin(ticks * 0.009283) * 0.05))
		barx = barx * (60 + math.cos(ticks * 0.07234) * 20) + 80
		barx = barx + (math.sin(ticks * 0.013) * math.cos(ticks * 0.011) * 20)
		local barw = 9
		local barh = barsh - bary + barsy + 1
		gpu.fill(barx - 2, bary, barw, barh, unicode.char(0x2591))
		gpu.fill(barx - 2 + (barw * 0.2), bary, barw * 0.6, barh, unicode.char(0x2593))
		gpu.fill(barx - 2 + (barw * 0.4), bary, barw * 0.2, barh, unicode.char(0x2588))
		barx = math.sin((ticks * (2.845 + math.sin(ticks * 0.07) * 0.5) + bary)
					* (0.017 + math.cos(ticks * 0.00532) * 0.05))
		barx = barx * (60 + math.sin(ticks * 0.12) * 20) + 80
		barx = barx + (math.sin(ticks * 0.013) * math.cos(ticks * 0.011) * 20)
		gpu.fill(barx - 2, bary, barw, barh, unicode.char(0x2591))
		gpu.fill(barx - 2 + (barw * 0.2), bary, barw * 0.6, barh, unicode.char(0x2593))
		gpu.fill(barx - 2 + (barw * 0.4), bary, barw * 0.2, barh, unicode.char(0x2588))
	end
	os.sleep(0.05)
	ticks = ticks + 1
end
resetGPU(80, 25)
