local this = class("cellHomeCollectionTag", G_UIModuleBase)
local homeCollectionWorldTpl = L_GameTpl:getHomeCollectionWorldTpl()

function this.bind()
  return {
    tagColor = C_Color(0, 0, 0, 1),
    tagName = "",
    collectionList = {
      moduleName = "pages/home/homeCollectionManage/cellHomeCollection"
    }
  }
end

function this.methods()
  return {
    collectionList = {
      onClick_choose = function(self, bind)
        self:emit("onClick_choose", bind)
        for i, v in pairs(self.bind.collectionList) do
          if bind.guid == v.guid then
            self.bind.collectionList:change(i, {isChoose = true})
          else
            self.bind.collectionList:change(i, {isChoose = false})
          end
        end
        self:emit("onClick_select", self.bind)
      end
    }
  }
end

function this:open()
  self:initData()
end

function this:refresh()
  self:refreshData()
end

function this:initData()
  if self.bind.data then
    local tmp = {}
    for i, v in ipairs(self.bind.data) do
      local collectionId = homeCollectionWorldTpl:getId(v)
      local targetId = self.parent._collectionGuid
      local hasDefaultId = false
      local isChoosed = false
      if not math.isEmpty(targetId) then
        hasDefaultId = true
      end
      if hasDefaultId then
        isChoosed = self.bind.activeSelect and targetId == collectionId and true or false
      else
        isChoosed = self.bind.activeSelect and i == 1 and true or false
      end
      table.insert(tmp, {
        data = v,
        guid = collectionId,
        isChoose = isChoosed
      })
      if isChoosed then
        self:emit("onClick_choose", tmp[i])
      end
    end
    self.bind.collectionList:clear()
    self.bind.collectionList:insert_array(tmp)
  end
end

function this:refreshData()
  self.bind.tagColor = self.bind.activeSelect == true and C_Color(0, 0, 0, 1) or C_Color(0, 0, 0, 0.6)
  if not self.bind.activeSelect then
    for i, v in ipairs(self.bind.collectionList) do
      self.bind.collectionList:change(i, {isChoose = false})
    end
  end
end

return this
