local cls = class("moduleEntrustLevelDetails", G_UIModuleBase)
local _dungeonEntrustTpl = L_GameTpl:getDungeonEntrustTpl()
local _dungeonTpl = L_GameTpl:getDungeonTpl()
local _trainTaskTpl = L_GameTpl:getTrainTaskTpl()
local _commonConditionTpl = L_GameTpl:getCommonConditionTpl()
local _kibiDuelTpl = L_GameTpl:getKiboDuelTpl()
local StarIconEnum = {
  [3] = "UI/Atlas/EntrustTask/tex_dungeonentrust_icon_wz03.png",
  [2] = "UI/Atlas/EntrustTask/tex_dungeonentrust_icon_wz02.png",
  [1] = "UI/Atlas/EntrustTask/tex_dungeonentrust_icon_wz01.png"
}

function cls.bind()
  return {
    active_imgIndicator = false,
    go_finished = false,
    img_finishedStar = "",
    txt_entrustName = "",
    txt_recommendLevel = "",
    active_btnEnemyInfo = false,
    active_listCondition = false,
    list_entrustTaskCondition = {
      moduleName = "pages/entrustTask/cellEntrustTaskCondition"
    },
    active_scrollDesc = false,
    txt_scrollDesc = "",
    active_Desc = false,
    txt_Desc = "",
    pos_Desc = C_Vector3.zero,
    active_npcNameNode = false,
    txt_npcName = "",
    active_consumeTipsNode = false,
    txt_energyConsume = "",
    sizeDelta_titleBg = C_Vector2.zero,
    table_taskReward = {
      moduleName = "pages/entrustTask/cellEntrustTaskReward"
    },
    txt_btnName = ""
  }
end

function cls.methods()
  return {
    onClick_openEnemyInfo = function(self)
      if self.entrustType == L_Const.EntrustType.PetDuel then
        L_UI:open("pageEntrustEnemyDetail", {
          dungeonId = self.levelData.id,
          entrustType = L_Const.EntrustType.PetDuel
        })
      elseif self.entrustType == L_Const.EntrustType.AreaEntrust then
        local dungeonEntrustCfg = _dungeonEntrustTpl:getTplById(self.levelId)
        L_UI:open("pageEntrustEnemyDetail", {
          dungeonId = _dungeonEntrustTpl:getDungeonId(dungeonEntrustCfg),
          entrustType = L_Const.EntrustType.AreaEntrust
        })
      end
    end,
    onClick_challenge = function(self)
      if self.entrustType == L_Const.EntrustType.AreaEntrust then
        local condId = AzurWorld.WorldCondMgr:CreateCond({70001}, nil)
        if not AzurWorld.WorldCondMgr:CheckCond(condId) then
          local cond = AzurWorld.WorldCondMgr:GetCond(condId)
          if cond then
            local str = cond:ShowMsg()
            L_FlyMsgManager:showNormalMsg(str)
          end
          return
        end
        L_EntrustStore:setCurChallengeEntrustId(self.levelId)
        L_EntrustStore:req_enterEntrustLevel(self.levelId)
      elseif self.entrustType == L_Const.EntrustType.TrainTask then
        self:checkAndGo()
      elseif self.entrustType == L_Const.EntrustType.PetDuel then
        local curStamina = L_PlayerStore:getCurrencyNum(L_Const.currencyType.stamina)
        if curStamina >= self.levelData.stamina then
          L_UI:open("pagePetPrepareDuel", {
            levelId = self.levelData.id
          })
        else
          L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_dungeon_lackenergy"))
          L_UI:open("pageStamina")
        end
      end
    end
  }
end

function cls:setShow(bShow)
  L_CommonUtil.setObjActive(self.gameObject, bShow)
end

function cls:initAreaEntrustDetails(levelId, bChangeTab, bChangeLevel)
  local func = function()
    self.entrustType = L_Const.EntrustType.AreaEntrust
    self.levelId = levelId
    self.bind.txt_btnName = "\230\142\165\229\143\151\229\167\148\230\137\152"
    local dungeonEntrustCfg = _dungeonEntrustTpl:getTplById(self.levelId)
    self.bind.txt_recommendLevel = string.format("\229\189\147\229\137\141\229\133\179\229\141\161\229\187\186\232\174\174 LV.%s", _dungeonEntrustTpl:getRecommendLevel(dungeonEntrustCfg))
    self.bind.txt_entrustName = _dungeonEntrustTpl:getName(dungeonEntrustCfg)
    self.bind.active_btnEnemyInfo = true
    self.bind.active_scrollDesc = true
    self.bind.txt_scrollDesc = _dungeonEntrustTpl:getIntroduce(dungeonEntrustCfg)
    self.bind.active_Desc = false
    self.bind.active_npcNameNode = true
    self.bind.txt_npcName = _dungeonEntrustTpl:getEntrustNpcName(dungeonEntrustCfg)
    local starNum = L_EntrustStore:getEntrustLevelStar(self.levelId)
    self.bind.go_finished = 0 < starNum
    self.bind.active_imgIndicator = 0 < starNum
    if 0 <= starNum then
      self.bind.img_finishedStar = StarIconEnum[starNum]
    end
    self.bind.active_consumeTipsNode = true
    self.staminaConsume = _dungeonEntrustTpl:getEnergyConsume(dungeonEntrustCfg)
    self.bind.txt_energyConsume = tostring(self.staminaConsume)
    self.bind.sizeDelta_titleBg = C_Vector2(384, 32)
    self.bind.active_listCondition = true
    self:refreshAreaEntrustTaskCondition(dungeonEntrustCfg)
    self:refreshAreaEntrustTaskReward(dungeonEntrustCfg)
  end
  if bChangeTab then
    self.bindComponents.rootAnim:Stop()
    self.bindComponents.boardAnim:Rewind()
    self.bindComponents.boardAnim:Play("anim_dungeon_formula_change")
    Timer.once(0.13, function()
      func()
    end, self, self.gameObject)
  elseif bChangeLevel then
    self.bindComponents.rootAnim:Stop()
    self.bindComponents.boardAnim:Rewind()
    self.bindComponents.boardAnim:Play("anim_dungeon_formula_change1")
    Timer.once(0.066, function()
      func()
    end, self, self.gameObject)
  else
    func()
  end
