local this = class("cellAccessorySlot", G_UIModuleBase)
local _, DEFAULT_COLOR = C_ColorUtility.TryParseHtmlString("#9baeb8")
local quality_color = L_Const.accessoryQualityColor
local _accessoryTpl = L_GameTpl:getAccessoryTpl()
local path = "UI/Atlas/Accessory/tex_icon_accessory_default.png"

function this.bind()
  return {
    go_equipped = false,
    go_empty = true,
    go_active = false,
    color_frame = DEFAULT_COLOR,
    img_rarity = "",
    img_icon = nil
  }
end

function this.methods()
  return {
    onClick_item = function(self)
      self:emit("on_click_accessory", {
        accessoryType = self.bind.type,
        selectHeroGuid = self.bind.heroId
      })
      self:emit("onClick_item", true)
    end,
    onPointEnter_item = function(self)
      if not L_DeviceTpl:getIsPc() then
        return
      end
      if self.bind.go_equipped then
        self.bind.go_active = true
      end
    end,
    onPointExit_item = function(self)
      if not L_DeviceTpl:getIsPc() or self.isEnterLock then
        return
      end
      if self.bind.go_equipped then
        self.bind.go_active = self.isSelected
      end
    end
  }
end

function this:open()
  self.isSelected = false
  L_AccessoryStore:listenCallFunc(L_AccessoryStore.event.refreshAccessory, self.changeLockStatus, self)
end

function this:refresh()
  local index = self.bind.index
  local heroId = self.bind.heroId
  if index then
    L_ReddotManager:registerReddot(self.bindComponents.reddotNormal, string.format(L_ReddotManager.DotDef.HeroJewel, heroId .. "_" .. index))
  end
  if self.bind.guid then
    C_LuaUtility.LoadAtlasTextureAsync(self.bind.img_icon, function(texture, tillingAndOffset, v4)
      if not L_CommonUtil.isValid(texture) then
        errorf("\232\175\165\232\163\133\229\164\135\231\154\132slotIcon\228\184\141\229\173\152\229\156\168  cfgId = " .. tostring(self.bind.accessoryId))
        return
      end
    end)
    local accessory = _accessoryTpl:getTplById(self.bind.accessoryId)
    local rarity = _accessoryTpl:getRarity(accessory)
    local _, color = C_ColorUtility.TryParseHtmlString(quality_color[rarity])
    self.bind.color_frame = color
    self.bind.img_rarity = string.format("UI/Atlas/AccessoryPage/tex_icon_rarityep_0%s.png", rarity + 1)
  end
end

function this:close()
  L_AccessoryStore:unListenCallFunc(L_AccessoryStore.event.refreshAccessory, self.changeLockStatus)
end

function this:setModuleInfo()
  if not self.isBind then
    return
  end
  if not self.bind.guid then
    return
  end
end

function this:setActive(active)
  self.bind.go_active = active
  self.isSelected = active
  if not active then
    self:emit("onClick_item", false)
  end
  if active then
    self:setModuleInfo()
  end
end

function this:changeLockStatus()
  self.isEnterLock = false
  self:setModuleInfo()
end

return this
