local this = class("cellPlayerDressItem", G_UIModuleBase)

function this.bind()
  return {
    img_show = "",
    isActive_cur = false,
    isActive_img = false,
    isActive_lock = false,
    isActive_reddot = false,
    isActive_select = false
  }
end

function this.methods()
  return {
    onClick = function(self)
      if self.bind.isActive_select == true then
        return
      end
      self:emit("onClick", self.bind)
      L_ReddotManager:clearNew(L_ReddotManager.DotDef.PlayerDressItem, self.bind.id, true)
      AzurWorld.RedDotMgr:RefreshRedDot(self.bind.id)
    end
  }
end

function this:open()
  self:refreshView()
  AzurWorld.RedDotMgr:BindRedDotRenderer(self.bind.id, self.bindComponents.reddot.gameObject)
  AzurWorld.RedDotMgr:MarkDirty(self.bind.id)
end

function this:close()
  print("close" .. self.bind.id)
end

function this:refresh()
  self:refreshView()
end

function this:refreshView()
  local item = L_PlayerDisplayStore:getItem(self.bind.id)
  if item ~= nil then
    self.url = item.url
  else
    self.url = ""
  end
  self:refreshCurDress()
  local tpl = L_PlayerDisplayStore._playerCardDressTpl:getTplById(self.bind.id)
  self.bind.isActive_img = true
  if tpl.defaultComponent == 1 and tpl.type == L_Const.playerDressType.hero then
    local baseInfo = L_PlayerStore:getPlayerBaseInfo()
    L_PhotoManager:newOrBindPhotoEntity(self.bindComponents.img_show, self.bindComponents.img_show, L_FriendStore:getPlayerAvatars(baseInfo, L_Const.avatarTextureIndex.body))
  elseif string.isEmpty(self.url) then
    self.bind.img_show = L_PlayerDisplayStore._playerCardDressTpl:getPic(tpl)
  else
    self.bind.img_show = ""
    self.bind.isActive_img = false
    L_PlayerDisplayStore:getHeroStandeeCapture(self.url, true, function(tex, url)
      if url == self.url then
        self.bind.isActive_img = true
        self.bind.isac = true
        local rect = C_Rect(0, 0, tex.width, tex.height)
        local pivot = C_Vector2.one * 0.5
        local sprite = Unity.Sprite.Create(tex, rect, pivot)
        self.bindComponents.img_show.sprite = sprite
      end
    end)
  end
  local has = L_PlayerDisplayStore:hasItem(self.bind.id)
  has = has or tpl.customize == 1
  self.bind.isActive_lock = not has
end

function this:refreshCurDress()
  self.bind.isActive_cur = L_PlayerDisplayStore:isCurDress(self.bind.id)
end

return this
