local this = class("pageInfoTip", G_UIPageBase)
local _currencyTpl = L_GameTpl:getCurrencyTpl()
local _commonItemTpl = L_GameTpl:getCommonItemTpl()
local _accessoryTpl = L_GameTpl:getAccessoryTpl()
local _accessoryMainTpl = L_GameTpl:getAccessoryMainTpl()
local _spiritronTpl = L_GameTpl:getSpiritronTpl()
local _battleInfoTpl = L_GameTpl:getBattleInfoTpl()
local _spiritronRankTpl = L_GameTpl:getSpiritronRankTpl()
local _taskItemTpl = L_GameTpl:getTaskItemTpl()
local _foodTpl = L_GameTpl:getFoodTpl()
local _mountSaddleTpl = L_GameTpl:getMountSaddleTpl()
local _collectionToolTpl = L_GameTpl:getCollectionToolTpl()
local _openAudio = "Play_SFX_System_HUD_General_Item_Detail_Open"
local _closeAudio = "Play_SFX_System_HUD_General_Item_Detail_Close"
local typeToFunc = {
  [L_Const.resType.commonItem] = "setCommonItemData",
  [L_Const.resType.spiritron] = "setSpiritronData",
  [L_Const.resType.currency] = "setCurrencyData",
  [L_Const.resType.accessory] = "setAccessoryData",
  [L_Const.resType.taskItem] = "setTaskItemData",
  [L_Const.resType.mountSaddle] = "setMountSaddleData",
  [L_Const.resType.collectionTool] = "setCollectionToolData"
}
local NoTipType = {
  [L_Const.resType.hero] = true,
  [L_Const.resType.pet] = true,
  [L_Const.resType.homeBuilding] = true
}

function this.bind()
  return {
    txt_name = "",
    img_icon = nil,
    img_iconFilter = nil,
    img_qualityCircle = nil,
    go_num = false,
    txt_num = nil,
    go_desc_main = false,
    txt_desc_main = nil,
    go_desc_sub = false,
    txt_desc_sub = nil,
    go_access = false,
    list_access = {
      moduleName = "pages/bag/cellItemAccess"
    },
    go_foodBuff = false,
    img_foodBuff = nil,
    go_foodDesc = false,
    go_foodDesc_pet = false,
    txt_foodDesc_pet = nil,
    go_foodDesc_hero = false,
    txt_foodDesc_hero = nil,
    module_fishRodBuff = {
      moduleName = "pages/fishing/moduleFishRodBuff"
    },
    go_spiritronLevel = false,
    txt_spiritronLevel = nil,
    go_spiritronStar = false,
    list_spiritronStar = {
      moduleName = "pages/bag/cellBagTipStar"
    },
    go_spiritronSkillDesc = false,
    txt_spiritronSkillDesc = nil,
    go_accessorySuitModule = false,
    module_accessorySuit = {
      moduleName = "pages/accessory/moduleAccessorySuit"
    },
    go_accessoryMainAtt = false,
    txt_mainAtt = "",
    img_mainAttIcon = "",
    go_suitInfo = false,
    txt_suitInfo = "",
    go_suitDescribe = false,
    txt_suitDescribe = "",
    go_accessoryAttModule = false,
    module_accessoryAtt = {
      moduleName = "pages/accessory/moduleAccessoryAtt"
    },
    go_randomPreview = false,
    list_randomPreview = {
      moduleName = "modulePages/cellIconBag"
    },
    go_spiritronAccessoryAtt = false,
    list_spiritronAccessoryAtt = {
      moduleName = "pages/bag/cellBagTipSpiritronAccessoryAtt"
    },
    goSelectNum = false,
    moduleSelectNum = {
      moduleName = "modulePages/moduleSelectNum"
    }
  }
end

function this.methods()
  return {
    onClick_close = function(self)
      self:popItem()
    end,
    onClick_confirmSelectNum = function(self)
      if self._selectNumCB then
        local num = self.modules.moduleSelectNum:getNum()
        self._selectNumCB(num)
      end
      L_UI:close(self.pageName)
    end,
    onClick_bg = function(self)
      self:popItem()
    end
  }
end

function this:check(options, callback)
  if NoTipType[options.itemType] then
    callback(false)
    return
  end
  if options.itemType == L_Const.resType.spiritron then
    local serverData = L_SpiritronStore:getSpiritronItem(options.guid)
    callback(not table.isEmpty(serverData))
    return
  end
  callback(true)
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  self._itemStack = {}
  self._itemType = options.itemType
  self._itemId = options.itemId
  self._guid = options.guid
  self._curNum = options.curNum
  self._maxNum = options.maxNum
  self._selectNumCB = options.selectNumCB
  self:initPage()
  C_AudioManager.Play(_openAudio)
