local this = class("moduleBagTip", G_UIModuleBase)
local _spiritronRankTpl = L_GameTpl:getSpiritronRankTpl()
local _battleInfoTpl = L_GameTpl:getBattleInfoTpl()
local _commonItemTpl = L_GameTpl:getCommonItemTpl()
local _foodTpl = L_GameTpl:getFoodTpl()
local _commonItemUseTypeTpl = L_GameTpl:getCommonItemUseTypeTpl()
local _taskItemTpl = L_GameTpl:getTaskItemTpl()
local _spiritronTpl = L_GameTpl:getSpiritronTpl()
local _accessoryTpl = L_GameTpl:getAccessoryTpl()
local _petEggTpl = L_GameTpl:getPetEggTpl()
local attrBaseName = {
  [L_Const.battleAtt.MAXHP] = "info_attr_basic_hp",
  [L_Const.battleAtt.ATK] = "info_attr_basic_atk",
  [L_Const.battleAtt.DEF] = "info_attr_basic_def",
  [L_Const.battleAtt.MDEF] = "info_attr_basic_mdef"
}

function this.bind()
  return {
    go_funcBtn = false,
    txt_funcBtn = nil,
    img_qualityBg = nil,
    img_icon = nil,
    img_iconFilter = nil,
    txt_name = "",
    go_desc = false,
    txt_desc = "",
    go_tag = false,
    list_tag = {
      moduleName = "pages/bag/cellBagTag"
    },
    go_desc_sub = false,
    txt_desc_sub = "",
    go_num = false,
    txt_num = "",
    go_access = false,
    list_access = {
      moduleName = "pages/bag/cellItemAccess"
    },
    list_star = {
      moduleName = "pages/bag/cellBagTipStar"
    },
    go_foodBuff = false,
    img_foodBuff = nil,
    go_foodUseBtnGroup = false,
    go_foodDesc = false,
    go_foodDesc_pet = false,
    txt_foodDesc_pet = nil,
    go_foodDesc_hero = false,
    txt_foodDesc_hero = nil,
    go_btnEatPet = false,
    go_moduleFishRodBuff = false,
    module_fishRodBuff = {
      moduleName = "pages/fishing/moduleFishRodBuff"
    },
    go_spiritronLevel = false,
    txt_spiritronLevel = nil,
    go_spiritronAtt = false,
    list_spiritronAtt = {
      moduleName = "pages/bag/cellBagTipSpiritronAccessoryAtt"
    },
    go_spiritronSkillDesc = false,
    txt_spiritronSkillDesc = nil,
    go_subDescLine = false,
    go_accessoryAtt = false,
    module_accessoryAtt = {
      moduleName = "pages/accessory/moduleAccessoryAtt"
    },
    go_accessorySuit = false,
    module_accessorySuit = {
      moduleName = "pages/accessory/moduleAccessorySuit"
    },
    go_accessoryHeadRank = false,
    img_accessoryHeadRank = nil,
    go_saLockBtn = false,
    isOn_saLock = false,
    go_saOwner = false,
    img_saOwner = nil,
    go_saMaxLevel = false
  }
end

function this.methods()
  return {
    onClick_func = function(self)
      if self._type == L_BagConst.bagTag.accessory then
        L_UI:open("pageAccessoryStrength", {
          accessoryId = self._guid
        })
      elseif self._type == L_BagConst.bagTag.spiritron then
        L_SpiritronManager:openSpiritUpgrade({
          isPreview = false,
          spiritGuid = self._guid
        })
      elseif self._type == L_BagConst.bagTag.petEgg then
        if math.isEmpty(self._guid) then
          return
        end
        L_PetReleaseManager:reqDeletePetEgg({
          self._guid
        })
      else
        L_BagManager:useBagItem(self._guid, self._callback or nil)
      end
    end,
    onClick_eatFoodByHero = function(self)
      L_BagManager:useBagItem(self._guid)
    end,
    onClick_eatFoodByPet = function(self)
      L_UI:open("pageMountFeed", {
        foodId = self._guid
      })
    end,
    onClick_lock = function(self)
      if self._type == L_BagConst.bagTag.accessory then
        L_AccessoryManager:setAccessoryLock(self._guid, function()
          local serverData = L_AccessoryStore:getAccessory(self._guid)
          self.bind.isOn_saLock = serverData.lock
        end)
      elseif self._type == L_BagConst.bagTag.spiritron then
        L_SpiritronStore:req_lockSpiritron(self._guid, function()
          local serverData = L_SpiritronStore:getSpiritronItem(self._guid)
          self.bind.isOn_saLock = serverData.lock
        end)
      end
    end,
    scrollList_access = {
      onClick_select = function(self)
        L_UI:close(self.pageName)
      end
    }
  }
