local Scene    = require "core.modules.scenes"
local MainMenu = Scene:extend()

stateDebugMenu = {}

local menus = require "scenes.debugmenu.menus"

local bannerAnim = 0

function MainMenu:new()
  MainMenu.super.new(self)

  self:createBackground("assets/backgrounds/back.png", "assets/sprites/gui/emblem.png")
  self.assets:addImageFont("small", "assets/fonts/small", 1)
  self.assets:addImageFont("medium", "assets/fonts/medium", 1)

  menus.Create(self)
  self.menusystem.menus["main"]:getFocus()

  self.assets:setMusic("assets/music/options.ogg")
  self.assets:playMusic()

  local screenwidth, screenheight = core.screen:getDimensions()

  self.banner = game.gui.newBanner("banner.png", "IMPERIUM PORCORUM")
  self.leftborder   = game.gui.newBorder(1, 64+24, screenwidth)
  self.rightborder  = game.gui.newBorder(0, 64+24, screenwidth)
  self:register()
  self.description  = "nothing to see here"
end

function MainMenu:createBackground( filename, emblem_filename )
  local screenwidth, screenheight = core.screen:getDimensions()
  local backAsset = love.graphics.newImage( filename )
  local Emblem    = love.graphics.newImage( emblem_filename )
  self.background = love.graphics.newCanvas(screenwidth, screenheight)
  love.graphics.setCanvas( self.background )
  local w, h = backAsset:getDimensions()

  for i=1, math.ceil(screenwidth / w) do
    for j=1,math.ceil(screenheight/ h) do
      love.graphics.draw(backAsset, (i-1)*w, (j-1)*h, 0, 1, 1)
    end
  end

  local w, h = Emblem:getDimensions()
  love.graphics.draw(Emblem, screenwidth*5/16, screenheight*10/16, 0, 1, 1, w/2, h/2)

  love.graphics.setCanvas( )
end

function MainMenu:selectMenu(menuID)
  self.menusystem:setAllMenuVisibility(false)
  self.menusystem:setAllMenuActivity(false)

  if (self.menusystem.menus[menuID] == nil) then
    error("ERROR, menu " .. menuID .. " doesn't exist in scene menusystem")
  end

  self.menusystem.menus["main"].isVisible = true

  self.menusystem.menus[menuID].isVisible = true
  self.menusystem.menus[menuID].isActive  = true

  self.menusystem.menus[menuID]:getFocus()
end

function MainMenu:refreshMenu()
  menuID = self.menusystem.focusedMenu
  menus.Create(self)

  self.menusystem:setAllMenuVisibility(false)
  self.menusystem:setAllMenuActivity(false)

  if (self.menusystem.menus[menuID] == nil) then
    error("ERROR, menu " .. menuID .. " doesn't exist in scene menusystem")
  end

  self.menusystem.menus[menuID].isVisible = true
  self.menusystem.menus[menuID].isActive  = true

  self.menusystem.menus[menuID]:getFocus()
end

function MainMenu:update(dt)
  MainMenu.super.update(self, dt)
  bannerAnim = (bannerAnim + (dt*32*1.5)) % 32
end

function MainMenu:draw()
  local screenwidth, screenheight = core.screen:getDimensions()
  local middlex, middley = screenwidth/2, screenheight/2
  utils.graphics.resetColor()

  love.graphics.draw(self.background)

  love.graphics.draw(self.leftborder, 0, -32+bannerAnim, 0, 1, 1)
  love.graphics.draw(self.rightborder, screenwidth, -bannerAnim, 0, -1, 1)

  love.graphics.draw(self.banner, middlex, 8, 0, 1, 1, 480/2)

  self.assets.fonts["small"]:set()
  self.assets.fonts["small"]:setLineHeight(1.2)
end

return MainMenu
