local this = class("playerDisplayStore", G_BaseStore)
this.event = {
  dressItemSync = " dressItemSync"
}
this:importPartialClass(require(L_R.store .. "playerDisplay.playerDisplayState"))
this:importPartialClass(require(L_R.store .. "playerDisplay.playerDisplayAction"))

function this:ctor()
  this.super.ctor(self)
  self._playerCardDressTpl = L_GameTpl:getPlayercardDressTpl()
  self._playerCardDressListTpl = L_GameTpl:getPlayercardDressListTpl()
end

function this:hasItem(id)
  return self.data.allItem[id] ~= nil
end

function this:isCurDress(id)
  local hero = self:getcurHero()
  if hero == nil then
    return false
  end
  if self:getcurHero().config == id or self:getcurPet() == id or self:getCurbackground() == id or self:getCurbase() == id then
    return true
  end
  return false
end

function this:getTexFilePath(name, folder, customPath)
  local path = ""
  customPath = customPath or L_PhotoStore.path.imgStandeeHeroSavePath
  path = customPath .. folder
  return string.format("%s/%s.png", path, name)
end

function this:updateCaptureInfo(id, url)
  local item = self:getItem(id)
  if item ~= nil then
    item.url = url
  end
end

function this:getLocalPath(url)
  if not string.isEmpty(url) then
    local strArr = string.split(url, "/")
    local name = strArr[#strArr]
    local path = L_PlayerDisplayStore:getTexFilePath(name, L_PlayerStore:getPlayerId())
    return path
  end
  return ""
end

function this:getHeroStandeeCapture(url, isSelf, callback)
  if isSelf then
    self:getHeroStandeeCaptureSelf(url, callback)
  else
    local successCall = function(self, tex)
      callback(tex, url)
    end
    local failCall = function(self, key)
    end
    C_CDNManager.DownloadToTex(url, handler(self, successCall), handler(self, failCall))
  end
end

function this:getHeroStandeeCaptureSelf(url, callback)
  local imgFilePath = self:getLocalPath(url)
  local isExists = C_File.Exists(imgFilePath)
  local getCallback = function(flag, tex)
    if flag then
      callback(tex, url)
      if not isExists then
        C_PhotoManager.SaveTexture2DByPath(imgFilePath, tex, false)
      end
    end
  end
  if isExists then
    C_PhotoManager.GetTexture2DByPath(imgFilePath, function(tex)
      callback(tex, url)
    end)
  else
    local successCall = function(self, tex)
      if getCallback then
        getCallback(true, tex)
      end
    end
    local failCall = function(self, key)
      if getCallback then
        getCallback(false, nil, key)
      end
    end
    C_CDNManager.DownloadToTex(url, handler(self, successCall), handler(self, failCall))
  end
end

function this:buildRedDotTree()
  self:getRedDotRootNode()
  self:buildRedDotTab()
  self:buildRedDotItem()
  if self.redDotRootNode ~= nil then
    self.redDotRootNode:RefreshState()
  end
end

function this:getRedDotRootNode()
  local node = AzurWorld.RedDotMgr:BuildRedDotByConfig(20000)
  self.redDotRootNode = node
  return node
end

function this:buildRedDotTab()
  for _, config in pairs(self._playerCardDressListTpl.data) do
    if config ~= nil and config.team == 2 and config.show == 1 and L_ConditionManager:isComplete(config.condition) then
      AzurWorld.RedDotMgr:RegistRedDot(config.id, self.redDotRootNode)
    end
  end
end

function this:buildRedDotItem()
  local allDressConfig = self._playerCardDressTpl.data
  for _, config in pairs(allDressConfig) do
    local type = config.type
    local id = config.id
    local tabNode = AzurWorld.RedDotMgr:GetRedDot(type)
    if tabNode ~= nil then
      do
        local node = AzurWorld.RedDotMgr:RegistRedDot(id, tabNode, function()
          return L_ReddotManager:haveNew(L_ReddotManager.DotDef.PlayerDressItem, id)
        end)
      end
    end
  end
end

function this:getTabRedDotNode(tabId)
  return AzurWorld.RedDotMgr:GetRedDot(tabId)
end

function this:registRedDot(id, rootRedDotNode, check)
  local node = AzurWorld.RedDotMgr:RegistRedDot(id, rootRedDotNode, check)
  return node
end

function this:clearCache()
end

return this
