local this = class("pageAccessoryInfoTip", G_UIPageBase)
local _accessoryTpl = L_GameTpl:getAccessoryTpl()

function this.bind()
  return {
    img_qualityBg = nil,
    img_icon = nil,
    txt_name = "",
    list_star = {
      moduleName = "pages/bag/cellBagTipStar"
    },
    module_accessoryAtt = {
      moduleName = "pages/accessory/moduleAccessoryAtt"
    },
    module_accessorySuit = {
      moduleName = "pages/accessory/moduleAccessorySuit"
    },
    go_accessoryHeadRank = false,
    img_accessoryHeadRank = nil,
    go_lock = false,
    isOn_saLock = false,
    go_saOwner = false,
    img_saOwner = nil
  }
end

function this.methods()
  return {
    onClick_close = function(self)
      L_UI:close(self.pageName)
    end,
    onClick_lock = function(self)
      L_AccessoryManager:setAccessoryLock(self._guid, function()
        local serverData = L_AccessoryStore:getAccessory(self._guid)
        self.bind.isOn_saLock = serverData.lock
      end)
    end
  }
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  self._guid = options.guid
  self._itemId = options.itemId
  self:initPage()
end

function this:close()
  this.super.close(self)
end

function this:initPage()
  self:setAccessoryData()
end

function this:setAccessoryData()
  local serverData = L_AccessoryStore:getAccessory(self._guid)
  local bInBag = false
  local accessoryId = -1
  if serverData then
    accessoryId = serverData.accessoryId
    bInBag = true
  else
    accessoryId = self._itemId
  end
  local tpl = _accessoryTpl:getTplById(accessoryId)
  local quality = _accessoryTpl:getRarity(tpl)
  local configData = L_ItemTplManager:getAccessoryItem(accessoryId)
  self.bind.txt_name = configData.name
  self.bind.img_qualityBg = self:getQualityBgName(quality)
  self.bind.img_icon = _accessoryTpl:getIcon(tpl)
  self.bind.go_lock = bInBag
  if bInBag then
    self.bind.isOn_saLock = serverData.lock
  end
  local isEquipped = bInBag and not math.isEmpty(serverData.wearHero)
  self.bind.go_saOwner = isEquipped
  if isEquipped then
    local heroData = L_HeroStore:getHero(serverData.wearHero)
    if heroData then
      local heroConf = L_ItemTplManager:getHeroItem(L_HeroStore:getHeroConfigId(heroData))
      self.bind.img_saOwner = heroConf.icon
      if heroData.type == L_Const.HeroType.HT_MAIN then
        L_PhotoManager:newOrBindPhotoEntity(self, self.bindComponents.img_head, L_PlayerStore:getPlayerModHeadImgName())
      else
        L_PhotoManager:clearPhotoEntity(self)
      end
    end
  end
  local moduleAccessoryAtt = self.modules.module_accessoryAtt
  if bInBag then
    moduleAccessoryAtt:initModule(self._guid)
  else
    moduleAccessoryAtt:initModulePreview(accessoryId)
  end
  local moduleAccessorySuit = self.modules.module_accessorySuit
  if bInBag then
    moduleAccessorySuit:setNormalSuit(self._guid)
  else
    moduleAccessorySuit:setNormalSuitPreview(accessoryId)
  end
  self:setStar(quality + 1)
  local headRank = _accessoryTpl:getHeadRank(tpl)
  self.bind.go_accessoryHeadRank = 0 < headRank
  if 0 < headRank then
    self.bind.img_accessoryHeadRank = L_AccessoryStore:getAccessoryHeadRankIcon(headRank)
  end
end

function this:setStar(starNum)
  local stars = {}
  for i = 1, 5 do
    table.insert(stars, {
      go_active = i <= starNum
    })
  end
  self.bind.list_star:clear()
  self.bind.list_star:insert_array(stars)
end

function this:getQualityBgName(quality)
  quality = quality or 0
  if self.getQualityBgNameFunc then
    return self.getQualityBgNameFunc(quality)
  end
  return string.format("UI/Atlas/Bag/tex_bag_rarity_tips_bg_%s.png", quality)
end

return this
