stage = {w = FrameWidth, h = FrameHeight-80}
infobar = {x = 0, y = stage.h, w = stage.w,h = 80}
gnomes = {}
scenes = {
  lg.newImage('images/bg2.png')
}

success = false --global boolean controled in gnome.lua
gameover = false

--TODO
--make win, linux, macOS builds, try web build
--FUTURE
--procedurally generated and animated backgrounds (like Jelly Juggle)
--procedurally generated gnomes
--More complex walking for gnomes
--Animated question mark
--options menu(music, sfx volume, timer on/off)
--camera with big worlds
--game modes

local Gnome = require 'gnome'
local gnome
local gnomeCount = 10
local font = lg.newFont("fonts/SukuriburuBySoultyRagevin-Regular.otf",64)
local plussign = lg.newImage("images/plus.png")
local level = 1
local bg = {}
local rects = {}
local circles = {}
local drawType = {'fill','line'}
local Timer = 60
local timercolor = {1,1,1}
local scored = false
local score = {
  img = lg.newImage('images/clock.png'),
  pointsImg = lg.newImage('images/pointsIcon.png'),
  pointsImg2 = lg.newImage('images/pointsIcon-large.png'),
  points = 0,
  x = 0,
  y = 0,
  timersx = 1,
  timersw = 1,
  timer = 0,
  timersin = 2,
  timersize = 1,
  timebonus = 11
}
local newtimebonus = 0

function brighten(color)
  local newColor = {
    color[1]+.3,
    color[2]+.3,
    color[2]+.3
    }
  return color
end

function randomRect()
  local size = rand(10,100)
  local rect = {x=rand(0,FrameWidth),y=rand(0,520),w=size,h=size,drawType=drawType[rand(1,2)]}
  return rect
end

function randomCircle()
  local rad = rand(5,50)
  local circle = {x=rand(0,FrameWidth),y=rand(0,520),rad = rad,drawType=drawType[rand(1,2)]}
  return circle
end

function love.keypressed(key)
  if key == 'space' then
    start = false
    sounds.tick:setVolume(.5)
  elseif key == 'r' and not start and not gameover then
    gnomeCount = 10
    timercolor = {1,1,1}
    gnomes = {}
    rects = {}
    circles = {}
    Timer = 60
    score.points = 0
    score.timebonus = 11
    stage:load()
    sounds.tick:setVolume(.5)
  elseif key == 'r' and not start and gameover then
    gnomeCount = 10
    start = true
    timercolor = {1,1,1}
    gnomes = {}
    rects = {}
    circles = {}
    Timer = 60
    score.points = 0
    score.timebonus = 11
    stage:load()
    sounds.tick:setVolume(0)
    --love.event.quit("restart")
  end
end


