local cls = class("moduleEntrustTask", G_UIModuleBase)
local _dungeonEntrustTagTpl = L_GameTpl:getDungeonEntrustTagTpl()
local _dungeonEntrustTpl = L_GameTpl:getDungeonEntrustTpl()
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 cls.bind()
  return {
    module_tabList = {
      moduleName = "pages/entrustTask/moduleEntrustTaskFilterTabList"
    },
    go_allTaskList = false,
    table_allTaskList = {
      moduleName = "pages/entrustTask/cellEntrustTaskGroup"
    },
    go_filterTaskList = false,
    table_filterTaskList = {
      moduleName = "pages/entrustTask/cellEntrustTask"
    },
    moduleEntrustLevelDetails = {
      moduleName = "pages/entrustTask/moduleEntrustLevelDetails"
    },
    img_rewardBox = "",
    img_rewardStar = "",
    txt_starNum = "",
    active_numBg = true,
    active_rewardBg = false,
    active_bg = false
  }
end

function cls.methods()
  return {
    onClick_openBox = function(self)
      self:onBtnOpenBox()
    end
  }
end

function cls:toBind(uiBinding)
  cls.super.toBind(self, uiBinding)
  self.selectFilterType = 999
  self.selectDifficultyIndex = 1
  self.groupFolder = {}
  self.isInit = false
  self:initTabList()
  self:refreshBoxInfo()
end

function cls:open()
  L_EntrustStore:listenCallFunc(L_EntrustStore.event.entrustRewardRefresh, self.onEvent_entrustRewardRefresh, self)
  L_EntrustStore:listenCallFunc(L_EntrustStore.event.entrustLevelSelect, self.onEvent_entrustLevelSelect, self)
end

function cls:initTabList()
  local tabList = {}
  for index, tpl in ipairs(_dungeonEntrustTagTpl:getShowData()) do
    table.insert(tabList, _dungeonEntrustTagTpl:getId(tpl))
  end
  self.modules.module_tabList:setData(tabList, function(tabId)
    local tpl = _dungeonEntrustTagTpl:getTplById(tabId)
    if not tpl then
      return
    end
    self.selectFilterType = tabId
    self:updateEntrustTaskList(self.isInit)
    self.isInit = true
  end, 1)
  self:updateEntrustTaskList()
end

function cls:updateEntrustTaskList(bChangeTab)
  self:initGroupTask(bChangeTab)
end

function cls:refreshBoxInfo()
  local curStarNum = L_EntrustStore:getEntrustStarNumByDifficulty(L_Const.EntrustType.AreaEntrust, self.selectDifficultyIndex)
  local firstRewardStarNum, rewardId = L_EntrustStore:getFirstCanReceiveStarNum(L_Const.EntrustType.AreaEntrust, self.selectDifficultyIndex)
  local canReceiveReward = curStarNum >= firstRewardStarNum and 0 < rewardId
  if canReceiveReward then
    self.bind.active_rewardBg = true
    self.bind.active_numBg = false
  else
    self.bind.txt_starNum = 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 cls:checkIsSpecialFilter()
  local cfg = _dungeonEntrustTagTpl:getTplById(self.selectFilterType)
  return _dungeonEntrustTagTpl:checkIsSpecial(cfg)
end

