local cls = class("cellEntrustTask", G_UIModuleBase)
local _dungeonEntrustTpl = L_GameTpl:getDungeonEntrustTpl()
local StarIconEnum = {
  [3] = "UI/Atlas/EntrustTask/tex_dungeonentrust_icon_wz06.png",
  [2] = "UI/Atlas/EntrustTask/tex_dungeonentrust_icon_wz05.png",
  [1] = "UI/Atlas/EntrustTask/tex_dungeonentrust_icon_wz04.png"
}

function cls.bind()
  return {
    selectTab = -1,
    img_starNum = "",
    txt_levelName = "",
    toggle_unlock = true,
    list_levelTag = {
      moduleName = "pages/entrustTask/cellEntrustTaskTag"
    },
    txt_unlockCondition = "",
    go_select = false
  }
end

function cls.methods()
  return {
    onClick_select = function(self)
      if self.bind.cbk then
        self.bind.cbk(self)
      end
    end
  }
end

function cls:refresh()
  self.dungeonEntrustCfg = _dungeonEntrustTpl:getTplById(self.bind.levelId)
  self.bind.txt_levelName = _dungeonEntrustTpl:getName(self.dungeonEntrustCfg)
  self:refreshUnlockCondition()
  self:refreshFinishStatus()
  self:refreshTagList()
  self:setSelected(self.bind.levelId == self.bind.selectEntrustTaskId)
end

function cls:refreshFinishStatus()
  if not self.unlock then
    return
  end
  local starNum = L_EntrustStore:getEntrustLevelStar(self.bind.levelId)
  self.bind.selectTab = 0 < starNum and 1 or 2
  if 0 < starNum then
    self.bind.img_starNum = StarIconEnum[starNum]
  end
end

function cls:refreshTagList()
  local info = {}
  local tag = _dungeonEntrustTpl:getTag(self.dungeonEntrustCfg)
  if tag then
    for _, v in ipairs(tag) do
      table.insert(info, {id = v})
    end
  end
  self.bind.list_levelTag:clear()
  self.bind.list_levelTag:insert_array(info)
end

function cls:refreshUnlockCondition()
  local isComplete, strTips = L_EntrustStore:checkEntrustLevelUnlock(self.bind.levelId)
  self.unlock = isComplete
  if not isComplete then
    self.bind.selectTab = 3
  end
  self.bind.toggle_unlock = isComplete
  if not isComplete then
    self.bind.txt_unlockCondition = strTips
  end
end

function cls:setSelected(bSelect)
  self.bind.go_select = bSelect
end

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

return cls
