local this = class("moduleAnnounceTypeList", G_UIModuleBase)

function this.bind()
  return {
    scrollList_type = {
      moduleName = "pages/announcement/cellAnnouncementType"
    }
  }
end

function this.methods()
  return {
    scrollList_type = {
      onClick_select = function(self, type)
        if self.selectType == type then
          return
        end
        if self.checkTypeFunc then
          self.checkTypeFunc(type)
        end
        self.selectType = type
        self:setTypeListActive()
      end
    }
  }
end

function this:initModule(typeList, announcementShowType, checkTypeFunc)
  self.announcementShowType = announcementShowType
  self.checkTypeFunc = checkTypeFunc
  local defaultSelectType, firstSelectType
  for i, v in pairs(typeList) do
    local isShow = true
    if v.type == L_Const.AnnouncementType.System then
    elseif announcementShowType == L_Const.AnnouncementShowType.OnlySystem then
      isShow = false
    end
    v.isShow = isShow
    if isShow then
      if firstSelectType == nil then
        firstSelectType = v.type
      end
      if defaultSelectType == nil then
        local state = L_ReddotManager:getRedDotState(L_ReddotManager.DotDef.AnnouncementActive, "Anncouncement_" .. v.type)
        if state then
          defaultSelectType = v.type
        end
      end
    end
  end
  if defaultSelectType == nil then
    defaultSelectType = firstSelectType
  end
  self:initTypeList(typeList)
  if self.selectType ~= defaultSelectType then
    self.selectType = defaultSelectType
    if self.checkTypeFunc then
      self.checkTypeFunc(self.selectType)
    end
  end
  self:setTypeListActive()
end

function this:getSelectType()
  if self.selectType == nil then
    return L_Const.AnnouncementType.Active
  end
  return self.selectType
end

function this:initTypeList(typeList)
  local tmp = {}
  for i, v in pairs(typeList) do
    table.insert(tmp, {
      type = v.type,
      isShow = v.isShow
    })
  end
  self.bind.scrollList_type:clear()
  self.bind.scrollList_type:insert_array(tmp)
end

function this:setTypeListActive()
  for i = 1, #self.bind.scrollList_type do
    local active = false
    if self.bind.scrollList_type:getValue(i, "type") == self.selectType then
      active = true
    end
    self.bind.scrollList_type:change(i, {
      go_active = active,
      go_normal = not active
    })
  end
end

function this:checkShowRedDot()
  return self.parent:checkShowRedDot()
end

return this
