local this = class("pageAnnouncement", G_UIPageBase)
local AnnouncementModuleTypeName = {
  [L_Const.AnnouncementModuleType.Empty] = "moduleAnnouncementEmpty",
  [L_Const.AnnouncementModuleType.Poster] = "moduleAnnouncementPoster",
  [L_Const.AnnouncementModuleType.RichText] = "moduleAnnouncementRichText",
  [L_Const.AnnouncementModuleType.Webview] = "moduleAnnouncementWebview"
}

function this.bind()
  return {
    announcementModuleBar = {
      type = "toggleModule",
      moduleAnnouncementEmpty = {
        assetName = "UI/Pages/Announcement/moduleAnnouncementEmpty",
        moduleName = "pages/announcement/moduleAnnouncementEmpty"
      },
      moduleAnnouncementPoster = {
        assetName = "UI/Pages/Announcement/moduleAnnouncementPoster",
        moduleName = "pages/announcement/moduleAnnouncementPoster"
      },
      moduleAnnouncementRichText = {
        assetName = "UI/Pages/Announcement/moduleAnnouncementRichText",
        moduleName = "pages/announcement/moduleAnnouncementRichText"
      },
      moduleAnnouncementWebview = {
        assetName = "UI/Pages/Announcement/moduleAnnouncementWebview",
        moduleName = "pages/announcement/moduleAnnouncementWebview"
      }
    },
    announcementModuleName = "",
    moduleAnnounceTypeList = {
      moduleName = "pages/announcement/moduleAnnounceTypeList"
    },
    moduleAnnounceScrollView = {
      moduleName = "pages/announcement/moduleAnnounceScrollView"
    },
    scrollAnchoredPosition = C_Vector2(0, 0),
    go_emptyScroll = false,
    go_tab = true,
    pos_main = C_Vector2(0, 0),
    year_label = string.empty,
    year_label2 = string.empty,
    day_label = string.empty,
    go_haveAnnounce = false,
    fullScreenBlur = false,
    partScreenBlur = false,
    partScreenBlurRange = false,
    go_year_label2 = false,
    go_select = false,
    pos_select = C_Vector2(0, 0)
  }
end

function this.methods()
  return {
    closeBtn = function(self)
      L_UI:close("pageAnnouncement")
      L_UI:close("pageAnnouncementLogin")
    end,
    onClick_bg = function(self)
      L_UI:close("pageAnnouncement")
      L_UI:close("pageAnnouncementLogin")
    end
  }
end

function this:checkShowRedDot()
  return self.announcementShowType == L_Const.AnnouncementShowType.Normal
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  self.announcementShowType = L_Const.AnnouncementShowType.Normal
  if options and options.announcementShowType then
    self.announcementShowType = options.announcementShowType
  end
  local isNorm = self.announcementShowType == L_Const.AnnouncementShowType.Normal
  self.bind.go_tab = isNorm
  self.bind.fullScreenBlur = not isNorm
  self.bind.partScreenBlur = isNorm
  self.bind.partScreenBlurRange = isNorm
  if false and self.announcementShowType == L_Const.AnnouncementShowType.Normal then
    self.bind.pos_main = self.bindComponents.posNormal.anchoredPosition
  else
    self.bind.pos_main = self.bindComponents.posOnlySystem.anchoredPosition
    L_LoginManager:getAnnouncementOnLogin(function(data, code)
      L_AnnouncementStore:syncAnnouncementDatasByHttp(data, code)
    end)
  end
  self.onSelectAnnouncementTypeHandler = handler(self, self.onSelectAnnouncementType)
  self.onSelectAnnouncementTitleHandler = handler(self, self.onSelectAnnouncementTitle)
  L_AnnouncementStore:listenCallFunc(L_AnnouncementStore.event.announcementClear, self.onEvent_announcementClear, self)
  L_AnnouncementStore:listenCallFunc(L_AnnouncementStore.event.announcementEnd, self.onEvent_announcementEnd, self)
  L_AnnouncementStore:listenCallFunc(L_AnnouncementStore.event.announcementAdd, self.onEvent_announcementAdd, self)
  self:initPage()
end

function this:open()
  this.super.open(self)
  self:showCurrentDate()
end

function this:close(options)
  this.super.close(self, options)
  L_AnnouncementStore:unListenCallFunc(L_AnnouncementStore.event.announcementClear, self.onEvent_announcementClear, self)
  L_AnnouncementStore:unListenCallFunc(L_AnnouncementStore.event.announcementEnd, self.onEvent_announcementEnd, self)
  L_AnnouncementStore:unListenCallFunc(L_AnnouncementStore.event.announcementAdd, self.onEvent_announcementAdd, self)
  L_AudioUtil.playSound("Play_SFX_System_UI_Post_Menu_Close")
end

function this:initPage()
  self:initModuleAnnounceTypeList()
  L_AudioUtil.playSound("Play_SFX_System_UI_Post_Menu_Open")
end

function this:showCurrentDate()
  local svrTs = L_TimeUtil.getServerTime()
  local dateTime = L_TimeUtil.getServerTimeOfDateTime(svrTs)
  self.bind.year_label = tostring(dateTime.Year)
  self.bind.day_label = string.format("%.2d", dateTime.Month) .. string.format("%.2d", dateTime.Day)
  self.bind.year_label2 = "\226\128\148" .. tostring(dateTime.Year) .. "\226\128\148"
end

function this:onEvent_announcementClear()
  local defaultType = self.modules.moduleAnnounceTypeList:getSelectType()
  local defaultId = self.modules.moduleAnnounceScrollView:getSelectTitleId()
  self:onSelectAnnouncementType(defaultType, defaultId)
