local Class = require 'libs.class'

local gnomeImgs = {
  lg.newImage('images/gnome1.png'),
  lg.newImage('images/gnome2.png'),
  lg.newImage('images/gnome3.png'),
  lg.newImage('images/gnome4.png'),
  lg.newImage('images/gnome5.png'),
  lg.newImage('images/gnome6.png'),
  lg.newImage('images/gnome7.png'),
  lg.newImage('images/gnome8.png'),
  lg.newImage('images/gnome9.png'),
  lg.newImage('images/gnome10.png'),
  lg.newImage('images/gnome11.png'),
  lg.newImage('images/gnome12.png'),
  lg.newImage('images/gnome13.png'),
  lg.newImage('images/gnome14.png'),
  lg.newImage('images/gnome15.png'),
  lg.newImage('images/gnome16.png'),
  lg.newImage('images/gnome17.png'),
  lg.newImage('images/gnome18.png'),
  lg.newImage('images/gnome19.png'),
  lg.newImage('images/gnome20.png'),
  lg.newImage('images/gnome21.png'),
  lg.newImage('images/gnome22.png'),
  lg.newImage('images/gnome23.png'),
  lg.newImage('images/gnome24.png'),
  lg.newImage('images/gnome25.png'),
  lg.newImage('images/gnome26.png'),
  lg.newImage('images/gnome27.png'),
  lg.newImage('images/gnome30.png'),
  lg.newImage('images/gnome31.png'),
  lg.newImage('images/gnome32.png'),
  lg.newImage('images/gnome33.png'),
  lg.newImage('images/gnome34.png'),
  lg.newImage('images/gnome35.png'),
  lg.newImage('images/gnome37.png'),
  lg.newImage('images/gnome38.png'),
  lg.newImage('images/gnome39.png'),
  lg.newImage('images/gnome40.png'),
  lg.newImage('images/gnome41.png'),
  lg.newImage('images/gnome42.png'),
  lg.newImage('images/gnome43.png'),
  lg.newImage('images/gnome44.png'),
  lg.newImage('images/gnome45.png'),
  lg.newImage('images/gnome46.png'),
  lg.newImage('images/gnome47.png'),
  lg.newImage('images/gnome48.png'),
  lg.newImage('images/gnome49.png'),
  lg.newImage('images/gnome50.png'),
  lg.newImage('images/gnome51.png'),
  lg.newImage('images/gnome52.png'),
  lg.newImage('images/gnome53.png'),
  lg.newImage('images/gnome54.png'),
  lg.newImage('images/gnome55.png'),
  lg.newImage('images/gnome56.png'),
  lg.newImage('images/gnome57.png'),
  lg.newImage('images/gnome58.png'),
  lg.newImage('images/gnome59.png'),
  lg.newImage('images/gnome60.png'),
  lg.newImage('images/gnome61.png'),
  lg.newImage('images/gnome62.png'),
  lg.newImage('images/gnome63.png'),
  lg.newImage('images/gnome64.png'),
  lg.newImage('images/gnome65.png'),
  lg.newImage('images/gnome66.png'),
  lg.newImage('images/gnome67.png'),
  lg.newImage('images/gnome68.png'),
  lg.newImage('images/gnome69.png'),
  lg.newImage('images/gnome70.png'),
  lg.newImage('images/gnome71.png'),
  lg.newImage('images/gnome72.png'),
  lg.newImage('images/gnome73.png'),
  lg.newImage('images/gnome74.png'),
  lg.newImage('images/gnome76.png'),
  lg.newImage('images/gnome77.png'),
  lg.newImage('images/gnome78.png'),
  lg.newImage('images/gnome79.png'),
  lg.newImage('images/gnome80.png'),
  lg.newImage('images/gnome81.png'),
  lg.newImage('images/gnome82.png'),
  lg.newImage('images/gnome83.png'),
  lg.newImage('images/gnome84.png'),
  lg.newImage('images/gnome85.png'),
  lg.newImage('images/gnome86.png'),
  lg.newImage('images/gnome87.png'),
  lg.newImage('images/gnome88.png'),
  lg.newImage('images/gnome89.png'),
  lg.newImage('images/gnome91.png'),
  lg.newImage('images/gnome92.png'),
  lg.newImage('images/gnome93.png'),
  lg.newImage('images/gnome94.png'),
  lg.newImage('images/gnome95.png'),
  lg.newImage('images/gnome96.png'),
  lg.newImage('images/gnome97.png'),
  lg.newImage('images/gnome98.png'),
  lg.newImage('images/gnome99.png'),
  lg.newImage('images/gnome100.png'),
  lg.newImage('images/gnome101.png'),
  lg.newImage('images/gnome102.png'),
  lg.newImage('images/gnome103.png'),
  lg.newImage('images/gnome104.png'),
  lg.newImage('images/gnome105.png'),
  lg.newImage('images/gnome106.png'),
  lg.newImage('images/gnome107.png'),
  lg.newImage('images/gnome108.png'),
  lg.newImage('images/gnome109.png'),
  lg.newImage('images/gnome110.png'),
  lg.newImage('images/gnome111.png'),
  lg.newImage('images/gnome112.png'),
  lg.newImage('images/gnome113.png'),
  lg.newImage('images/gnome114.png'),
  lg.newImage('images/gnome115.png'),
  lg.newImage('images/gnome116.png'),
  lg.newImage('images/gnome117.png'),
  lg.newImage('images/gnome118.png'),
  lg.newImage('images/gnome119.png'),
  lg.newImage('images/gnome120.png'),
  lg.newImage('images/gnome121.png'),
  lg.newImage('images/gnome122.png'),
  lg.newImage('images/gnome123.png'),
  lg.newImage('images/gnome124.png'),
  lg.newImage('images/gnome125.png'),
  lg.newImage('images/gnome126.png'),
  lg.newImage('images/gnome127.png'),
  lg.newImage('images/gnome128.png'),
  lg.newImage('images/gnome129.png'),
  lg.newImage('images/gnome130.png')
}