end

function this:close()
  self._callback = nil
  L_PhotoManager:clearPhotoEntity(self)
end

function this:setData(type, guid, param)
  self._guid = guid
  self._type = type
  self._param = param or table.empty
  if type == L_BagConst.bagTag.accessory then
    self:setAccessoryData()
  elseif type == L_BagConst.bagTag.spiritron then
    self:setSpiritronData()
  elseif type == L_BagConst.bagTag.taskProp then
    self:setTaskPropData()
  elseif type == L_BagConst.bagTag.petEgg then
    self:setPetEggData()
  elseif type == L_BagConst.bagTag.collectionTool then
    self:setCollectionTool()
  else
    self:setCommonItemData()
  end
  self.bindComponents.spiritStoryRed.gameObject:SetActive(type == L_BagConst.bagTag.spiritron)
  if self._lastType ~= self._type then
    self.bindComponents.ani_moduleBagTip:Stop()
    self.bindComponents.ani_moduleBagTip:Play("anim_Nbag_tips_in")
    self._lastType = self._type
    self._lastGuid = self._guid
    self.bindComponents.scrollView_content.verticalNormalizedPosition = 1
  elseif self._lastGuid ~= self._guid then
    self.bindComponents.ani_moduleBagTip:Stop()
    self.bindComponents.ani_moduleBagTip:Play("anim_Nbag_tips_change")
    self._lastGuid = self._guid
    self.bindComponents.scrollView_content.verticalNormalizedPosition = 1
  end
end

function this:setAccessoryData()
  local serverData = L_AccessoryStore:getAccessory(self._guid)
  local tpl = _accessoryTpl:getTplById(serverData.accessoryId)
  local quality = _accessoryTpl:getRarity(tpl)
  self.bind.txt_name = string.format("%s+%s", _accessoryTpl:getName(tpl), serverData.level)
  self.bind.img_qualityBg = self:getQualityBgName(quality)
  self.bind.img_icon = _accessoryTpl:getIcon(tpl)
  self.bind.go_saLockBtn = true
  self.bind.isOn_saLock = serverData.lock
  local isEquipped = not math.isEmpty(serverData.wearHero)
  self.bind.go_saOwner = isEquipped
  if isEquipped then
    local heroData = L_HeroStore:getHero(serverData.wearHero)
    if heroData then
      local heroConf = L_ItemTplManager:getHeroItem(L_HeroStore:getHeroConfigId(heroData))
      self.bind.img_saOwner = heroConf.icon_small
      if heroData.type == L_Const.HeroType.HT_MAIN then
        L_PhotoManager:newOrBindPhotoEntity(self, self.bindComponents.img_head, L_PlayerStore:getPlayerModHeadImgName())
      else
        L_PhotoManager:clearPhotoEntity(self)
      end
    end
  end
  self.bind.go_accessoryAtt = true
  local moduleAccessoryAtt = self.modules.module_accessoryAtt
  moduleAccessoryAtt:initModule(self._guid)
  self.bind.go_accessorySuit = true
  local moduleAccessorySuit = self.modules.module_accessorySuit
  moduleAccessorySuit:setNormalSuit(self._guid)
  local isMax = L_AccessoryStore:getIsMaxLevel(self._guid)
  self.bind.go_funcBtn = not isMax and not self._param.hideFuncBtn
  self.bind.go_saMaxLevel = isMax
  local tpl_use = _commonItemUseTypeTpl:getTplById(L_BagConst.bagUseType.accessory)
  self.bind.txt_funcBtn = _commonItemUseTypeTpl:getName(tpl_use)
  self:setStar(quality + 1)
  local headRank = _accessoryTpl:getHeadRank(tpl)
  self.bind.go_accessoryHeadRank = 0 < headRank
  if 0 < headRank then
    self.bind.img_accessoryHeadRank = L_AccessoryStore:getAccessoryHeadRankIcon(headRank)
  end
end

