-- scenes/mainmenu :: the main menu of the different birb scenes

--[[
  Copyright © 2019 Kazhnuz

  Permission is hereby granted, free of charge, to any person obtaining a copy of
  this software and associated documentation files (the "Software"), to deal in
  the Software without restriction, including without limitation the rights to
  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  the Software, and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]

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

local ListBox   = require "birb.modules.menusystem.menus.listbox"
local Widget    = require "birb.modules.menusystem.menus.widgets"

local TextMenu = require "birb.modules.menusystem.textmenu"

local SceneWidget   = TextMenu.baseWidgets.Base:extend()
local ExitWidget    = TextMenu.baseWidgets.Base:extend()

local MENU_NAME = "mainMenu"

function MainMenu:start()
  self.assets:batchImport("scenes.mainmenu.assets")
  print(self.assets:getFont("medium"))
  local menu = self:addMenu()
  for i=1, 4 do
    local name = i .. "player"
    menu:addSubmenu(name, core.lang:translate("commons", name))
    if i == 1 then
      self:addScene(scenes.Plateformer,   "plateform", i)
    end
    self:addScene(scenes.MovePlayer,    "topdown", i)
    self:addScene(scenes.MovePlayer3D,  "topdown3D", i)
    self:addScene(scenes.Action3D,  "bigmap3D", i)
    if i > 1 then
      self:addScene(scenes.MovePlayer,    "topdown (zoom)", i, "zoom")
      self:addScene(scenes.MovePlayer3D,  "topdown3D (zoom)", i, "zoom")
    end
  end
  self:addSubMenu("menus", "menu")
  self:addScene(scenes.Inventory, "inventory")
  self:addScene(scenes.Options, "options")
  self:addScene(scenes.TestMenu, "tests")


  self.menusystem:setSoundFromSceneAssets("navigate")
  local menu = self.menusystem.menus[MENU_NAME]
  menu:switch("main")

  ExitWidget(self, "main")
  self.menusystem:switchMenu(MENU_NAME)
end

-- MENU FUNCTION
-- Functions that serve the handling of menus

function MainMenu:addMenu()
  local w = 424/2
  local x, y = w / 2, 24
  return TextMenu(MENU_NAME, "medium", x, y, w, 8, 0)
end

function MainMenu:addSubMenu(name, fullname)
  local menu = self.menusystem.menus[MENU_NAME]
  menu:addSubmenu(name, core.lang:translate("mainmenu", fullname))
end

function MainMenu:addScene(scene, fullname, arg1, arg2, arg3, arg4, arg5)
  local args = {arg1, arg2, arg3, arg4, arg5}
  SceneWidget(self, MENU_NAME, scene, fullname, args)
end

function MainMenu:draw()
  love.graphics.setColor(.3,  .1, .4, 1)
  love.graphics.rectangle("fill", 0, 0, 424, 240)
  utils.graphics.resetColor()
  love.graphics.print(math.floor(game.playtime), 8, 8)
end

-- WIDGETS
-- Widgets used by menus

-- Scene widget :: switch scene

function SceneWidget:new(scene, menu, newscene, fullname, args)
  self.scene  = scene
  self.args   = args
  self.newscene     = newscene
  local label       = core.lang:translate("mainmenu", fullname)
  SceneWidget.super.new(self, MENU_NAME, label)
end

function SceneWidget:action()
  core.scenemanager:storeCurrentScene("mainmenu")
  core.scenemanager:prepareTransition()
  self.newscene(self.args[1], self.args[2], self.args[3], self.args[4], self.args[5])
end

-- Exit Widget : exit the scenes

function ExitWidget:new(scene, menu)
  self.scene = scene
  local label       = core.lang:translate("commons", "exit")
  SceneWidget.super.new(self, MENU_NAME, label)
end

function ExitWidget:action()
  game:write()
  love.event.quit()
end

return MainMenu