function stage:load()
  score.timer = 0
  score.timersin = 0
  gameover = false
  sounds.music:play()
  sounds.music:setLooping(true)
  sounds.music:setVolume(.15)
  sounds.tick:play()
  sounds.tick:setVolume(0)
  sounds.tick:setLooping(true)
  --Timer = 60
  local color1 = {rand(),rand(),rand()}
  local color2 = brighten(color1)
  bg = {
    img = scenes[rand(1,#scenes)],
    color = color1,
      --rect = {x=200,y=130,w=400,h=260,color=color2}
  }
  for i=1,rand(10,30) do
    table.insert(rects,randomRect())
    table.insert(circles,randomCircle())
  end
  
  for i=1,gnomeCount do
    local x,y = rand(32,FrameWidth-32),rand(32,FrameHeight-32)
    gnome = Gnome(x,y,gnomeCount)
    table.insert(gnomes,gnome)
  end
  
  gnome:load(gnomeCount)
end

function stage:update(dt)
       -- print(love.audio.getActiveSourceCount( ))
  if not start then
    if math.floor(Timer) > 0 then
      Timer = Timer - 1 * dt
      if math.floor(score.timebonus) > 0 then
        score.timebonus = score.timebonus - 1 * dt
      end
    end
  end
  
  --print(score.timebonus)
  --from Plant Party!
  if not start then
    score.timer = score.timer + dt
    score.timersin = score.timersin + dt*2/Timer
    --score.timersize = score.timersize + dt/timer
    score.timersx = score.timersize + 0.2 * math.sin(score.timersin*score.timer)
    score.timersy = score.timersx
  end
  
  
  for _,gnome in ipairs(gnomes) do
    gnome:update(dt)
  end
  
  if success then
    scored = true
    score.points = score.points + 1
    newtimebonus = score.timebonus
    if math.floor(newtimebonus) > 0 then
      local cuckoo = sounds.cuckoo:clone()
      cuckoo:play()
    end
    Timer = Timer+newtimebonus
    score.x,score.y = gnomes[1].x,gnomes[1].y
    score.timebonus = 11
    gnomes = {}
    rects = {}
    circles = {}
    if gnomeCount < 118 then
      gnomeCount = gnomeCount+10
    end
    self:load()
    success = false
  end
  
  if math.floor(Timer) <= 0 then
    gameover = true
    sounds.stop:setVolume(.1)
    sounds.stop:play()
  end
  if gameover then
    Timer = 60
    sounds.music:setVolume(0)
    sounds.tick:setVolume(0)
    for g,gnome in ipairs(gnomes) do
      gnome.walkDir = 9
    end
  end
  
  
end

function love.mousepressed(x,y)
  if not start and not gameover then
    for _, gnome in ipairs(gnomes) do
      gnome:mousepressed(x,y)
    end
  end

end


function stage:draw()
  lg.setFont(font)
  lg.setColor(bg.color)
  lg.draw(bg.img,0,0) --bg/scene
  for r,rect in ipairs(rects) do
    lg.rectangle(rect.drawType,rect.x,rect.y,rect.w,rect.h)
  end
  for c,circle in ipairs(circles) do
    lg.circle(circle.drawType,circle.x,circle.y,circle.rad)
  end
  
  --lg.setColor(bg.rect.color)
  --lg.rectangle('fill',bg.rect.x,bg.rect.y,bg.rect.w,bg.rect.h)
  lg.setColor(1,1,1,1)
  for _,gnome in ipairs(gnomes) do
    gnome:draw()
  end
  if not start then
    lg.setColor(0.1,0.1,0.1,1)
    lg.rectangle('fill',infobar.x,infobar.y,infobar.w,infobar.h)
    lg.setColor(1,1,1,1)
    lg.printf("Find the missing gnomes!",infobar.x+60,infobar.y+35,600,"center",0,0.5,0.5)
    
    --traget gnome infobar element
    lg.setColor(1,1,1,1)
    lg.draw(gnomes[1].img,infobar.x+20,infobar.y+10)
    
    local timerX,timerY = infobar.x+FrameWidth-80,infobar.y+18
    if scored then
      local angle = math.atan2(score.y-timerY,score.x-timerX)
      score.x = score.x - (500 * love.timer.getDelta() * math.cos(angle))
      score.y = score.y - (500 * love.timer.getDelta() * math.sin(angle))
      if math.floor(newtimebonus) > 0 then timercolor = {.9,.8,.1,1} end
      if math.abs(score.y-timerY) > 10 and math.abs(score.x-timerX) > 10 then
        if math.floor(newtimebonus) > 0 then
          lg.setColor(1,1,1,0.7)
          lg.draw(score.img,score.x,score.y)
          lg.printf(tostring(math.floor(newtimebonus)),score.x+50,score.y,100,"center",0,1.5,1.5)
          lg.setColor(timercolor)
          lg.draw(plussign,timerX-75,timerY+10)
          lg.printf(tostring(math.floor(newtimebonus)),timerX-80,timerY+5,100,"center",0,.8,.8)
        end
      else
        scored = false
        timercolor = {1,1,1}
      end
    end
    
      --Timer infobar element
    local timerstring = tostring(math.floor(Timer))
    --lg.printf(tostring(math.floor(Timer)),infobar.x+FrameWidth-80,infobar.y+20,100,"center",0,)
    lg.setColor(timercolor)
    if math.floor(Timer) > 10 then
      lg.printf(timerstring,timerX,timerY,100,"center",0,score.timersx,score.timersy,32)
      --lg.setColor(.5,.8,1,1)
      --lg.printf(timerstring,timerX,timerY,100,"center",0,score.timersx,score.timersy,32)
    else
      timercolor = {1,0,0}
      lg.printf(timerstring,timerX,timerY,100,"center",0,score.timersx,score.timersy,32)
      --lg.setColor(1,0,0,1)
      --lg.printf(timerstring,timerX,timerY,100,"center",0,score.timersx,score.timersy,32)
    end
    
    lg.setColor(1,1,1)
    lg.draw(score.pointsImg,infobar.x+((FrameWidth/2)-score.pointsImg:getWidth()/2),infobar.y+20)
    lg.printf("x",infobar.x+((FrameWidth/2)-score.pointsImg:getWidth()/2)+24,infobar.y+28,32,"center",0,.5,.5)
    lg.printf(tostring(score.points),infobar.x+((FrameWidth/2)-score.pointsImg:getWidth()/2)+28,infobar.y+30,100,"center",0,.5,.5)
    
    --print(scored,newtimebonus)
  else
    --start screen stuff
    lg.setColor(0.1,0.1,0.1,1)
    lg.rectangle('fill',infobar.x,infobar.y-50,infobar.w,infobar.h+100)
    lg.setColor(0,0,0)
    
    lg.printf("Gnome",4,304,300,"center",-.45,2,2)
    lg.printf("Or",124,364,300,"center",-.50,2,2)
    lg.printf("Trouble!",204,404,300,"center",-.50,2,2)
    lg.setColor(1,1,1)
    lg.printf("Gnome",0,300,300,"center",-.45,2,2)
    lg.printf("Or",120,360,300,"center",-.50,2,2)
    lg.printf("Trouble!",200,404,300,"center",-.50,2,2)
    lg.printf("When you find the pictured Gnome, click on them.",infobar.x+20,infobar.y-35,1500,"center",0,0.5,0.5)
    lg.printf("Find as many Gnomes as you can in the time limit.",infobar.x+20,infobar.y,1500,"center",0,0.5,0.5)
    lg.printf("Press space to begin!  Press R to restart at anytime.",infobar.x+20,infobar.y+35,1500,"center",0,0.5,0.5)
  end
  
  if gameover then
    lg.setColor(.1,.1,.1,0.95)
    timercolor = {1,1,1,0}
    lg.rectangle('fill',0,0,FrameWidth,FrameHeight)
    lg.setColor(1,1,1,1)
    lg.printf("Gnomes you found",FrameWidth/2-300,FrameHeight/2-32,600,"center",0,.7,.7)
    lg.setColor(1,1,1,1)
    lg.draw(score.pointsImg2,FrameWidth/2+60,FrameHeight/2-55,0,1,1)
    lg.printf("X",FrameWidth/2+110,FrameHeight/2-30,32,"center",0,.5,.5)
    lg.printf(tostring(score.points),FrameWidth/2+130,FrameHeight/2-42,100,"center",0,1,1)
    lg.printf("Press R to try again!",FrameWidth/2-150,FrameHeight/2+62,600,"center",0,.5,.5)

  end
  --print(score.timer,score.timersin,score.timersize)
end