function this:setSpiritronData()
  local serverData = L_SpiritronStore:getSpiritronItem(self._guid)
  local tpl = _spiritronTpl:getTplById(serverData.spiritronId)
  L_ReddotManager:registerReddot(self.bindComponents.spiritStoryRed, string.format(L_ReddotManager.DotDef.SpiritSory, serverData.spiritronId))
  self.bind.txt_name = _spiritronTpl:getName(tpl)
  self.bind.img_qualityBg = self:getQualityBgName(_spiritronTpl:getRarity(tpl))
  self.bind.img_icon = _spiritronTpl:getAvatarTexture(tpl, 1)
  self.bind.go_saLockBtn = true
  self.bind.isOn_saLock = serverData.lock
  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 isEquipped = not math.isEmpty(serverData.spiritronHero)
  self.bind.go_saOwner = isEquipped
  if isEquipped then
    local heroData = L_HeroStore:getHero(serverData.spiritronHero)
    if heroData then
      local heroConf = L_ItemTplManager:getHeroItem(L_HeroStore:getHeroConfigId(heroData))
      self.bind.img_saOwner = heroConf.icon_small
      if L_HeroStore:getHeroGuid(heroData) == L_HeroStore:getDefaultHeroGuid() then
        L_PhotoManager:newOrBindPhotoEntity(self, self.bindComponents.img_head, L_PlayerStore:getPlayerModHeadImgName())
      else
        L_PhotoManager:clearPhotoEntity(self)
      end
    end
  end
  local rarity = _spiritronTpl:getRarity(tpl)
  self:setStar(rarity + 1)
  local tmp = {}
  local attList = L_SpiritronManager:getConfigAttList(serverData.spiritronId, serverData.spiritronLv, serverData.rank)
  local showBg = true
  for k, v in pairs(attList) do
    local tpl_battleInfo = _battleInfoTpl:getTplById(k)
    local name = L_WordsTpl:getValue(attrBaseName[k])
    table.insert(tmp, {
      txt_name = string.isEmpty(name) and _battleInfoTpl:getName(tpl_battleInfo) or name,
      txt_value = _battleInfoTpl:getShowTxt(k, v),
      go_bg = showBg
    })
    showBg = not showBg
  end
  self.bind.go_spiritronAtt = true
  self.bind.list_spiritronAtt:clear()
  self.bind.list_spiritronAtt:insert_array(tmp)
  self.bind.go_spiritronSkillDesc = true
  self.bind.txt_spiritronSkillDesc = L_SpiritronManager:getConfigSkillDesc(serverData.spiritronId, serverData.star)
  self.bind.go_subDescLine = true
  self.bind.go_desc_sub = true
  self.bind.txt_desc_sub = _spiritronTpl:getDesc(tpl)
  local tpl_use = _commonItemUseTypeTpl:getTplById(L_BagConst.bagUseType.spiritron)
  self.bind.go_funcBtn = not self._param.hideFuncBtn
  self.bind.txt_funcBtn = _commonItemUseTypeTpl:getName(tpl_use)
end

function this:setTaskPropData()
  local tpl = _taskItemTpl:getTplById(self._guid)
  local quality = _taskItemTpl:getRarity(tpl)
  self.bind.txt_name = _taskItemTpl:getName(tpl)
  self.bind.img_qualityBg = self:getQualityBgName(quality)
  self.bind.img_icon = _taskItemTpl:getIcon(tpl)
  self.bind.go_num = true
  self.bind.txt_num = tostring(L_TaskStore:getTaskPropNum(self._guid))
  self.bind.go_tag = true
  local tag = {}
  local tags = _taskItemTpl:getTag(tpl)
  for i, v in ipairs(tags) do
    table.insert(tag, {txt_tag = v})
  end
  self.bind.list_tag:clear()
  self.bind.list_tag:insert_array(tag)
  self.bind.go_desc = true
  self.bind.txt_desc = _taskItemTpl:getDesc(tpl)
  self.bind.go_desc_sub = true
  self.bind.txt_desc_sub = _taskItemTpl:getSpecialDesc(tpl)
  local useType = _taskItemTpl:getUseType(tpl)
  self.bind.go_funcBtn = not math.isEmpty(useType) and not self._param.hideFuncBtn
  if not math.isEmpty(useType) then
    local tpl_use = _commonItemUseTypeTpl:getTplById(useType)
    self.bind.txt_funcBtn = _commonItemUseTypeTpl:getName(tpl_use)
  end
  self:setStar(quality + 1)
end

