local this = class("moduleAnnouncementRichText", G_UIModuleBase)
local replaceTable = {
  {"&lt;", "<"},
  {"&gt;", ">"},
  {"&#34;", "\""},
  {"<br>", "\n"},
  {"style=\"\">", "style=\"\"/>"}
}

function this.bind()
  return {
    announcementContent = {
      moduleName = "pages/announcement/cellAnnouncementContent"
    },
    titleText = "",
    scrollList_content = {
      moduleName = "pages/announcement/cellAnnouncementContent"
    },
    go_scroll = false,
    contentScrollPosition = C_Vector2(0, 0),
    canvasGroup = 1
  }
end

function this:onDownloadCalback(isSuccess, data)
  self.bind.go_scroll = isSuccess
  if not isSuccess then
    return
  end
  data = self:formatSrcString(data)
  local retList = C_Html2UnityRichApi.ToUnityRichText(data)
  self.modules.announcementContent:refresh()
  self.bind.canvasGroup = 0
  self.bind.scrollList_content:clear()
  self.bind.scrollList_content:insert_array(self:parse_text(retList))
end

function this:formatSrcString(data)
  if data == nil then
    return nil
  end
  for k, v in pairs(replaceTable) do
    data = string.gsub(data, v[1], v[2])
  end
  return data
end

function this:parse_text(c_list)
  local list = {}
  local count = c_list.Count
  for i = 0, count - 1 do
    local srcText = C_Html2UnityRichApi.ReplaceStartEnBlank(c_list[i].text)
    local data = {announcementText = srcText}
    if not string.isEmpty(srcText) then
      local pattern = "img=\"(.+)\""
      local matched = string.match(srcText, pattern)
      if not string.isEmpty(matched) then
        data.announcementUrl = matched
        data.nativeSize = true
        data.announcementText = nil
      end
      data.announcementBg = not string.isEmpty(c_list[i].bg)
      data.announcementFont = c_list[i].font
      data.announcementFontColor = c_list[i].fontColor
    end
    if i == count - 1 then
      function data.textShowDoneCb()
        self:textShowDone()
      end
    else
      data.textShowDoneCb = nil
    end
    data.announcementSpaceSize = c_list[i].spaceSize
    table.insert(list, data)
  end
  return list
end

function this:textShowDone()
  self.bind.canvasGroup = 1
end

function this:initData(data)
  self.bindComponents.scrollRect:StopMovement()
  self.bind.contentScrollPosition = C_Vector2(0, 0)
  local content = data:getContent()
  self:OnGetAnnounceCallback(true, content)
end

function this:clearHtmlCache(html)
  if self.cacheData ~= nil then
    self.cacheData[html] = nil
  end
end

function this:OnGetAnnounceCallback(isSuccess, data)
  if not isSuccess then
    return
  end
  self.cacheData = self.cacheData or {}
  local html = data
  if self.cacheData[html] then
    self:onDownloadCalback(true, self.cacheData[html])
  else
    self.bind.go_scroll = false
    self:downloadTxt(html, handler(self, self.onDownloadCalback))
  end
end

function this:downloadTxt(url, callback)
  local successCall = function(self, tex)
    self.cacheData[url] = tex
    if callback then
      callback(true, tex)
    end
  end
  local failCall = function(self, key)
    if callback then
      callback(false, nil)
    end
  end
  C_CDNManager.GetObject(url, handler(self, successCall), handler(self, failCall))
end

function this:parse_hidetags_old(text)
  local hidetag_pattern = "<hidetag>(.-)</hidetag>"
  local start_pos = 1
  local ret = {}
  ret.titleText = "123123123"
  ret.list = {}
  local font
  while true do
    local start, end_pos = text:find(hidetag_pattern, start_pos)
    if not start then
      if start_pos + 1 < string.len(text) then
        local hidetag_content = text:sub(start_pos + 1, string.len(text))
        table.insert(ret.list, {announcementText = hidetag_content})
      end
      break
    end
    local c = text:sub(start, end_pos)
    print("1", start, start_pos, c, string.len(c))
    if start_pos ~= start then
      local second_char = string.sub(text, start - 1, start - 1)
      local offset = 0
      if second_char == "\n" or second_char == "\r" then
        offset = 1
      else
      end
      local hidetag_content = text:sub(start_pos + 1, start - 1 - offset)
      table.insert(ret.list, {announcementText = hidetag_content, font = font})
    end
    do
      local hidetag_content = text:sub(start + 9, end_pos - 10)
      local pattern = "^titleimg=(.+)"
      local matched = string.match(hidetag_content, pattern)
      if not string.isEmpty(matched) then
        ret.titleImg = matched
      end
      pattern = "^titleText=(.+)"
      matched = string.match(hidetag_content, pattern)
      if not string.isEmpty(matched) then
        ret.titleText = matched
      end
      pattern = "^img=(.+)"
      matched = string.match(hidetag_content, pattern)
      print("inori tag", hidetag_content)
      if not string.isEmpty(matched) then
        table.insert(ret.list, {announcementUrl = matched, nativeSize = true})
      end
      pattern = "^font=(.+)"
      matched = string.match(hidetag_content, pattern)
      if not string.isEmpty(matched) then
        if matched == "null" then
          font = nil
        else
          font = matched
        end
      end
      if end_pos + 1 <= string.len(text) then
        local second_char = string.sub(text, end_pos + 1, end_pos + 1)
        if second_char == "\n" or second_char == "\r" then
          end_pos = end_pos + 1 + 1
        end
      end
    end
    start_pos = end_pos
  end
  return ret
end

return this