end

function this:onEvent_announcementAdd(list, refreshList)
  local defaultType = self.modules.moduleAnnounceTypeList:getSelectType()
  local needRefresh = false
  for i, data in pairs(list) do
    if data and data:IsValid() and defaultType == data:getAnnouncementTab() then
      needRefresh = true
      break
    end
  end
  if refreshList ~= nil then
    for _, id in pairs(refreshList) do
      local data = L_AnnouncementStore:getAnnouncementDataById(id)
      if data ~= nil and data:getAnnouncementType() == L_Const.AnnouncementModuleType.RichText then
        local richText = self.modules.announcementModuleBar[AnnouncementModuleTypeName[L_Const.AnnouncementModuleType.RichText]]
        richText:clearHtmlCache(data:getContent())
      end
    end
  end
  if not needRefresh then
    return
  end
  local defaultId = self.modules.moduleAnnounceScrollView:getSelectTitleId()
  self:onSelectAnnouncementType(defaultType, defaultId)
end

function this:onEvent_announcementEnd(list)
  local defaultType = self.modules.moduleAnnounceTypeList:getSelectType()
  local needRefresh = false
  for i, data in pairs(list) do
    if defaultType == data:getAnnouncementTab() then
      needRefresh = true
      break
    end
  end
  if not needRefresh then
    return
  end
  local defaultId = self.modules.moduleAnnounceScrollView:getSelectTitleId()
  for i, data in pairs(list) do
    print("inori delete " .. data:getId())
  end
  self:onSelectAnnouncementType(defaultType, defaultId)
end

function this:initModuleAnnounceTypeList()
  local typeList = {
    L_Const.AnnouncementType.Active,
    L_Const.AnnouncementType.System,
    L_Const.AnnouncementType.Info
  }
  self.menuList = {}
  for i, v in pairs(typeList) do
    local data = {type = v}
    table.insert(self.menuList, data)
  end
  self.modules.moduleAnnounceTypeList:initModule(self.menuList, self.announcementShowType, self.onSelectAnnouncementTypeHandler)
end

function this.sortAnnouncementData(a, b)
  local dataA = L_AnnouncementStore:getAnnouncementDataById(a.id)
  local dataB = L_AnnouncementStore:getAnnouncementDataById(b.id)
  if dataA == nil then
    return false
  end
  if dataB == nil then
    return true
  end
  if dataA:getOrderId() ~= dataB:getOrderId() then
    return dataA:getOrderId() > dataB:getOrderId()
  end
  if dataA:getPublishTime() ~= dataB:getPublishTime() then
    return dataA:getPublishTime() > dataB:getPublishTime()
  end
  return dataA:getId() > dataB:getId()
end

function this:onSelectAnnouncementType(t, defaultId)
  self.tmp = {}
  local datas = L_AnnouncementStore:getAnnouncementDatas()
  local haveValue = false
  for i, v in pairs(datas) do
    if v:getAnnouncementTab() == t and v:IsValid() then
      table.insert(self.tmp, {
        id = v:getId()
      })
      if defaultId == v:getId() then
        haveValue = true
      end
    end
  end
  table.sort(self.tmp, self.sortAnnouncementData)
  local virtualData
  if haveValue then
  else
    local data = L_AnnouncementStore:getAnnouncementDataById(defaultId)
    defaultId = nil
    if data ~= nil then
      if data:getAnnouncementTab() == t then
        virtualData = {
          id = data:getId()
        }
      else
        virtualData = nil
      end
    end
    local index = 1
    if virtualData then
      for i = 1, #self.tmp do
        local data = self.tmp[i]
        if self.sortAnnouncementData(data, virtualData) then
          index = i
        else
          index = i
          break
        end
      end
    end
    if self.tmp[index] then
      defaultId = self.tmp[index].id
    end
  end
  self:resetSelectObj()
  self.modules.moduleAnnounceScrollView:initModule(self.tmp, defaultId, self.onSelectAnnouncementTitleHandler)
  if #self.tmp <= 0 then
    self:onSelectAnnouncementTitle(defaultId)
  end
  if not haveValue and virtualData == nil then
    self.bind.scrollAnchoredPosition = C_Vector2(0, 0)
  end
  local haveAnnounce = #self.tmp > 0
  self.bind.go_emptyScroll = not haveAnnounce
  self.bind.go_haveAnnounce = haveAnnounce
end

function this:onSelectAnnouncementTitle(index)
  local data = L_AnnouncementStore:getAnnouncementDataById(index)
  local name = L_Const.AnnouncementModuleType.Empty
  if data ~= nil and data:IsValid() then
    name = data:getAnnouncementType()
  end
  if AnnouncementModuleTypeName[name] == nil then
    name = L_Const.AnnouncementModuleType.Empty
  end
  self.bind.go_year_label2 = name == L_Const.AnnouncementModuleType.Poster
  self.bind.announcementModuleName = AnnouncementModuleTypeName[name]
  self.modules.announcementModuleBar[AnnouncementModuleTypeName[name]]:initData(data, self.announcementShowType)
  if self:checkShowRedDot() then
    L_AnnouncementStore:setAnnouncementIsRead(index)
  end
end

function this:resetSelectObj()
  if #self.tmp == 0 then
    self.bind.go_select = false
  else
    self.bind.go_select = true
    self.bind.pos_select = C_Vector2(230.6, -60)
  end
end

function this:check(options, callback)
  options = options or {}
  if options.fromLogin then
    callback(true)
    return
  end
  local result = L_SystemUnlockManager:checkAndTip(L_SystemConst.enum.announcemenet)
  callback(result)
end

return this