function this:setPetEggData()
  local petEgg = L_PetStore:getPetEggItem(self._guid)
  local tpl = _petEggTpl:getTplById(petEgg.configId)
  local quality = _petEggTpl:getRarity(tpl)
  self.bind.txt_name = _petEggTpl:getName(tpl)
  self.bind.img_qualityBg = self:getQualityBgName(quality)
  self.bind.img_icon = _petEggTpl:getEggImgIcon(tpl)
  self.bind.go_tag = true
  local tag = {}
  local tags = _petEggTpl:getTag(tpl)
  for i, v in ipairs(tags) do
    table.insert(tag, {txt_tag = v})
  end
  self.bind.list_tag:clear()
  self.bind.list_tag:insert_array(tag)
  self.bind.go_desc = true
  self.bind.txt_desc = _petEggTpl:getDesc(tpl)
  self.bind.go_desc_sub = true
  self.bind.txt_desc_sub = _petEggTpl:getSpecialDesc(tpl)
  local useType = _petEggTpl:getUseType(tpl)
  self.bind.go_funcBtn = not math.isEmpty(useType) and not self._param.hideFuncBtn
  if not math.isEmpty(useType) then
    local tpl_use = _commonItemUseTypeTpl:getTplById(useType)
    self.bind.txt_funcBtn = _commonItemUseTypeTpl:getName(tpl_use)
  end
  self:setStar(quality + 1)
end

function this:setCollectionTool()
  local data = L_CollectionToolStore:getTool(self._guid)
  local configTable = CS.Azur.Gameplay.Table.TDCollectionToolTable.GetData(data.id)
  local quality = configTable.rarity
  self.bind.txt_name = configTable.name.value
  self.bind.img_qualityBg = self:getQualityBgName(quality)
  self.bind.img_icon = configTable.icon[0]
  self.bind.go_tag = true
  local tag = {}
  local tags = configTable.tag
  if tags then
    for i = 0, tags.Count - 1 do
      table.insert(tag, {
        txt_tag = tags[i].value
      })
    end
  end
  self.bind.list_tag:clear()
  self.bind.list_tag:insert_array(tag)
  self.bind.go_desc = true
  self.bind.txt_desc = configTable.desc.value
  self.bind.go_desc_sub = true
  self.bind.txt_desc_sub = configTable.specialDesc.value
  if L_SystemUnlockStore:getSystemIsOpen(L_SystemConst.enum.itemAccess) then
    self.bind.go_access = true
    local tmp = {}
    for i = 1, 3 do
      local systemIdKey = string.format("way%d", i)
      local descKey = string.format("way%ddesc", i)
      if not string.isEmpty(configTable[descKey].value) then
        table.insert(tmp, {
          systemId = configTable[systemIdKey],
          txt_access = configTable[descKey].value,
          itemId = self._guid
        })
      end
    end
    self.bind.list_access:clear()
    self.bind.list_access:insert_array(tmp)
  end
  self:setStar(quality + 1)
end

function this:setCommonItemData()
  local tpl = _commonItemTpl:getTplById(self._guid)
  local quality = _commonItemTpl:getRarity(tpl)
  self.bind.txt_name = _commonItemTpl:getName(tpl)
  self.bind.img_qualityBg = self:getQualityBgName(quality)
  self.bind.img_icon = _commonItemTpl:getIcon(tpl)
  self.bind.img_iconFilter = _commonItemTpl:getFilterIcon(tpl)
  self.bind.go_num = true
  self.bind.txt_num = tostring(L_BagStore:getItemNum(self._guid))
  self.bind.go_tag = true
  local tag = {}
  local tags = _commonItemTpl:getTag(tpl)
  for i, v in ipairs(tags) do
    table.insert(tag, {txt_tag = v})
  end
  self.bind.list_tag:clear()
  self.bind.list_tag:insert_array(tag)
  self.bind.go_desc = true
  self.bind.txt_desc = _commonItemTpl:getDesc(tpl)
  self.bind.go_desc_sub = true
  self.bind.txt_desc_sub = _commonItemTpl:getSpecialDesc(tpl)
  if L_SystemUnlockStore:getSystemIsOpen(L_SystemConst.enum.itemAccess) then
    self.bind.go_access = true
    local accessList = _commonItemTpl:getWayList(tpl)
    local tmp = {}
    for i, v in ipairs(accessList) do
      table.insert(tmp, {
        systemId = v.id,
        txt_access = v.desc,
        itemId = self._guid
      })
    end
    self.bind.list_access:clear()
    self.bind.list_access:insert_array(tmp)
  end
  local useType = _commonItemTpl:getUseType(tpl)
  self.bind.go_funcBtn = not math.isEmpty(useType) and not self._param.hideFuncBtn
  if not math.isEmpty(useType) then
    local tpl_use = _commonItemUseTypeTpl:getTplById(useType)
    self.bind.txt_funcBtn = _commonItemUseTypeTpl:getName(tpl_use)
  end
  self:setStar(quality + 1)
  local type = _commonItemTpl:getType(tpl)
  self.bind.go_moduleFishRodBuff = L_BagConst.bagItemType.fishingRod == type
  if type == L_BagConst.bagItemType.food then
    self:setFoodData()
  elseif type == L_BagConst.bagItemType.fishingRod then
    self:setFishingRodData()
  elseif type == L_BagConst.bagItemType.fishingBait then
    self:setFishingBaitData()
  end
