local ParentScreen = require "scenes.overworld.screens.parent"
local ItemsScreen = ParentScreen:extend()

local menu = require "game.modules.menus.list"
local const = require "scenes.overworld.screens.mainmenu.const"
local gui = require "game.modules.gui"

local ItemWidget = menu.DualTextWidget:extend()
local BackWidget = menu.DualTextWidget:extend()
local ResumeWidget = menu.DualTextWidget:extend()
local DropWidget = menu.DualTextWidget:extend()
local UseWidget = menu.DualTextWidget:extend()

local DESC_SIZE = 48*4

function ItemsScreen:new(scene, menuIndex, widgetIndex)
  self.menuIndex = menuIndex or 1
  self.widgetIndex = widgetIndex or 1
  self.choiceBack = gui.newChoiceBack(DESC_SIZE)
  self.descBox = gui.newTextBox("assets/gui/dialogbox.png", DESC_SIZE, 40+16)
  self.effectBox = gui.newTextBox("assets/gui/dialogbox.png", DESC_SIZE, 40)
  self.desc = ""
  self.effects = ""
  ItemsScreen.super.new(self, scene, "Items")
end

function ItemsScreen:update(dt)
  if (self.scene.menusystem.menus["useMenu"] == nil) then
    local keys = self.scene:getKeys(1)
    if (keys["left"].isPressed) then
      self.widgetIndex = 1
      self:getMenu(self.menuIndex - 1)
    end
    if (keys["right"].isPressed) then
      self.widgetIndex = 1
      self:getMenu(self.menuIndex + 1)
    end
  end
end

