local fancy = require "game.modules.menus.fancy"

local widgets = {}

widgets.BattleWidget    = fancy.BaseWidget:extend()
widgets.ActionWidget    = widgets.BattleWidget:extend()
widgets.SubMenuWidget   = widgets.BattleWidget:extend()
widgets.BackMenuWidget  = widgets.BattleWidget:extend()
widgets.SkillWidget     = widgets.BattleWidget:extend()
widgets.ItemWidget      = widgets.BattleWidget:extend()

-- WIDGETS
-- All widgets used by the Characters menus

-- Basic Battle Widget
-- The base used by all battle widgets

function widgets.BattleWidget:new(character, menu_name, label1, label2)
  local label1 = label1 or ""
  local label2 = label2 or ""

  self.character = character
  local scene = self.character.turnSystem.scene

  widgets.BattleWidget.super.new(self, scene, menu_name, label1, label2)
  self.assets = self.scene.assets
end

-- Internal functions

function widgets.BattleWidget:selectAction()
  -- Rien de base, à voir ensuite comment je gère
end

function widgets.BattleWidget:action()
  self.scene:flushKeys()
  self.scene.menusystem:deactivate()

  self:sendCharacterData()
end

-- External functions

function widgets.BattleWidget:sendCharacterData()
  self.character:doNothing()
end

-- ActionWidget
-- The basic action widget

function widgets.ActionWidget:new(character, menu_name, action, label)
  self.actionType = action or ""
  local label = label or self.actionType
  widgets.ActionWidget.super.new(self, character, menu_name, label, "")
end

function widgets.ActionWidget:sendCharacterData()
  self.character:doBasicAction(self.actionType)
end

-- SubMenuWidget
-- A simple widget to change menu

function widgets.SubMenuWidget:new(character, menu_name, label, newmenu)
  local label2 = ""
  self.sfx = "mBack"
  if label ~= "back" then
    label2 = ">"
    self.sfx = "mBeep"
  end
  widgets.SubMenuWidget.super.new(self, character, menu_name, label, label2)
  self.newmenu = newmenu or "BaseMenu"
end

function widgets.SubMenuWidget:action()
  self.assets.sfx[self.sfx]:play()
  self.scene.menusystem:switchMenu(self.newmenu)
end

-- BackMenuWidget
-- Quit the menu

function widgets.BackMenuWidget:new(character, menu_name)
  widgets.BackMenuWidget.super.new(self, character, menu_name, "back", "")
end

function widgets.BackMenuWidget:sendCharacterData()
  self.assets.sfx["mBack"]:play()
  self.character:receiveBackSignal()
end

-- SkillWidget
-- A widget to handle skills

function widgets.SkillWidget:new(character, menu_name, skill)
  self.skillname = skill
  local label2 = "00"

  self.skilldata = game.skills:getSkillData(skill)

  if self.skilldata ~= nil then
    label2 = self.skilldata.cost or 0
    if label2 < 10 then
      label2 = "0" .. label2
    end
  end
  local color = {1, 1, 1}
  if (self.skilldata.cost > character.abstract.pp) then
    color = {1, 0.3, 0.3}
  end

  widgets.SkillWidget.super.new(self, character, menu_name, self.skilldata.fullname, "-" .. label2, "skills")
  self.color = color
end

function widgets.SkillWidget:sendCharacterData()

  if (self.skilldata ~= nil and self.skilldata.cost <= self.character.abstract.pp) then
    self.assets.sfx["mSelect"]:play()
    self.character:useSkill(self.skillname)
  else
    core.debug:warning("cbs/menu", "skill " .. self.skillname .. " doesn't exist")
    self.character:doNothing()
    self.assets.sfx["mError"]:play()
    self.scene.turns.hud:showMessage("You haven't enough PP!")
  end

end

-- ItemWidget
-- A widget to handle items

function widgets.ItemWidget:new(character, menu_name, item)
  self.category = menu_name
  self.itemname = item
  local label2 = "00"

  self.number = game.loot:getItemNumber(menu_name, item)
  self.itemdata = game.loot:getItemData(menu_name, item)

  label2 = self.number

  widgets.ItemWidget.super.new(self, character, menu_name, self.itemdata.fullname, "x" .. label2, "skills")
end

function widgets.ItemWidget:sendCharacterData()
  self.character:useItem(self.category, self.itemname)
  self.assets.sfx["mBeep"]:play()
end


return widgets