end

function this:close()
  this.super.close(self)
  table.clear(self._itemStack)
  C_AudioManager.Play(_closeAudio)
end

function this:initPage()
  self:setGeneralData()
  if self[typeToFunc[self._itemType]] then
    self[typeToFunc[self._itemType]](self)
  end
  if self._selectNumCB then
    self.bind.goSelectNum = true
    self.modules.moduleSelectNum:initModule(nil, self._maxNum, 1, self._curNum)
    local weight, _ = self.bindComponents.transInfo:GetUISize()
    self.bindComponents.transInfo:SetUISize(weight, 280)
  end
end

function this:pushItem(options)
  if NoTipType[options.itemType] then
    return
  end
  local stackData = {
    itemType = self._itemType,
    itemId = self._itemId,
    guid = self._guid,
    curNum = self._curNum,
    maxNum = self._maxNum,
    selectNumCB = self._selectNumCB
  }
  table.insert(self._itemStack, stackData)
  self._itemType = options.itemType
  self._itemId = options.itemId
  self._guid = options.guid
  self._curNum = options.curNum
  self._maxNum = options.maxNum
  self._selectNumCB = options.selectNumCB
  self:initPage()
end

function this:popItem()
  if self._itemStack and #self._itemStack > 0 then
    local recoverOption = self._itemStack[#self._itemStack]
    table.remove(self._itemStack)
    self._itemType = recoverOption.itemType
    self._itemId = recoverOption.itemId
    self._guid = recoverOption.guid
    self._curNum = recoverOption.curNum
    self._maxNum = recoverOption.maxNum
    self._selectNumCB = recoverOption.selectNumCB
    self:initPage()
  else
    L_UI:close(self.pageName)
  end
end

function this:setGeneralData()
  local configData = L_ItemTplManager:getItemConfig(self._itemType, self._itemId)
  local commonItemTpl = _commonItemTpl:getTplById(self._itemId)
  self.bind.img_iconFilter = commonItemTpl and _commonItemTpl:getFilterIcon(commonItemTpl) or ""
  self.bind.txt_name = configData.name
  self.bind.img_icon = configData.icon
  self.bind.txt_desc_main = configData.desc
  self.bind.img_qualityCircle = string.format("UI/Atlas/Bag/tex_bag_rarity_tips_w_%s.png", configData.quality)
end

function this:setCommonItemData()
  local tpl = _commonItemTpl:getTplById(self._itemId)
  self.bind.go_num = _commonItemTpl:getInBag(tpl)
  self.bind.txt_num = tostring(L_BagStore:getItemNum(self._itemId))
  self.bind.go_desc_main = true
  self.bind.go_desc_sub = true
  self.bind.txt_desc_sub = _commonItemTpl:getSpecialDesc(tpl)
  local accessList = _commonItemTpl:getWayList(tpl)
  self:initAccessList(accessList)
  if _commonItemTpl:getType(tpl) == L_BagConst.bagItemType.food then
    self:setFoodData()
  elseif _commonItemTpl:getType(tpl) == L_BagConst.bagItemType.fishingRod then
    self:setFishingRodData()
  end
  if _commonItemTpl:getType(tpl) == L_BagConst.bagItemType.heroRandomGift then
    self.bind.go_randomPreview = true
    self:setRandomPreviewData()
  else
    self.bind.go_randomPreview = false
  end
end

function this:setFoodData()
  local tpl_food = _foodTpl:getTplById(self._itemId)
  self.bind.go_foodBuff = true
  self.bind.img_foodBuff = string.format("UI/Atlas/FoodType/tex_icon_foodtype_tag_0%s.png", _foodTpl:getFoodType(tpl_food))
  local satiety = _foodTpl:getSatietyScore(tpl_food)
  local isPetFood = not math.isEmpty(satiety)
  self.bind.go_desc_main = false
  self.bind.go_foodDesc = true
  self.bind.go_foodDesc_pet = isPetFood
  self.bind.txt_foodDesc_pet = string.concat(L_WordsTpl:getValue("ui_pet_food_desc"), satiety)
  local tpl_commonItem = _commonItemTpl:getTplById(self._itemId)
  self.bind.go_foodDesc_hero = true
  self.bind.txt_foodDesc_hero = _commonItemTpl:getDesc(tpl_commonItem)
end

function this:setFishingRodData()
  self.bind.go_desc_main = false
  local moduleRodBuff = self.modules.module_fishRodBuff
  moduleRodBuff:setData(self._itemId)
end

function this:setTaskItemData()
  local tpl = _taskItemTpl:getTplById(self._itemId)
  self.bind.go_num = _taskItemTpl:getInBag(tpl)
  self.bind.txt_num = tostring(L_TaskStore:getTaskPropNum(self._itemId))
  self.bind.go_desc_main = true
  self.bind.go_desc_sub = true
  self.bind.txt_desc_sub = _taskItemTpl:getSpecialDesc(tpl)
end

function this:setSpiritronData()
  local serverData = L_SpiritronStore:getSpiritronItem(self._guid)
  self.bind.go_spiritronLevel = true
  local rankData = _spiritronRankTpl:getChildren(serverData.spiritronId)
  local limitTxt = L_GameUtil.fillColor(string.concat("/", rankData[serverData.rank].rankLevelLimit), L_Const.colorHtml.grey008)
  self.bind.txt_spiritronLevel = string.concat(serverData.spiritronLv, limitTxt)
  local tpl = _spiritronTpl:getTplById(serverData.spiritronId)
  self.bind.img_icon = _spiritronTpl:getAvatarTexture(tpl, 1)
  self.bind.go_spiritronStar = true
  local stars = {}
  for i = 1, serverData.star - 1 do
    table.insert(stars, {})
  end
  self.bind.list_spiritronStar:clear()
  self.bind.list_spiritronStar:insert_array(stars)
  self.bind.go_spiritronSkillDesc = true
  self.bind.txt_spiritronSkillDesc = L_SpiritronManager:getConfigSkillDesc(serverData.spiritronId, serverData.star)
  local tmp = {}
  local attList = L_SpiritronManager:getConfigAttList(self._itemId, serverData.spiritronLv, serverData.rank)
  for k, v in pairs(attList) do
    local tpl_battleInfo = _battleInfoTpl:getTplById(k)
    table.insert(tmp, {
      txt_name = _battleInfoTpl:getName(tpl_battleInfo),
      txt_value = _battleInfoTpl:getShowTxt(k, v)
    })
  end
  self.bind.go_spiritronAccessoryAtt = true
  self.bind.list_spiritronAccessoryAtt:clear()
  self.bind.list_spiritronAccessoryAtt:insert_array(tmp)
end

function this:setAccessoryData()
  local serverData = L_AccessoryStore:getAccessory(self._guid)
  local isInBag = not table.isEmpty(serverData)
  if isInBag then
    self:_setAccessoryDataInBag()
  else
    self:_setAccessoryDataNotInBag()
  end
end

function this:_setAccessoryDataInBag()
  local serverData = L_AccessoryStore:getAccessory(self._guid)
  local accessoryId = serverData.accessoryId
  local tpl = _accessoryTpl:getTplById(accessoryId)
  local configData = L_ItemTplManager:getAccessoryItem(accessoryId)
  self.bind.txt_name = string.concat(configData.name, "+", serverData.level)
  local packAtt = L_AccessoryManager:getPackAtt(serverData)
  self.bind.go_accessoryMainAtt = true
  local tpl_battleInfo = _battleInfoTpl:getTplById(packAtt.mainAtt.attId[1])
  self.bind.txt_mainAtt = string.concat(packAtt.mainAtt.showName, "+", _battleInfoTpl:getShowTxt(packAtt.mainAtt.attId[1], packAtt.mainAtt.value))
  self.bind.img_mainAttIcon = _battleInfoTpl:getIcon(tpl_battleInfo)
  self.bind.go_accessoryAttModule = true
  local moduleAccessoryAtt = self.modules.module_accessoryAtt
  moduleAccessoryAtt:initModule(self._guid, true)
  local suitId = _accessoryTpl:getSetId(tpl)
  local hasSuit = not math.isEmpty(suitId)
  self.bind.go_accessorySuitModule = hasSuit
  if hasSuit then
    self.modules.module_accessorySuit:setNormalSuit(self._guid)
  end
  self.bind.go_suitDescribe = true
  self.bind.txt_suitDescribe = _accessoryTpl:getDesc(tpl)
end

function this:_setAccessoryDataNotInBag()
  local accessoryId = self._itemId
  local tpl = _accessoryTpl:getTplById(accessoryId)
  local configData = L_ItemTplManager:getAccessoryItem(accessoryId)
  self.bind.txt_name = configData.name
  self.bind.go_accessoryMainAtt = true
  local mainAttId = _accessoryTpl:getMainAttr(tpl)[1][1]
  local tpl_mainAtt = _accessoryMainTpl:getTplByGroupIdAndLevel(mainAttId, 0)
  local mainAttBattleInfoId = _accessoryMainTpl:getBattleInfo(tpl_mainAtt)[1]
  local tpl_battleInfo = _battleInfoTpl:getTplById(mainAttBattleInfoId)
  self.bind.txt_mainAtt = string.concat(_accessoryMainTpl:getBattleInfoName(tpl_mainAtt), "+", _battleInfoTpl:getShowTxt(mainAttBattleInfoId, _accessoryMainTpl:getValue(tpl_mainAtt)))
  self.bind.img_mainAttIcon = _battleInfoTpl:getIcon(tpl_battleInfo)
  local suitId = _accessoryTpl:getSetId(tpl)
  local hasSuit = not math.isEmpty(suitId)
  self.bind.go_accessorySuitModule = hasSuit
  self.bind.go_suitInfo = hasSuit
  if hasSuit then
    local suitModule = self.modules.module_accessorySuit
    suitModule:setNormalSuitPreview(self._itemId)
    self.bind.txt_suitInfo = L_AccessoryManager:getSuitContainsDesc(suitId)
  end
  self.bind.go_suitDescribe = true
  self.bind.txt_suitDescribe = _accessoryTpl:getDesc(tpl)
end

function this:setCurrencyData()
  local tpl = _currencyTpl:getTplById(self._itemId)
  local num = L_PlayerStore:getCurrencyNum(self._itemId)
  self.bind.go_desc_main = true
  self.bind.go_desc_sub = true
  self.bind.txt_desc_sub = _currencyTpl:getSpecialDesc(tpl)
  self.bind.go_num = true
  self.bind.txt_num = tostring(num)
  local accessList = _currencyTpl:getWayList(tpl)
  self:initAccessList(accessList)
end

function this:setCollectionToolData()
  local tpl = _collectionToolTpl:getTplById(self._itemId)
  self.bind.go_desc_main = true
  self.bind.go_desc_sub = true
  self.bind.txt_desc_sub = _collectionToolTpl:getSpecialDesc(tpl)
  self.bind.go_num = false
  local access = _collectionToolTpl:getWay1(tpl)
  local accessDesc = _collectionToolTpl:getWay1desc(tpl)
  if not L_SystemUnlockStore:getSystemIsOpen(L_SystemConst.enum.itemAccess) then
    return
  end
  self.bind.go_access = not math.isEmpty(access)
  local tmp = {}
  table.insert(tmp, {
    systemId = access,
    txt_access = accessDesc,
    itemId = self._itemId
  })
  self.bind.list_access:clear()
  self.bind.list_access:insert_array(tmp)
end

function this:setMountSaddleData()
  local tpl = _mountSaddleTpl:getTplById(self._itemId)
  self.bind.go_desc_main = true
  self.bind.go_desc_sub = true
  self.bind.txt_desc_sub = _commonItemTpl:getSpecialDesc(tpl)
end

function this:setRandomPreviewData()
  local tpl = _commonItemTpl:getTplById(self._itemId)
  local dropId = _commonItemTpl:getUseFunction(tpl)[1]
  local dropItems = L_GameTpl:getDropTpl():getDropGroup(dropId)
  local previewList = {}
  for _, dropInfo in pairs(dropItems) do
    local numStr = dropInfo.minValue == dropInfo.maxValue and tostring(dropInfo.minValue) or tostring(dropInfo.minValue) .. "~" .. tostring(dropInfo.maxValue)
    table.insert(previewList, {
      itemType = dropInfo.type,
      itemId = dropInfo.itemId,
      itemNumTxt = numStr,
      callback = function()
        self:pushItem({
          itemType = dropInfo.type,
          itemId = dropInfo.itemId
        })
      end
    })
  end
  self.bind.list_randomPreview:clear()
  self.bind.list_randomPreview:insert_array(previewList)
end

function this:initAccessList(accessList)
  if not L_SystemUnlockStore:getSystemIsOpen(L_SystemConst.enum.itemAccess) then
    return
  end
  self.bind.go_access = not table.isEmpty(accessList)
  local tmp = {}
  for i, v in ipairs(accessList) do
    table.insert(tmp, {
      systemId = v.id,
      txt_access = v.desc,
      itemId = self._itemId
    })
  end
  self.bind.list_access:clear()
  self.bind.list_access:insert_array(tmp)
end

return this
