local this = class("moduleAccessorySuit", G_UIModuleBase)
local _accessoryTpl = L_GameTpl:getAccessoryTpl()
local _accessorySetTpl = L_GameTpl:getAccessorySetTpl()

function this.bind()
  return {
    txt_suitName = nil,
    list_suitDesc = {
      moduleName = "pages/accessory/cellAccessoryAtt"
    }
  }
end

function this:setNormalSuit(guid)
  local serverData = L_AccessoryStore:getAccessory(guid)
  self:setNormalSuitPreview(serverData.accessoryId)
end

function this:setNormalSuitPreview(accessoryId)
  local tpl_accessory = _accessoryTpl:getTplById(accessoryId)
  local suitId = _accessoryTpl:getSetId(tpl_accessory)
  if 0 < suitId then
    local suitTxtData = L_AccessoryManager:getSuitDesc(accessoryId)
    self.bind.txt_suitName = suitTxtData.name
    local tmp = {}
    for i, v in ipairs(suitTxtData.suit) do
      table.insert(tmp, {
        txt_desc = v.desc,
        alpha_canvas = 1
      })
    end
    self.bind.list_suitDesc:clear()
    self.bind.list_suitDesc:insert_array(tmp)
  else
    self.bind.txt_suitName = L_WordsTpl:getValue("ui_moduleAccessorySuit")
    self.bind.list_suitDesc:clear()
  end
end

function this:setEquippedSuit(guid, heroGuid, isPreview)
  local serverData = L_AccessoryStore:getAccessory(guid)
  local accessoryId = serverData.accessoryId
  local wearHero = serverData.wearHero
  local tpl_accessory = _accessoryTpl:getTplById(accessoryId)
  local suitId = _accessoryTpl:getSetId(tpl_accessory)
  if suitId <= 0 then
    self.bind.txt_suitName = L_WordsTpl:getValue("ui_moduleAccessorySuit")
    self.bind.list_suitDesc:clear()
    return
  end
  local suitTxtData = L_AccessoryManager:getSuitDesc(accessoryId)
  local heroAccessoryGuidList = L_HeroStore:getHeroAccessoryGuidList(L_HeroStore:getHero(heroGuid))
  local suitNum = 1
  local tplCur = _accessoryTpl:getTplById(accessoryId)
  local posCur = _accessoryTpl:getType(tplCur)
  local setCur = _accessoryTpl:getSetId(tplCur)
  for _, guidTmp in pairs(heroAccessoryGuidList) do
    if not math.isEmpty(guidTmp) and guidTmp ~= guid and not math.isEmpty(setCur) then
      local serverDataTmp = L_AccessoryStore:getAccessory(guidTmp)
      local tplTmp = _accessoryTpl:getTplById(serverDataTmp.accessoryId)
      local posTmp = _accessoryTpl:getType(tplTmp)
      local setTmp = _accessoryTpl:getSetId(tplTmp)
      if setTmp == setCur then
        if isPreview and posCur ~= posTmp then
          suitNum = suitNum + 1
        elseif not isPreview and serverDataTmp.wearHero == wearHero then
          suitNum = suitNum + 1
        end
      end
    end
  end
  for i, v in ipairs(suitTxtData.suit) do
    if suitNum >= v.index then
      suitTxtData.suit[i].active = true
    end
  end
  local tpl_set = _accessorySetTpl:getTplById(_accessoryTpl:getSetId(tplCur))
  local suitSkillList = _accessorySetTpl:getSkill(tpl_set)
  local maxSuitNum = suitSkillList[#suitSkillList][1]
  local nameColor = suitNum >= suitTxtData.suit[1].index and L_Const.colorHtml.blue008 or L_Const.colorHtml.grey001
  self.bind.txt_suitName = L_GameUtil.fillColor(string.format("%s(%s/%s)", suitTxtData.name, suitNum, maxSuitNum), nameColor)
  local tmp = {}
  for i, v in ipairs(suitTxtData.suit) do
    table.insert(tmp, {
      txt_desc = L_GameUtil.fillColor(v.desc, v.active and L_Const.colorHtml.blue008 or L_Const.colorHtml.grey003),
      alpha_canvas = v.active and 1 or 0.5
    })
  end
  self.bind.list_suitDesc:clear()
  self.bind.list_suitDesc:insert_array(tmp)
end

return this
