timezone = -8
clock = 24

alpha = Color.new(0, 0, 0, 150)
black = Color.new(0, 0, 0)
white = Color.new(256, 256, 256)
red = Color.new(256, 0, 0)
green = Color.new(0, 256, 0)
blue = Color.new(0, 0, 256)
showInfo = false
rSelect = true
gSelect = false
bSelect = false
forward = true
reverse = false
random = false
pause = false
r = 0
g = 0
b = 0
x0 = 0
y0 = 0

courierb = Font.load("courierb.ttf")
courierb:setPixelSizes(0, 12)

timeWindow = Image.createEmpty(200, 12)
rgbWindow = Image.createEmpty(215, 224)
battWindow = Image.createEmpty(140, 12)

function drawRect(where, x, y, width, height, color)
	where:drawLine(x, y, x + width, y, color)
	where:drawLine(x + width, y, x + width, y + height, color)
	where:drawLine(x + width, y + height, x, y + height, color)
	where:drawLine(x, y + height, x, y, color)
end

function getTime()
	dateString = os.date("%a, %B %d, %Y")
	timeValue = os.date("*t")
	hh = timeValue.hour
	mm = timeValue.min
	ss = timeValue.sec
	if clock == 12 then
		if hh > 12 then
			hh = hh + timezone - 12
			dt = "AM"
		else
			hh = hh + timezone
			dt = "PM"
		end

		if hh < 10 then hh = "0" .. hh end
		if mm < 10 then mm = "0" .. mm end
		if ss < 10 then ss = "0" .. ss end

		timeString = tostring(dateString .. " " .. hh .. ":" .. mm .. " " .. dt)
	else
		if hh > 12 then hh = hh + timezone + 12 else hh = hh + timezone end
		if hh > 24 then hh = hh - 24 end

		if hh < 10 then hh = "0" .. hh end
		if mm < 10 then mm = "0" .. mm end
		if ss < 10 then ss = "0" .. ss end

		timeString = tostring(dateString .. " " .. hh .. ":" .. mm .. ":" .. ss)
	end
	return tostring(timeString)
end

math.randomseed(os.time())
math.random()
math.random()
math.random()

