local this = class("cellAnnouncementContent", G_UIModuleBase)
local imgCDNPath = "project-3/announcement/"
local imgSavePath = Unity.Application.persistentDataPath .. "/photo/announcement/"

function this.bind()
  return {
    go_image = false,
    go_text = false,
    space_size = C_Vector2(100, 10),
    go_bg = true,
    img_bg = false,
    imgHead = nil,
    textContent = "",
    dotlines = {
      moduleName = "pages/announcement/cellAnnouncementDotline"
    },
    go_titleSpace = false
  }
end

function this:refresh()
  local url = self.bind.announcementUrl
  if self.bind.announcementSpaceSize ~= nil and self.bind.announcementSpaceSize > 0 then
    self.bind.space_size = C_Vector2(100, tonumber(self.bind.announcementSpaceSize))
    self.bind.go_bg = false
    return
  else
    self.bind.space_size = C_Vector2(100, 0)
    self.bind.go_bg = true
  end
  if string.isEmpty(url) then
    self.bind.go_image = false
  else
    self:setImgName(self.bind.announcementUrl)
    self.bind.go_image = true
  end
  if self.bind.announcementBg then
    self.bind.img_bg = true
    self.bind.go_titleSpace = true
  else
    self.bind.img_bg = false
    self.bind.go_titleSpace = false
  end
  local text = self.bind.announcementText
  if string.isEmpty(text) then
    self.bind.go_text = false
    self:triggerTextShowDone()
  else
    self.bind.go_text = true
    if self.bind.announcementBg then
      self.bind.textContent = string.sub(text, 2)
    else
      self.bind.textContent = text
    end
    self.bindComponents.Text.onClick:AddListener(function(url)
      CS.UnityEngine.Application.OpenURL(url)
    end)
    if not string.isEmpty(self.bind.announcementFont) then
      self.bindComponents.Text:LoadFontStyle(self.bind.announcementFont)
    else
      self.bindComponents.Text:LoadFontStyle("ast_fontstyle_a24")
    end
    if not string.isEmpty(self.bind.announcementFontColor) then
      self.bindComponents.Text:LoadColorStyle(self.bind.announcementFontColor)
    else
      self.bindComponents.Text:LoadColorStyle("grey003")
    end
  end
end

function this:open()
  if self.onDirtyLayoutCallback == nil then
    self.onDirtyLayoutCallback = handler(self, self.delayRefreshDotline)
  end
  self.bindComponents.TextCallback.modifyCallback = self.onDirtyLayoutCallback
end

function this:close()
  if self.delayTimer then
    Timer.remove(self.delayTimer)
  end
  self.delayTimer = nil
  self.bindComponents.TextCallback.modifyCallback = nil
end

function this:delayRefreshDotline()
  if self.delayTimer then
    Timer.remove(self.delayTimer)
  end
  CS.UnityEngine.UI.LayoutRebuilder.MarkLayoutForRebuild(self.bindComponents.BgGo)
  self.delayTimer = Timer.once(0.1, function()
    CS.UnityEngine.UI.LayoutRebuilder.MarkLayoutForRebuild(self.bindComponents.BgGo)
    self:refreshDotline()
    self:triggerTextShowDone()
  end)
end

function this:triggerTextShowDone()
  if self.bind.textShowDoneCb ~= nil then
    self.bind.textShowDoneCb()
  end
end

function this:refreshDotline()
  local tmp = {}
  local totalHeight = self.bindComponents.BgGo.sizeDelta.y
  local lines = self.bindComponents.Text.cachedTextGeneratorForLayout.lines
  local linesCount = 0
  local height = 0
  local firstLineStarted = false
  for i = 0, lines.Count - 1 do
    if not firstLineStarted then
      if 0 < lines[i].startCharIdx then
        firstLineStarted = true
      else
        height = 0
      end
    end
    if firstLineStarted and 0 < lines[i].startCharIdx then
      linesCount = linesCount + 1
    end
  end
  if linesCount <= 0 then
    linesCount = 1
  end
  local singleLineHeight = totalHeight / linesCount
  for i = 0, linesCount - 1 do
    height = -singleLineHeight * (i + 1) - 4
    table.insert(tmp, {height = height})
  end
  self.bind.dotlines:clear()
  if self.bind.announcementBg then
    return
  end
  self.bind.dotlines:insert_array(tmp)
end

function this:setImg()
  if self.texture == nil then
    self:createTexWithName(self.name)
  else
    self:refreshImg()
  end
end

function this:refreshImg()
  if self.sprite ~= nil then
    self.bind.imgHead = self.sprite
    if self.bindComponents.Image then
      self.bindComponents.Image:SetNativeSize()
    end
  end
  if self.callBack ~= nil then
    self.callBack()
    self.callBack = nil
  end
end

function this:setImgName(name, callBack)
  if string.isEmpty(name) then
    return
  end
  self.callBack = callBack
  if self.name == name then
    self:refreshImg()
    return
  end
  self.texture = nil
  self.name = name
  self:setImg()
end

function this:getScreenShotHead(url, callback)
  local successCall = function(self, tex)
    L_PhotoStore:call(L_PhotoStore.event.photoFinishGet, {url = url, tex = tex})
    if callback then
      callback(true, tex)
    end
  end
  local failCall = function(self, key)
    if callback then
      callback(false, nil)
    end
  end
  C_CDNManager.DownloadToTex(url, handler(self, successCall), handler(self, failCall))
end

function this:loadHeadTex(name, callback)
  local path = ""
  local strArr = string.split(name, "_")
  if not string.isEmpty(strArr[2]) then
    path = imgSavePath .. strArr[2]
  end
  local imgFilePath = path .. "/" .. name
  imgFilePath = imgFilePath .. ".png"
  local isExists = C_File.Exists(imgFilePath)
  local getCallback = function(flag, tex)
    if flag and not isExists then
      C_PhotoManager.SaveTexture2DByPath(imgFilePath, tex)
    end
    if callback then
      callback(tex)
    end
  end
  if isExists then
    C_PhotoManager.GetTexture2DByPath(imgFilePath, handler(self, function(self, tex)
      getCallback(true, tex)
    end))
  else
    self:getScreenShotHead(name, getCallback)
  end
end

function this:createTexWithName(name)
  self:loadHeadTex(name, function(tex)
    if tex then
      self.texture = tex
      local rect = C_Rect(0, 0, tex.width, tex.height)
      local pivot = C_Vector2.one * 0.5
      self.sprite = Unity.Sprite.Create(tex, rect, pivot)
      self:refreshImg()
    end
  end)
end

return this
