local this = class("moduleEntrustBattleTaskList", G_UIModuleBase)
local RewardBoxEnum = {
  HasReward = "UI/Atlas/EntrustTask/tex_dungeonentrust_icon_bx_box_01.png",
  NoReward = "UI/Atlas/EntrustTask/tex_dungeonentrust_icon_bx_box_02.png"
}
local RewardStarEnum = {
  HasReward = "UI/Atlas/EntrustTask/tex_dungeonentrust_icon_bx_Star_01.png",
  NoReward = "UI/Atlas/EntrustTask/tex_dungeonentrust_icon_bx_Star_02.png"
}

function this.bind()
  return {
    txt_reward = "",
    active_numBg = true,
    active_rewardBg = false,
    scroll_view = {
      moduleName = "pages/entrustTask/cellEntrustBattleTask"
    },
    img_rewardBox = "",
    img_rewardStar = "",
    active_bg = false
  }
end

function this.methods()
  return {
    btn_reward_click = function(self)
      self:onBtnOpenBox()
    end
  }
end

function this:activate()
  self:open()
  self:openModules()
  self:show()
  self:showModules()
end

function this:deActivate()
  self:hide()
  self:hideModules()
  self:close()
  self:closeModules()
end

function this:open()
  self:refreshBoxInfo()
  L_EntrustStore:listenCallFunc(L_EntrustStore.event.entrustRewardRefresh, self.onEvent_entrustRewardRefresh, self)
end

function this:close()
  L_EntrustStore:unListenCallFunc(L_EntrustStore.event.entrustRewardRefresh, self.onEvent_entrustRewardRefresh, self)
end

function this:setListData(data)
  self.listData = data
end

function this:refreshList()
  local tmp_list = {}
  for i, item in pairs(self.listData) do
    table.insert(tmp_list, item)
  end
  table.sort(tmp_list, function(a, b)
    return tonumber(a.level_num) < tonumber(b.level_num)
  end)
  self.bind.scroll_view:clear()
  self.bind.scroll_view:insert_array(tmp_list)
end

function this:refreshAllBtnFalse()
  for i = 1, #self.bind.scroll_view do
    self.bind.scroll_view:getItemCls(i):refreshSelect(false)
  end
end

function this:selectItem(index)
  for i = 1, #self.bind.scroll_view do
    if self.bind.scroll_view:getItemCls(i):getLevelId() == index then
      self.bind.scroll_view:getItemCls(i):refreshSelect(true)
    else
    end
  end
end

function this:refreshBoxInfo()
  local curStarNum = L_EntrustStore:getEntrustStarNumByDifficulty(L_Const.EntrustType.PetDuel, 0)
  local firstRewardStarNum, rewardId = L_EntrustStore:getFirstCanReceiveStarNum(L_Const.EntrustType.PetDuel, 0)
  local canReceiveReward = curStarNum >= firstRewardStarNum and 0 < rewardId
  if canReceiveReward then
    self.bind.active_rewardBg = true
    self.bind.active_numBg = false
  else
    self.bind.txt_reward = string.format("%s/%s", curStarNum, firstRewardStarNum)
    self.bind.active_rewardBg = false
    self.bind.active_numBg = true
  end
  self.bind.img_rewardBox = canReceiveReward and RewardBoxEnum.HasReward or RewardBoxEnum.NoReward
  self.bind.img_rewardStar = canReceiveReward and RewardStarEnum.HasReward or RewardStarEnum.NoReward
  self.bindComponents.rewardBtn.legacyAnimationTriggers.normalTrigger = canReceiveReward and "anim_entrust_normal_full" or "anim_entrust_normal"
  self.bindComponents.rewardBtn.legacyAnimationTriggers.highlightedTrigger = canReceiveReward and "anim_entrust_hover_full" or "anim_entrust_hover"
  self.bindComponents.rewardBtn.legacyAnimationTriggers.selectedTrigger = canReceiveReward and "anim_entrust_Selected_full" or "anim_entrust_Selected"
  self.bindComponents.rewardAnim:Play(canReceiveReward and "anim_entrust_normal_full" or "anim_entrust_normal")
end

function this:onBtnOpenBox()
  self.bind.active_bg = true
  Timer.once(0.4, function()
    self.bind.active_bg = false
    local curStarNum = L_EntrustStore:getEntrustStarNumByDifficulty(L_Const.EntrustType.PetDuel, 0)
    local rewardIdList = L_EntrustStore:getCanReceiveRewardIdList(L_Const.EntrustType.PetDuel, curStarNum, 0)
    if rewardIdList then
      L_EntrustStore:req_getEntrustReward(rewardIdList)
      return
    end
    L_UI:open("pageEntrustTaskReward", {
      difficultyId = 0,
      entrustType = L_Const.EntrustType.PetDuel
    })
  end, self, self.gameObject)
end

function this:onEvent_entrustRewardRefresh()
  self:refreshBoxInfo()
end

return this