local gnomeSelection = {}
local walkDir = {1,2,3,4,5,6,7,8,9}
local question = lg.newImage("images/huh.png")

function shufflePick(source,count)
  --copies an arbitrary number of unique values from a source to a new table
  --useful for when you need to randomly pick a bunch of unique things out (like sprites)
  --Thanks to @Zorg from the LOVE Discord
  
  --copy
  local destination = {}
  for i=1,#source do
    destination[i] = source[i]
  end
  
  --shuffle
  for i=#destination,2, -1 do
    local j = rand(1,i)
    destination[j],destination[i] = destination[i],destination[j]
  end
  
  --remove from copy until count is matched
  for i=#destination,1,-1 do
    if i > count then 
      table.remove(destination,i) 
    end
  end
  return destination
end


local Gnome = Class{
  init = function(self,x,y)
    self.id = nil
    self.img = nil
    self.x = x
    self.y = y
    self.walkDir = walkDir[rand(1,#walkDir)]
    self.walkTimer = rand(1,3)
    self.walkSpeed = rand(20,80)
    self.idleTimer = rand(0,3)
    self.clicked = false
    self.question = false
    self.questiontimer = 2
    self.found = false
  end
}

function Gnome:load(count)
  gnomeSelection = shufflePick(gnomeImgs,count)
  for _,self in ipairs(gnomes) do
    self.id = _
    self.img = gnomeSelection[_]
  end
  
end

function Gnome:update(dt)

    if self.walkTimer > 0 then
      self.walkTimer = self.walkTimer - 1 * dt
    end
    if self.walkTimer <= 0 then
      if self.idleTimer > 0 then
        self.idleTimer = self.idleTimer - 1 * dt
        self.walkDir = walkDir[9] --stand still
      end
      if self.idleTimer <= 0 then
        self.walkTimer = rand(1,3)
        self.walkDir = walkDir[rand(1,#walkDir)]
        self.idleTimer = rand(0,4)
      end
    end
    
    if self.walkDir == 1 then self.x = self.x + self.walkSpeed * dt
      elseif self.walkDir == 2 then self.x = self.x - self.walkSpeed * dt 
      elseif self.walkDir == 3 then self.y = self.y + self.walkSpeed * dt
      elseif self.walkDir == 4 then self.y = self.y - self.walkSpeed * dt
      elseif self.walkDir == 5 then self.x = self.x - self.walkSpeed * dt self.y = self.y - self.walkSpeed * dt
      elseif self.walkDir == 6 then self.x = self.x + self.walkSpeed * dt self.y = self.y + self.walkSpeed * dt
      elseif self.walkDir == 7 then self.x = self.x + self.walkSpeed * dt self.y = self.y - self.walkSpeed * dt
      elseif self.walkDir == 8 then self.x = self.x - self.walkSpeed * dt self.y = self.y + self.walkSpeed * dt
      elseif self.walkDir == 9 then self.x = self.x self.y = self.y
    end
    
    --wrapping
    if self.x-16.5 > stage.w then self.x = -16.5
      elseif self.x+16.5 < 0 then self.x = stage.w + 16.5
      elseif self.y-31 > stage.h then self.y = -31
      elseif self.y+31 < 0 then self.y = stage.h+31
    end
    
    if self.clicked then
      if self.id ~= 1 then
        self.question = true
        self.questiontimer = 1
        self.walkDir = 9
        local wronggnome = sounds.wronggnome[rand(1,3)]:clone()
        wronggnome:play()
        self.clicked = false
      else
        self.found = true
        self.walkDir = 9
        success = true --in stage.lua
        self.clicked = false
      end
    end
  
end

function Gnome:mousepressed(x,y)
  if CheckCollisionPoint(x,y,self.x-16.5,self.y-31,33,62) then
    self.clicked = true
    if self.id == 1 then
      local bell = sounds.bell:clone()
      bell:play()
      --sounds.goodclick:play()
    else
      local badclick = sounds.badclick:clone()
      badclick:play()
    end
  end
end

      

function Gnome:draw()
  --look left or right
  if self.walkDir == 1 or self.walkDir == 6 or self.walkDir == 7 then
    lg.draw(self.img,self.x,self.y,0,-1,1,16.5,31)
  else
    lg.draw(self.img,self.x,self.y,0,1,1,16.5,31)

  end
  if self.question then
    self.questiontimer = self.questiontimer -1 * love.timer.getDelta()
    if self.questiontimer > 0 then
      lg.draw(question,self.x+8,self.y-40,0.3)
    end
    
  end
  --lg.setColor(1,0,0)
  --lg.rectangle('fill',self.x,self.y,4,4)
  lg.setColor(1,1,1)
  
end

return Gnome