end

function this:setFoodData()
  local tpl_food = _foodTpl:getTplById(self._guid)
  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_funcBtn = self.bind.go_funcBtn and not isPetFood and not self._param.hideFuncBtn
  self.bind.go_foodUseBtnGroup = isPetFood and not self._param.hideFuncBtn
  self.bind.go_desc = 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._guid)
  self.bind.go_foodDesc_hero = true
  self.bind.txt_foodDesc_hero = _commonItemTpl:getDesc(tpl_commonItem)
  self.bind.go_btnEatPet = L_SystemUnlockStore:getSystemIsOpen(L_SystemConst.enum.mountSnackBag)
end

function this:setFishingRodData()
  local isFromPond = not math.isEmpty(self._param.fishPondId)
  self.bind.go_desc = false
  self.bind.go_tag = not isFromPond
  self.bind.go_access = not isFromPond
  self.bind.go_subDescLine = false
  self.bind.go_num = false
  local moduleRodBuff = self.modules.module_fishRodBuff
  moduleRodBuff:setData(self._guid, self._param.fishPondId)
end

function this:setFishingBaitData()
  local isFromPond = not math.isEmpty(self._param.fishPondId)
  self.bind.go_tag = not isFromPond
  self.bind.go_access = not isFromPond
end

function this:reset()
  self.bind.go_funcBtn = false
  self.bind.go_desc = false
  self.bind.go_tag = false
  self.bind.go_desc_sub = false
  self.bind.go_num = false
  self.bind.go_access = false
  self.bind.go_foodBuff = false
  self.bind.go_foodUseBtnGroup = false
  self.bind.go_foodDesc = false
  self.bind.go_moduleFishRodBuff = false
  self.bind.go_spiritronLevel = false
  self.bind.go_spiritronAtt = false
  self.bind.go_spiritronSkillDesc = false
  self.bind.go_subDescLine = false
  self.bind.go_accessoryAtt = false
  self.bind.go_accessorySuit = false
  self.bind.go_accessoryHeadRank = false
  self.bind.go_saOwner = false
  self.bind.go_saLockBtn = false
  self.bind.go_saMaxLevel = false
end

function this:setStar(starNum)
  local stars = {}
  for i = 1, starNum do
    table.insert(stars, {
      go_active = i <= starNum
    })
  end
  self.bind.list_star:clear()
  self.bind.list_star:insert_array(stars)
end

function this:setEmpty()
  self._lastGuid = nil
  self._lastType = nil
end

function this:setType(type)
  self._lastType = type
end

function this:restSelectGuid()
  self._lastGuid = nil
end

function this:restSelectType()
  self._lastType = nil
end

function this:setUseCallBack(cb)
  self._callback = cb
end

function this:setEquippedAccessory(heroGuid, isPreview)
  local suitModule = self.modules.module_accessorySuit
  suitModule:setEquippedSuit(self._guid, heroGuid, isPreview)
  self.bind.go_saMaxLevel = false
end

function this:getQualityBgName(quality)
  quality = quality or 0
  if self.getQualityBgNameFunc then
    return self.getQualityBgNameFunc(quality)
  end
  return string.format("UI/Atlas/Bag/tex_bag_rarity_tips_bg_%s.png", quality)
end

function this:setQualityBgFunc(cb)
  self.getQualityBgNameFunc = cb
end

function this:setMapCollectionShow(isShow)
  self.bind.go_access = isShow
  self.bind.go_foodUseBtnGroup = isShow
end

return this