end

function cls:refreshAreaEntrustTaskCondition(dungeonEntrustCfg)
  local starNum = L_EntrustStore:getEntrustLevelStar(self.levelId)
  local dungeonId = _dungeonEntrustTpl:getDungeonId(dungeonEntrustCfg)
  local dungeonCfg = _dungeonTpl:getTplById(dungeonId)
  local dungeonTaskDecs = _dungeonTpl:getChallengeDes(dungeonCfg)
  local info = {}
  for i, v in ipairs(dungeonTaskDecs) do
    table.insert(info, {
      txt_condition = v,
      bFinish = i <= starNum
    })
  end
  self.bind.list_entrustTaskCondition:clear()
  self.bind.list_entrustTaskCondition:insert_array(info)
end

function cls:refreshAreaEntrustTaskReward(dungeonEntrustCfg)
  local rewardItems = {}
  local starNum = L_EntrustStore:getEntrustLevelStar(self.levelId)
  if starNum < 3 then
    local reward = _dungeonEntrustTpl:getThreeStarRewardShow(dungeonEntrustCfg)
    for i = 1, #reward do
      local item = L_ItemTplManager:getItemConfig(reward[i].itemType, reward[i].itemId)
      if item then
        table.insert(rewardItems, {
          itemType = item.itemType,
          itemId = item.itemId,
          num = reward[i].itemNum,
          starNum = 3
        })
      end
    end
  end
  if starNum < 2 then
    local reward = _dungeonEntrustTpl:getTwoStarRewardShow(dungeonEntrustCfg)
    for i = 1, #reward do
      local item = L_ItemTplManager:getItemConfig(reward[i].itemType, reward[i].itemId)
      if item then
        table.insert(rewardItems, {
          itemType = item.itemType,
          itemId = item.itemId,
          num = reward[i].itemNum,
          starNum = 2
        })
      end
    end
  end
  if starNum < 1 then
    local reward = _dungeonEntrustTpl:getOneStarRewardShow(dungeonEntrustCfg)
    for i = 1, #reward do
      local item = L_ItemTplManager:getItemConfig(reward[i].itemType, reward[i].itemId)
      if item then
        table.insert(rewardItems, {
          itemType = item.itemType,
          itemId = item.itemId,
          num = reward[i].itemNum,
          starNum = 1
        })
      end
    end
  end
  local reward = _dungeonEntrustTpl:getRewardShow(dungeonEntrustCfg)
  for i = 1, #reward do
    local item = L_ItemTplManager:getItemConfig(reward[i].itemType, reward[i].itemId)
    if item then
      table.insert(rewardItems, {
        itemType = item.itemType,
        itemId = item.itemId,
        itemNumTxt = reward[i].itemNumTxt,
        starNum = 0
      })
    end
  end
  self.bind.table_taskReward:clear()
  self.bind.table_taskReward:insert_array(rewardItems)
end

function cls:initTrainTaskDetails(selectType, taskId)
  self._selectType = selectType
  self._selectTaskId = taskId
  self.entrustType = L_Const.EntrustType.TrainTask
  self.bind.txt_btnName = "\229\137\141\229\190\128\230\149\153\229\173\166"
  self.bind.txt_recommendLevel = "\228\187\187\229\138\161\228\191\161\230\129\175"
  local tpl_task = _trainTaskTpl:getTplById(taskId)
  self.bind.txt_entrustName = _trainTaskTpl:getTaskName(tpl_task)
  self.bind.active_btnEnemyInfo = false
  self.bind.active_scrollDesc = false
  self.bind.active_Desc = true
  self.bind.txt_Desc = _trainTaskTpl:getDesc(tpl_task)
  self.bind.pos_Desc = C_Vector3(-35.607, 265, 0)
  self.bind.active_npcNameNode = false
  self.bind.go_finished = false
  self.bind.active_consumeTipsNode = false
  self.bind.sizeDelta_titleBg = C_Vector2(621.2, 32)
  self.bind.active_listCondition = false
  self:refreshTrainTaskReward(taskId)
