-- for love 12.0
-- on iphone (landscape), screen seems to be 830x368 (hard to touch corners x:30px y20px)
-- iphone landscape, camera island 60px, bottom swipe bar 40px
-- iPad 10 Gen desktop: 1180 x 820, 147 chars wide (1176px) by 51 char height (816px)
-- iPad safe area: 1024 x 768, 128 chars by 48 chars
-- draw to a 1024 x 768 canvas, then draw the canvas centered on desktop
-- need this for canvas - love.graphics.setBlendMode("alpha", "premultiplied")
-- crtmonitor background is 1376 x 1032, screen edge offsetX = - 176, Y = -132
-- scale the font in factors of 8 (height)
-- mode 1 = 4x8, mode 2 (default for mouse/pencil) = 8x16
-- mode 3 (default for touch) = 12x24, mode 4 = 16x32
-- mode 1 = 256 chars x 96 chars, mode 2 = 128 chars by 48 chars
-- mode 3 = 85 chars x 32 chars, mode 4 = 64 chars by 24 chars


	-- constants
	WIDTH_DESKTOP, HEIGHT_DESKTOP = love.window.getDesktopDimensions(1)
	WIDTH_WINDOW, HEIGHT_WINDOW = love.graphics.getDimensions()
	FONT = "Mx437_IBM_VGA_8x16.ttf"
	FONT_HEIGHT = 8 -- smallest size (height) of truetype font
	FONT_WIDTH = FONT_HEIGHT/2
	if WIDTH_DESKTOP > 1024 then
		SCREEN_OFFSETX = math.floor((WIDTH_DESKTOP-1024)/2)
	else
		SCREEN_OFFSETX = 0
	end
	if HEIGHT_DESKTOP > 768 then
		SCREEN_OFFSETY = math.floor((HEIGHT_DESKTOP-768)/2)
	else
		SCREEN_OFFSETY = 0
	end
	BGOFFSETX = -176+SCREEN_OFFSETX --{ offset position of CRT Monitor 
	BGOFFSETY = -132+SCREEN_OFFSETY --{ 

	-- variables
	local mouse = {}
	mouse.x = 0
	mouse.y = 0
	mouse.button = 0
	mouse.clicks = 0
	local keypressed = {}
	keypressedHistory = ""	
	local holdToQuit = 0
	local game = {}
	game.selected = {
		["fontSize"] = 3,
	}

	-- fonts codepage 437
	local monoFont = {
		[1] = love.graphics.newFont(FONT, FONT_HEIGHT),
		[2] = love.graphics.newFont(FONT, FONT_HEIGHT*2),
		[3] = love.graphics.newFont(FONT, FONT_HEIGHT*3),
		[4] = love.graphics.newFont(FONT, FONT_HEIGHT*4),
	}

	love.graphics.setFont( monoFont[3] ) -- set initial font

	-- graphics
	local canvas = love.graphics.newCanvas(1024, 768) -- make a new canvas

	-- flags
	local mouseDetected = false


function love.load()

end -- love.load


function love.keypressed(key, scancode, isrepeat)
	-- trap 'escape' unicode 27 to quit
	if scancode == "escape" then
		love.event.quit()
	end

	-- detect 1,2,3,4 for fontSize changing
		if scancode == "1" then
		game.selected["fontSize"] = 1
	end
	if scancode == "2" then
		game.selected["fontSize"] = 2
	end
	if scancode == "3" then
		game.selected["fontSize"] = 3
	end
	if scancode == "4" then
		game.selected["fontSize"] = 4
	end

end

function love.keyreleased(key, scancode)
	-- reset holdToQuit
	holdToQuit = 0
end

-- love.mousepressed( x, y, button, istouch, presses )
-- number presses
-- The number of presses in a short time frame and small area, used to simulate double, triple clicks
function love.mousepressed( x, y, button, istouch, presses )
	-- mouse button presses detected
	mouse.button = button
	mouse.clicks = presses
end
function love.mousemoved( x, y, dx, dy, istouch )
	-- mouse movement detected
	mouseDetected = true
	mouse.x = love.mouse.getX() - SCREEN_OFFSETX
	mouse.y = love.mouse.getY() - SCREEN_OFFSETY
end
-- love.touchpressed( id, x, y, dx, dy, pressure )
-- number pressure
-- The amount of pressure being applied. Most touch screens aren't pressure sensitive, in which case the pressure will be 1.
function love.mousereleased( x, y, button, istouch, presses )
	-- reset holdToQuit
	holdToQuit = 0
end

function love.touchpressed( id, x, y, dx, dy, pressure )
	-- detect touches
end

function love.touchreleased( id, x, y, dx, dy, pressure )
	-- detect touch releases
end

function love.draw()
	-- set font before draw text
  love.graphics.setFont(monoFont[game.selected["fontSize"]])

	love.graphics.setCanvas(canvas) -- select the 1024 x 768 canvas
  love.graphics.clear(0, 0, 0, 0) -- clear the canvas
	love.graphics.setColor(0,0,0,1) -- black color
	love.graphics.rectangle("fill",0,0,1024,768) -- draw black background
	love.graphics.setColor(1,1,1,1) -- white color 
	
		-- draw screen rulers
	love.graphics.print("0....,....1....,....2....,....3....,....4....,....5....,....6....,....7....,....8....,....9....,....0....,....1....,....2....,....",0,0)
	love.graphics.print("0\n.\n.\n.\n.\n-\n.\n.\n.\n.\n1\n.\n.\n.\n.\n-\n.\n.\n.\n.\n2\n.\n.\n.\n.\n-\n.\n.\n.\n.\n3\n.\n.\n.\n.\n-\n.\n.\n.\n.\n4\n.\n.\n.\n.\n-\n.\n.\n.\n.\n",0,0)

	   
  love.graphics.print("--[ ESC to quit ]--",FONT_WIDTH*((64-20)/2),FONT_HEIGHT*30)

  -- settings for canvas
  love.graphics.setCanvas() -- switch back to default screen canvas
  love.graphics.setColor(0.929,0.878,0.78,1) -- dark blue
  love.graphics.rectangle("fill",0,0,WIDTH_DESKTOP,HEIGHT_DESKTOP)
  love.graphics.setColor(1,1,1,1)
--  love.graphics.draw(crtmonitor, BGOFFSETX, BGOFFSETY)
--  love.graphics.setBlendMode("alpha", "premultiplied") -- needed for canvas
  
  -- draw ANSI borders around 1024x768 canvas
  -- draw extended black border first
	love.graphics.setColor(0, 0, 0, 1)
  love.graphics.rectangle("fill",SCREEN_OFFSETX+(FONT_WIDTH*-1),SCREEN_OFFSETY+(FONT_HEIGHT*-1),1024+(FONT_WIDTH*2),768+(FONT_HEIGHT*2))

  for i = -2,129 do
  	love.graphics.setColor(0.568, 0.529, 0.454, 1)
		love.graphics.print("▀",SCREEN_OFFSETX+(FONT_WIDTH*i),SCREEN_OFFSETY+(FONT_HEIGHT*-1))
  	love.graphics.setColor(0.868, 0.829, 0.754, 1)
		love.graphics.print("▄",SCREEN_OFFSETX+(FONT_WIDTH*i),SCREEN_OFFSETY+(FONT_HEIGHT*48))	
  end
	for i =  -1,48 do
  love.graphics.setColor(0.568, 0.529, 0.454, 1)
	love.graphics.print("▒ ",SCREEN_OFFSETX+(FONT_WIDTH*-2),SCREEN_OFFSETY+(FONT_HEIGHT*i))	
	love.graphics.print(" ▒",SCREEN_OFFSETX+(FONT_WIDTH*128),SCREEN_OFFSETY+(FONT_HEIGHT*i))	
	end

  -- draw the 1024x768 canvas in the middle
  love.graphics.setColor(1, 1, 1, 1)
--	love.graphics.print("░",SCREEN_OFFSETX+(FONT_WIDTH*-1),SCREEN_OFFSETY+(FONT_HEIGHT*-1))	

  love.graphics.draw(canvas, SCREEN_OFFSETX, SCREEN_OFFSETY)

end



function love.update(dt)

-- joystickcount = love.joystick.getJoystickCount( ) -- number
-- direction = Joystick:getHat( hat ) -- constant: c d l ld lu r rd ru u

-- x, y = love.touch.getPosition( id )
-- pressure = love.touch.getPressure( id )

	-- for holdToQuit feature
	if love.mouse.isDown( 1, 2, 3) then
		holdToQuit = holdToQuit + dt
	end
	if love.touch.getTouches() ~= nil then
		if #love.touch.getTouches() >=1 then
			holdToQuit = holdToQuit + dt
		end
	end

	if holdToQuit >= 3 then
		love.event.quit()
	end

end