local this = class("waterMarkManager", G_EventManagerBase)
local cellWidth = 274
local cellHeight = 360

function this:ctor()
  this.super.ctor(self)
  self.markNode = nil
  self.markList = nil
  self.isSetPlayerId = false
  self.cellCount = 0
  self.testWaterMarkNode = nil
  self.testWaterMarkTextNode = nil
  self.isTestMarkRefresh = false
end

function this:initialize()
  L_PlayerStore:listenCallFunc(L_PlayerStore.event.refreshBaseInfo, self.onEvent_refreshBaseInfo, self)
  local root = L_UI:getRoot()
  self.markNode = root:Find("[VisiableWaterMark]/markNode")
  self.testWaterMarkNode = root:Find("[VisiableWaterMark]/testingWaterMark")
  self.testWaterMarkTextNode = root:Find("[VisiableWaterMark]/testingWaterMark/markIdText")
  if L_CommonUtil.isValid(self.markNode) then
    self.markList = self.markNode:Find("markList")
    self.markNode.gameObject:ActiveTrans(false)
    self.cellWaterMark = self.markNode:Find("cell/cellWaterMark")
  end
  self:refreshTestWaterMark(false)
end

function this:calculateCellCount()
  if not L_CommonUtil.isValid(self.markList) then
    return
  end
  local screenWidth = Unity.Screen.width
  local screenHeight = Unity.Screen.height
  local horizontalCount = math.floor(screenWidth / cellWidth)
  local verticalCount = math.floor(screenHeight / cellHeight)
  self.cellCount = math.ceil(horizontalCount * verticalCount * 1.5)
end

function this:refreshMarkNode(isActive)
  if not L_CommonUtil.isValid(self.markNode) then
    return
  end
  self:calculateCellCount()
  self:clearMarkList()
  self:creatMarkList()
  self.markNode.gameObject:ActiveTrans(isActive)
  local playerId = L_PlayerStore:getPlayerId()
  if isActive and not self.isSetPlayerId and playerId ~= nil then
    for i = 0, self.markList.childCount - 1 do
      local idText = self.markList:GetChild(i):Find("markIdText"):GetComponent(typeof(C_LTextMeshProUGUI))
      idText.text = tostring(playerId)
    end
    self.isSetPlayerId = true
  end
end

function this:refreshTestWaterMark(isActive)
  if self.testWaterMarkNode == nil then
    error("Water Mark: testWaterMarkNode is nil")
    return
  end
  if self.testWaterMarkTextNode == nil then
    error("Water Mark: testWaterMarkTextNode is nil")
    return
  end
  if isActive then
    local playerId = L_PlayerStore:getPlayerId()
    if playerId ~= nil then
      local testText = self.testWaterMarkTextNode:GetComponent(typeof(C_LTextMeshProUGUI))
      local str = L_WordsTpl:getRawValue("notice_watermark_tips01")
      if testText ~= nil and str ~= nil then
        local fin_str = L_Config:provider(str, {
          [0] = tostring(playerId)
        })
        if fin_str ~= nil then
          self.testWaterMarkNode.gameObject:ActiveTrans(isActive)
          testText.text = fin_str
        end
      else
        print("Water Mark: notice_watermark_tips01 is nil")
      end
    else
      print("Water Mark: playerId is nil")
    end
  else
    self.testWaterMarkNode.gameObject:ActiveTrans(isActive)
  end
end

function this:creatMarkList()
  for i = 1, self.cellCount do
    local cell = C_UnityObject.Instantiate(self.cellWaterMark.gameObject, self.markList)
    cell.name = "cellWaterMark_" .. i
  end
end

function this:clearMarkList()
  if L_CommonUtil.isValid(self.markList) then
    for i = 0, self.markList.childCount - 1 do
      local child = self.markList:GetChild(i)
      if L_CommonUtil.isValid(child) then
        C_GameObject.Destroy(child.gameObject)
      end
    end
  end
end

function this:dispose()
  if L_CommonUtil.isValid(self.markNode) then
    self:clearMarkList()
    self.markNode.gameObject:ActiveTrans(false)
  end
  self.markNode = nil
  self.markList = nil
  self.isSetPlayerId = nil
  self.cellCount = 0
  self.cellLayout = nil
  L_PlayerStore:unListenCallFunc(L_PlayerStore.event.refreshBaseInfo, self.onEvent_refreshBaseInfo)
end

function this:onEvent_refreshBaseInfo()
  self:refreshTestWaterMark(true)
end

return this