function ItemsScreen:getMenu(newMenuIndex)
  local newMenuIndex = newMenuIndex or 1
  if (newMenuIndex > #game.loot.inventory) then
    newMenuIndex = 1
  end
  if (newMenuIndex < 1) then
    newMenuIndex = #game.loot.inventory
  end

  self.menuIndex = newMenuIndex
  self.scene.menusystem:reset()
  self:setMenu()
end

function ItemsScreen:setMenu(widgetId)
  self.pocket = game.loot:getPocketById(self.menuIndex)
  local w = 128+32
  menu.ListMenu(self.scene, "menu", const.X2 - w, const.Y + 8, w, 9, true)
  for i,item in ipairs(self.pocket.list) do
    --menu.DualTextWidget(self.scene, "menu", item.name, "x" .. item.number)
    ItemWidget(self.scene, self.pocket.name, item, i)
  end
  BackWidget(self.scene)
  self.scene.menusystem.menus["menu"]:setCancelWidget()
  self.scene.menusystem:switchMenu("menu")
  self.scene.menusystem.menus["menu"].isLocked = true
  self.scene.menusystem.menus["menu"].widget.selected = math.max(1, self.widgetIndex)
end

function ItemsScreen:setUseMenu(item, widgetId)
  local w = 96
  menu.ListMenu(self.scene, "useMenu", const.X2 + 16 - w, const.Y2 - 48, w, 3, true)
  UseWidget(self.scene, self.pocket.name, item, widgetId)
  DropWidget(self.scene, self.pocket.name, item, widgetId)
  ResumeWidget(self.scene)
  self.scene.menusystem.menus["useMenu"]:setCancelWidget()
  self.scene.menusystem:switchMenu("useMenu")
  self.scene.menusystem.menus["useMenu"]:setDepth(-1)
end

function ItemsScreen:removeUseMenu()
  self.scene.menusystem.menus["useMenu"]:destroy()
  self.scene.menusystem:switchMenu("menu")
end

function ItemsScreen:draw()
  self.scene.assets.fonts["small"]:setLineHeight(16/18)
  self:drawPocket()
  self:drawDescription(const.X, const.Y2 - (88+16))
  self:drawEffects(const.X, const.Y2 - 40)
end

function ItemsScreen:drawDescription(x, y)
  love.graphics.draw(self.descBox, x, y)
  local xx, yy, ww = x + 6, y + 4, DESC_SIZE - 12
  self.scene.assets.fonts["small"]:draw(self.desc, xx, yy, ww, "left")
end

function ItemsScreen:drawEffects(x, y)
  love.graphics.draw(self.effectBox, x, y)
  local xx, yy, ww = x + 6, y + 4, DESC_SIZE - 12
  self.scene.assets.fonts["small"]:draw(self.effects, xx, yy, ww, "left")
end

function ItemsScreen:drawPocket()
  local middleX = ((16) + (DESC_SIZE - 24)) / 2
  love.graphics.draw(self.choiceBack, const.X, const.Y)

  self.scene.assets.fonts["small"]:draw("<", const.X + 16, const.Y - 2, -1, "left")
  self.scene.assets.fonts["small"]:draw(">", const.X + DESC_SIZE - 24, const.Y - 2, -1, "right")
  self.scene.assets.fonts["small"]:draw(self.pocket.fullname, const.X + middleX, const.Y - 2, -1, "center")

  self:drawPocketRoll(const.X + 48*2, const.Y + 20)
end

function ItemsScreen:drawPocketRoll(x, y)
  core.screen:setScissor(const.X, const.Y+ 16, 48*4, 48)

  local trueX = x - ((self.menuIndex - 1) * 32)
  for i, pocket in ipairs(game.loot.inventory) do
    local trueIndex = i - self.menuIndex
    if (trueIndex > 4) then
      trueIndex = trueIndex - 8
    end
    if (trueIndex < -4) then
      trueIndex = trueIndex + 8
    end
    if (trueIndex ~= 0) then
      love.graphics.setColor(.3, .3, .3, .6)
    end
    self.scene.assets.tileset["itembox"]:drawTile(i, x + ((trueIndex)*32), y, 0, 1, 1, 14, 0)
    utils.graphics.resetColor()
  end

  core.screen:resetScissor()
end

-- Items Widgets
function ItemWidget:new(scene, pocket, item, widgetId)
  self.item = item
  self.itemData = game.loot:getItemData(pocket, self.item.name)
  self.pocket = pocket
  self.widgetId = widgetId
  ItemWidget.super.new(self, scene, "menu", self.itemData.fullname, "x" .. utils.math.numberToString(self.item.number, 2))
end

function ItemWidget:selectAction()
  self.scene.currentScreen.desc = self.itemData.description
  self.scene.currentScreen.effects = game.loot:getEffectStrings(self.pocket, self.item.name)
end

function ItemWidget:action() 
  self.scene.currentScreen:setUseMenu(self.item, self.widgetId)
end

function BackWidget:new(scene)
  ItemWidget.super.new(self, scene, "menu", "Back", "")
end

function BackWidget:selectAction()
  self.scene.currentScreen.desc = ""
  self.scene.currentScreen.effects = ""
end

function BackWidget:action()
  self.scene.screens.mainmenu["pause"](self.scene, "main", 3)
end

-- Use Menu Widget

function UseWidget:new(scene, pocket, item, widgetId)
  self.item = item
  self.itemData = game.loot:getItemData(pocket, self.item.name)
  self.pocket = pocket
  self.widgetId = widgetId
  UseWidget.super.new(self, scene, "useMenu", "Use", "")
  if (not self.itemData.usableOnMap) then
    self.color = {0.6, 0.6, 0.6}
  end
end

function UseWidget:action()
  if (self.itemData.usableOnMap) then
    self.scene.screens.mainmenu["useItems"](self.scene, self.pocket, self.item, self.widgetId)
  end
end

function DropWidget:new(scene, pocket, item, widgetId)
  self.item = item
  self.itemData = game.loot:getItemData(pocket, self.item.name)
  self.pocket = pocket
  self.max = self.item.number
  self.widgetId = widgetId
  self.number = 1
  DropWidget.super.new(self, scene, "useMenu", "Drop", "<" .. utils.math.numberToString(self.number, 2) .. ">")
end

function DropWidget:updateSelected()
  local keys = self.scene:getKeys(1)
  if (keys["left"].isPressed) then
    if (self.number > 1) then
      self.number = self.number - 1
    else
      self.number = self.max
    end
    self.label2 = "<" .. utils.math.numberToString(self.number, 2) .. ">"
    self:invalidateCanvas()
  end
  if (keys["right"].isPressed) then
    if (self.number < self.max) then
      self.number = self.number + 1
    else
      self.number = 1
    end
    self.label2 = "<" .. utils.math.numberToString(self.number, 2) .. ">"
    self:invalidateCanvas()
  end
end

function DropWidget:action()
  game.loot:removeItem(self.pocket, self.item.name, self.number)
  self.scene.currentScreen.widgetIndex = self.widgetId
  self.scene.currentScreen:removeUseMenu()
  self.scene.currentScreen:setMenu()
end

function ResumeWidget:new(scene)
  ResumeWidget.super.new(self, scene, "useMenu", "Back", "")
end

function ResumeWidget:action()
  self.scene.currentScreen:removeUseMenu()
end

return ItemsScreen
