local cls = class("pageEntrustEnemyDetail", G_UIPageBase)
local _dungeonTpl = L_GameTpl:getDungeonTpl()
local _enemyTpl = L_GameTpl:getEnemyTpl()
local _kiBoDuelTpl = L_GameTpl:getKiboDuelTpl()
local _petTpl = L_GameTpl:getPetTpl()

function cls.bind()
  return {
    list_enemy = {
      moduleName = "pages/entrustTask/cellEntrustEnemy"
    },
    txt_enemyName = "",
    txt_enemyLevel = "",
    txt_enemyDesc = "",
    module_commonTipLarge = L_Const.ModuleInfo.CommonTipLarge
  }
end

function cls.methods()
  return {
    module_commonTipLarge = {
      onClick_bg = function(self)
        L_UI:close(self.pageName)
      end,
      onClick_close = function(self)
        L_UI:close(self.pageName)
      end
    }
  }
end

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

function cls:open(options)
  self.entrustType = options.entrustType
  self.dungeonId = options.dungeonId
  self.selectIndex = 1
  self:initCommonTipModule()
  self:initPage()
end

function cls:initCommonTipModule()
  local moduleTip = self.modules.module_commonTipLarge
  moduleTip:initModule({
    txtTitle = L_WordsTpl:getValue("ui_entrust_enemy_message"),
    hideConfirm = true,
    hideCancel = true
  })
end

function cls:initPage()
  local onClick_select = function(cell)
    self.selectIndex = cell.bind.index
    for i = 1, #self.bind.list_enemy do
      self.bind.list_enemy:change(i, {
        selectIndex = self.selectIndex
      })
    end
    self:refreshSelectInfo()
  end
  if self.entrustType == L_Const.EntrustType.PetDuel then
    local curKiBoDuelCfg = _kiBoDuelTpl:getTplById(self.dungeonId)
    self.enemyInfo = {}
    for k, v in pairs(_kiBoDuelTpl:getEnemy(curKiBoDuelCfg)) do
      table.insert(self.enemyInfo, {
        selectIndex = self.selectIndex,
        id = k,
        level = v,
        cbk = onClick_select,
        entrustType = L_Const.EntrustType.PetDuel
      })
    end
    table.sort(self.enemyInfo, function(a, b)
      return a.id < b.id
    end)
    for index, v in ipairs(self.enemyInfo) do
      v.index = index
    end
    self.bind.list_enemy:clear()
    self.bind.list_enemy:insert_array(self.enemyInfo)
    self:refreshSelectInfo()
    return
  end
  local cfg = _dungeonTpl:getTplById(self.dungeonId)
  local enemyList = _dungeonTpl:getEnemy(cfg)
  self.enemyInfo = {}
  for i, v in ipairs(enemyList) do
    local id, level = table.unpack(v)
    table.insert(self.enemyInfo, {
      selectIndex = self.selectIndex,
      index = i,
      id = id,
      level = _dungeonTpl:getDiffType(cfg),
      cbk = onClick_select
    })
  end
  self.bind.list_enemy:clear()
  self.bind.list_enemy:insert_array(self.enemyInfo)
  self:refreshSelectInfo()
end

function cls:refreshSelectInfo()
  if self.entrustType == L_Const.EntrustType.PetDuel then
    local petCfgId = self.enemyInfo[self.selectIndex].id
    local petCfg = _petTpl:getTplById(petCfgId)
    self.bind.txt_enemyName = _petTpl:getName(petCfg)
    self.bind.txt_enemyLevel = string.format("Lv.%s", self.enemyInfo[self.selectIndex].level)
    self.bind.txt_enemyDesc = _petTpl:getDesc(petCfg)
    return
  end
  local enemyId = self.enemyInfo[self.selectIndex].id
  local enemyCfg = _enemyTpl:getTplById(enemyId)
  self.bind.txt_enemyName = _enemyTpl:getName(enemyCfg)
  self.bind.txt_enemyLevel = string.format("Lv.%s", self.enemyInfo[self.selectIndex].level)
  self.bind.txt_enemyDesc = _enemyTpl:getDesc(enemyCfg)
end

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

return cls