end

function cls:refreshTrainTaskReward(taskId)
  local completed = L_EntrustStore:getIsTrainTaskCompleted(taskId)
  local tpl_task = _trainTaskTpl:getTplById(taskId)
  local rewardList = {}
  if self._selectType == L_Const.trainTaskType.dungeon then
    rewardList = _trainTaskTpl:getPassReward(tpl_task)
  elseif self._selectType == L_Const.trainTaskType.kiBoDuel then
    local kibo_duel_id = _trainTaskTpl:getDungeonId(tpl_task)
    local tpl_kibo_duel = _kibiDuelTpl:getTplById(kibo_duel_id)
    rewardList = _kibiDuelTpl:getFirstLoot(tpl_kibo_duel)
  end
  local rewardItems = {}
  for i = 1, #rewardList do
    local item = L_ItemTplManager:getItemConfig(rewardList[i].itemType, rewardList[i].itemId)
    table.insert(rewardItems, {
      itemType = item.itemType,
      itemId = item.itemId,
      num = rewardList[i].itemNum,
      active_img_receive = completed
    })
  end
  self.bind.table_taskReward:clear()
  self.bind.table_taskReward:insert_array(rewardItems)
end

function cls:checkAndGo()
  if self._selectType == L_Const.trainTaskType.dungeon then
    L_EntrustStore:req_CSProtoTeachStart(self._selectTaskId)
  elseif self._selectType == L_Const.trainTaskType.kiBoDuel then
    local tpl_task = _trainTaskTpl:getTplById(self._selectTaskId)
    local levelId = _trainTaskTpl:getDungeonId(tpl_task)
    L_PetDuelStore:setCurChallengeLevelId(levelId)
    C_KiBoDuelLuaWrapper.SetCurrentLevel(levelId)
    L_PetDuelStore:req_CSProtoKiboDuelStart(levelId)
  end
end

function cls:initEntrustBattleTaskDetails(levelData)
  self.entrustType = L_Const.EntrustType.PetDuel
  self.levelData = levelData
  self.bind.txt_btnName = "\229\137\141\229\190\128\229\175\185\229\134\179"
  self.bind.txt_recommendLevel = "\228\187\187\229\138\161\228\191\161\230\129\175"
  self.bind.txt_entrustName = self.levelData.level_name
  local starNum = self.levelData.complete_star
  self.bind.go_finished = 0 < starNum
  self.bind.active_imgIndicator = 0 < starNum
  if 0 <= starNum then
    self.bind.img_finishedStar = StarIconEnum[starNum]
  end
  self.bind.active_btnEnemyInfo = true
  self.bind.active_scrollDesc = false
  self.bind.active_Desc = true
  self.bind.txt_Desc = self.levelData.desc
  self.bind.pos_Desc = C_Vector3(-35.607, 47.965, 0)
  self.bind.active_npcNameNode = true
  self.bind.txt_npcName = self.levelData.npc_name
  local starNum = L_EntrustStore:getEntrustLevelStar(self.levelId)
  self.bind.go_finished = 0 < starNum
  self.bind.active_imgIndicator = 0 < starNum
  if 0 <= starNum then
    self.bind.img_finishedStar = StarIconEnum[starNum]
  end
  self.bind.active_consumeTipsNode = true
  self.bind.txt_energyConsume = tostring(self.levelData.stamina)
  self.bind.sizeDelta_titleBg = C_Vector2(384, 32)
  self.bind.active_listCondition = true
  self:refreshEntrustBattleTaskCondition()
  self:refreshEntrustBattleTaskReward()
end

function cls:refreshEntrustBattleTaskCondition()
  local conditionList = {}
  local descS = L_ConditionManager:getDescs(self.levelData.challenge_list)
  for i, v in ipairs(descS) do
    table.insert(conditionList, {
      txt_condition = v,
      bFinish = i <= self.levelData.complete_star
    })
  end
  self.bind.list_entrustTaskCondition:clear()
  self.bind.list_entrustTaskCondition:insert_array(conditionList)
end

function cls:refreshEntrustBattleTaskReward()
  local rewardList = {}
  for i, reward in pairs(self.levelData.first_loot_list) do
    if i > self.levelData.complete_star then
      table.insert(rewardList, {
        itemType = reward.itemType,
        itemId = reward.itemId,
        num = reward.itemNum,
        starNum = i
      })
    end
  end
  table.sort(rewardList, function(a, b)
    return a.starNum > b.starNum
  end)
  for i, reward in pairs(self.levelData.challenge_reward_show_list) do
    table.insert(rewardList, {
      itemType = reward.itemType,
      itemId = reward.itemId,
      num = reward.itemNum,
      starNum = 0
    })
  end
  self.bind.table_taskReward:clear()
  self.bind.table_taskReward:insert_array(rewardList)
end

function cls:close(options)
  cls.super.close(self, options)
end

return cls