function cls:initGroupTask(bChangeTab)
  local modeInfo = _dungeonEntrustTpl:getInfoByDifficulty(self.selectDifficultyIndex)
  if self:checkIsSpecialFilter() then
    self.bind.go_allTaskList = true
    self.bind.go_filterTaskList = false
    self.bindComponents.scroll_taskListAnim:Rewind()
    self.bindComponents.scroll_taskListAnim:Play()
    local onClick_select = function(cell)
      if self.groupFolder[cell.bind.groupId] then
        self.groupFolder[cell.bind.groupId] = not self.groupFolder[cell.bind.groupId]
      else
        self.groupFolder[cell.bind.groupId] = true
      end
      L_EntrustStore:call(L_EntrustStore.event.entrustTaskGroupFolder, cell.bind.groupId, self.groupFolder[cell.bind.groupId])
    end
    self.allInfo = {}
    local index = 1
    for _, v in ipairs(modeInfo) do
      local levelInfo = {}
      for _, v1 in ipairs(v.levelInfos) do
        if L_EntrustStore:checkEntrustLevelShow(v1.id) then
          table.insert(levelInfo, v1)
        end
      end
      if 0 < #levelInfo then
        table.insert(self.allInfo, {
          index = index,
          levelInfos = levelInfo,
          groupId = v.groupId,
          bFold = self.groupFolder[v.groupId],
          cbk = onClick_select
        })
        index = index + 1
      end
    end
    self:getInitSelectEntrustId()
    for _, v in ipairs(self.allInfo) do
      v.selectEntrustTaskId = self.selectEntrustTaskId
    end
    self.bind.table_allTaskList:clear()
    self.bind.table_allTaskList:insert_array(self.allInfo)
    self:refreshSelectLevelDetails(bChangeTab)
    return
  end
  self.bind.go_allTaskList = false
  self.bind.go_filterTaskList = true
  self.bindComponents.scroll_filtertaskListAnim:Rewind()
  self.bindComponents.scroll_filtertaskListAnim:Play()
  local onClick_select = function(cell)
    local bUnlock = L_EntrustStore:checkEntrustLevelUnlock(cell.bind.levelId)
    if not bUnlock then
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_cellPetDuelLevel"))
      return
    end
    self.selectEntrustTaskId = cell.bind.levelId
    for i = 1, #self.bind.table_filterTaskList do
      self.bind.table_filterTaskList:change(i, {
        selectEntrustTaskId = self.selectEntrustTaskId
      })
    end
    self:refreshSelectLevelDetails(false, true)
  end
  self.filterInfo = {}
  local index = 1
  for _, v in ipairs(modeInfo) do
    for _, v1 in ipairs(v.levelInfos) do
      if L_EntrustStore:checkEntrustLevelShow(v1.id) then
        local cfg = _dungeonEntrustTpl:getTplById(v1.id)
        local tag = _dungeonEntrustTpl:getTag(cfg)
        if tag then
          for _, v in ipairs(tag) do
            if v == self.selectFilterType then
              table.insert(self.filterInfo, {
                index = index,
                levelId = v1.id,
                cbk = onClick_select
              })
              index = index + 1
              break
            end
          end
        end
      end
    end
  end
  table.sort(self.filterInfo, function(a, b)
    local isCompleteA, strTipsA = L_EntrustStore:checkEntrustLevelUnlock(a.levelId)
    local isCompleteB, strTipsB = L_EntrustStore:checkEntrustLevelUnlock(b.levelId)
    if isCompleteA and not isCompleteB then
      return true
    end
    if not isCompleteA and isCompleteB then
      return false
    end
    return a.index < b.index
  end)
  index = 1
  for _, v in ipairs(self.filterInfo) do
    v.index = index
    index = index + 1
  end
  self:getInitSelectEntrustId()
  for _, v in ipairs(self.filterInfo) do
    v.selectEntrustTaskId = self.selectEntrustTaskId
  end
  self.bind.table_filterTaskList:clear()
  self.bind.table_filterTaskList:insert_array(self.filterInfo)
  self:refreshSelectLevelDetails(bChangeTab)
end

function cls:getInitSelectEntrustId()
  self.selectEntrustTaskId = nil
  if self:checkIsSpecialFilter() then
    for i = 1, #self.allInfo do
      local levelInfos = self.allInfo[i].levelInfos
      for j = 1, #levelInfos do
        local bUnlock = L_EntrustStore:checkEntrustLevelUnlock(levelInfos[j].id)
        local bFinished = L_EntrustStore:checkEntrustLevelSuc(levelInfos[j].id)
        if bUnlock and not bFinished then
          self.selectEntrustTaskId = levelInfos[j].id
          return
        end
      end
    end
    if not self.selectEntrustTaskId then
      for i = #self.allInfo, 1, -1 do
        local levelInfos = self.allInfo[i].levelInfos
        for j = #levelInfos, 1, -1 do
          local bUnlock = L_EntrustStore:checkEntrustLevelUnlock(levelInfos[j].id)
          if bUnlock then
            self.selectEntrustTaskId = levelInfos[j].id
            return
          end
        end
      end
    end
    if not self.selectEntrustTaskId then
    end
    return
  end
  for i = 1, #self.filterInfo do
    local bUnlock = L_EntrustStore:checkEntrustLevelUnlock(self.filterInfo[i].levelId)
    local bFinished = L_EntrustStore:checkEntrustLevelSuc(self.filterInfo[i].levelId)
    if bUnlock and not bFinished then
      self.selectEntrustTaskId = self.filterInfo[i].levelId
      return
    end
  end
  if not self.selectEntrustTaskId then
    for i = #self.filterInfo, 1, -1 do
      local bUnlock = L_EntrustStore:checkEntrustLevelUnlock(self.filterInfo[i].levelId)
      if bUnlock then
        self.selectEntrustTaskId = self.filterInfo[i].levelId
        return
      end
    end
  end
  if not self.selectEntrustTaskId then
  end
end

function cls:refreshSelectLevelDetails(bChangeTab, bChangeLevel)
  local moduleEntrustLevelDetails = self.modules.moduleEntrustLevelDetails
  if not self.selectEntrustTaskId then
    moduleEntrustLevelDetails:setShow(false)
    return
  end
  moduleEntrustLevelDetails:setShow(true)
  moduleEntrustLevelDetails:initAreaEntrustDetails(self.selectEntrustTaskId, bChangeTab, bChangeLevel)
end

function cls:onEvent_entrustLevelSelect(levelId)
  if not self:checkIsSpecialFilter() then
    return
  end
  self.selectEntrustTaskId = levelId
  for i = 1, #self.bind.table_allTaskList do
    self.bind.table_allTaskList:change(i, {
      selectEntrustTaskId = self.selectEntrustTaskId
    })
  end
  self:refreshSelectLevelDetails(false, true)
end

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

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

function cls:close(options)
  cls.super.close(self, options)
  L_EntrustStore:unListenCallFunc(L_EntrustStore.event.entrustRewardRefresh, self.onEvent_entrustRewardRefresh, self)
  L_EntrustStore:unListenCallFunc(L_EntrustStore.event.entrustLevelSelect, self.onEvent_entrustLevelSelect, self)
end

function cls:unBind()
  cls.super.unBind(self)
end

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

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

return cls