while true do
	pad = Controls.read()

	if pad:start() and pad ~= oldpad then
		if pause then pause = false else pause = true end
	end

	if pause then
		r = r
		if pad:select() and pad~= oldpad then
			strFilename = "colorPlayR" .. r .. "G" .. g .. "B" .. b .. ".png"
			screen:save(strFilename)
		end
	elseif forward then
		r = r + 1
		if pad:select() and pad ~= oldpad then
			reverse = true
			forward = false
			random = false
		end
	elseif reverse then
		r = r - 1
		if pad:select() and pad ~= oldpad then
			random = true
			forward = false
			reverse = false
		end
	elseif random then
		r = math.random(0, 256)
		g = math.random(0, 256)
		b = math.random(0, 256)
		if pad:select() and pad ~= oldpad then
			forward = true
			reverse = false
			random = false
		end
	end

	if pad:right() then
		if rSelect then
			if reverse then r = r + 2 else r = r + 1 end
		elseif gSelect then
			g = g + 1
		elseif bSelect then
			b = b + 1
		end
	end

	if pad:left() then
		if rSelect then
			if forward then r = r - 2 else r = r - 1 end
		elseif gSelect then
			g = g - 1
		elseif bSelect then
			b = b - 1
		end
	end

	if pad:up() and pad ~= oldpad then
		if rSelect then
			bSelect = true
			rSelect = false
		elseif gSelect then
			rSelect = true
			gSelect = false
		elseif bSelect then
			gSelect = true
			bSelect = false
		end
	end

	if pad:down() and pad ~= oldpad then
		if rSelect then
			gSelect = true
			rSelect = false
		elseif gSelect then
			bSelect = true
			gSelect = false
		elseif bSelect then
			rSelect = true
			bSelect = false
		end
	end

	if r > 256 then
		r = 0
		g = g + 1
	elseif g > 256 then
		g = 0
		b = b + 1
	elseif b > 256 then
		b = 0
		r = r + 1
	end

	if r < 0 then
		r = 256
		g = g - 1
	elseif g < 0 then
		g = 256
		b = b - 1
	elseif b < 0 then
		b = 256
		r = r - 1
	end

	if pad:l() then
		r = 0
		g = 0
		b = 0
	end

	if pad:r() then
		r = 256
		g = 256
		b = 256
	end

	if pad:square() then
		r = 256
		g = 0
		b = 0
	end

	if pad:triangle() then
		r = 0
		g = 256
		b = 0
	end

	if pad:circle() then
		r = 0
		g = 0
		b = 256
	end

	if pad:cross() and pad ~= oldpad then
		if showInfo then showInfo = false else showInfo = true end
	end

	newColor = Color.new(r, g, b)
	screen:clear(newColor)

	if showInfo then
		drawRect(timeWindow, 0, 0, 200, 12, black)
		timeWindow:fillRect(0, 0, 200, 12, alpha)
		timeWindow:fontPrint(courierb, 2, 9, getTime(), white)

		drawRect(rgbWindow, 0, 0, 215, 224, black)
		rgbWindow:fillRect(0, 0, 215, 224, alpha)
		if rSelect then rgbWindow:fontPrint(courierb, 2, 9, "R:" .. r, green) else rgbWindow:fontPrint(courierb, 2, 9, "R:" .. r, white) end
		if gSelect then rgbWindow:fontPrint(courierb, 2, 20, "G:" .. g, green) else rgbWindow:fontPrint(courierb, 2, 20, "G:" .. g, white) end
		if bSelect then rgbWindow:fontPrint(courierb, 2, 31, "B:" .. b, green) else rgbWindow:fontPrint(courierb, 2, 31, "B:" .. b, white) end
		rgbWindow:fontPrint(courierb, 2, 53, "--------Controls--------", white)
		rgbWindow:fontPrint(courierb, 2, 64, "Cross: Show/Hide Information", white)
		rgbWindow:fontPrint(courierb, 2, 86, "L: Full Black", white)
		rgbWindow:fontPrint(courierb, 2, 97, "R: Full White", white)
		rgbWindow:fontPrint(courierb, 2, 119, "Square: Full Red", white)
		rgbWindow:fontPrint(courierb, 2, 130, "Triangle: Full Green", white)
		rgbWindow:fontPrint(courierb, 2, 141, "Circle: Full Blue", white)
		rgbWindow:fontPrint(courierb, 2, 163, "Up/Down: Select Color", white)
		rgbWindow:fontPrint(courierb, 2, 174, "Left/Right: Adjust Color +/- 1", white)
		rgbWindow:fontPrint(courierb, 2, 196, "Select: Forward/Reverse/Random", white)
		rgbWindow:fontPrint(courierb, 2, 207, "Select: (Paused) Screenshot", white)
		rgbWindow:fontPrint(courierb, 2, 218, "Start: Pause/Unpause", white)

		drawRect(battWindow, 0, 0, 140, 12, black)
		battWindow:fillRect(0, 0, 140, 12, alpha)
		battWindow:fontPrint(courierb, 2, 9, "Battery Level: " .. System.powerGetBatteryLifePercent() .. "%", white)

		screen:blit(145, 10, timeWindow)
		screen:blit(138, 24, rgbWindow)
		screen:blit(173, 250, battWindow)
	else
		aX = pad:analogX()
		aY = pad:analogY()

		if math.abs(aX) > 32 or math.abs(aY) > 32 then
			x0 = x0 + aX / 30
			y0 = y0 + aY / 30
		end

		if x0 < 0 then x0 = 0 end
		if x0 >= 479 then x0 = 479 end
		if y0 < 0 then y0 = 0 end
		if y0 >= 271 then y0 = 271 end

		screen:drawLine(x0 - 5, y0, x0 + 5, y0, red)
		screen:drawLine(x0, y0 - 5, x0, y0 + 5, red)
	end

	oldpad = pad
	screen.flip()
	screen.waitVblankStart()
end