local this = {}

function this:req_equipAccessory(param, callback)
  local data = {
    hero_id = param.hero_id,
    ornament_guid = param.ornament_guid
  }
  L_Net:sendMessage(MsgGenCode.CSProtoMoveOrnament, data, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc then
      if callback then
        callback(errorCode)
      end
      self:callAccessory(param.ornament_guid)
      self:callAccessory(param.oldGuid)
    end
  end)
end

function this:req_upgradeAccessory(param, callback)
  if L_BattleDataManager:checkPlayerBattle() then
    L_FlyMsgManager:showNormalMsg(L_BattleDataManager:getBattleBlockTips(L_Const.BattleBlockType.EquipmentStrength))
    return
  end
  local oldData = L_AccessoryManager:getPackAtt(self:getAccessory(param.u32))
  L_Net:sendMessage(MsgGenCode.CSProtoUpgradeOrnament, param, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc then
      if callback then
        callback()
      end
      self:call(self.event.refreshAccessoryList)
      self:callAccessory(param.u32)
      self:call(self.event.upgradeAccessorySuccess, oldData)
    end
  end)
end

function this:req_breakAccessory(param, callback)
  if L_BattleDataManager:checkPlayerBattle() then
    L_FlyMsgManager:showNormalMsg(L_BattleDataManager:getBattleBlockTips(L_Const.BattleBlockType.EquipmentResolveEntrance))
    return
  end
  L_Net:sendMessage(MsgGenCode.CSProtoBreakDownOrnament, param, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc then
      if callback then
        callback()
      end
      self:call(self.event.refreshAccessoryList)
      local rewards = rspData.rewards
      if not table.isEmpty(rewards) then
        L_RewardManager:showPage(rewards, rspData.src)
      end
    end
  end)
end

function this:req_setLockAccessory(param, callback)
  self:call(self.event.changeLock, true)
  L_Net:sendMessage(MsgGenCode.CSProtoSetLockOrnament, param, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc then
      if callback then
        callback()
      end
      self:callAccessory(param.guid)
      self:call(self.event.changeLock, false)
    end
  end)
end

function this:req_removeAccessory(param, callback)
  local data = {
    hero_id = param.hero_id,
    type = param.type
  }
  L_Net:sendMessage(MsgGenCode.CSProtoRemoveOrnament, data, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc then
      if callback then
        callback()
      end
      self:callAccessory(param.guid)
    end
  end)
end

function this:req_quickWearAccessory(params, callback)
  if C_BattleManager.IsInBattle() == true then
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_accessoryStore"))
    return
  end
  local data = {
    hero_guid = params.heroGuid,
    spiritron_guid = params.spiritronGuid,
    ornament_guids = params.accessoryGuidList
  }
  printf("6028 \228\184\128\233\148\174\232\163\133\229\164\135 \229\143\130\230\149\176data:", data)
  L_Net:sendMessage(MsgGenCode.CSProtoQuickWear, data, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc and callback then
      callback()
    end
  end)
end

function this:rsp_syncAccessoryBag_all(data)
  self:syncAccessoryBag(data)
  self:call(self.event.refreshAccessoryList)
end

function this:rsp_syncAccessoryBag(data)
  local haveDel = self:syncAccessoryBag(data.ornaments, true)
  local deleteItems = data.guid
  if not table.isEmpty(deleteItems) then
    for _, v in pairs(deleteItems) do
      self.data.accessoryList[v] = nil
    end
  end
  self:call(self.event.refreshAccessoryList, not table.isEmpty(deleteItems) or haveDel)
end

function this:syncAccessoryBag(list, isSync)
  local haveDel
  for _, v in pairs(list or {}) do
    if isSync and not self:getAccessory(v.guid) then
      L_ReddotManager:markNew(L_ReddotManager.DotDef.JewelNew, v.guid)
    end
    if v.del then
      if self.data.accessoryList[v.guid] then
        self.data.accessoryList[v.guid] = nil
        haveDel = true
      end
    else
      local entity = self:getAccessory(v.guid, true)
      entity.accessoryId = v.id
      entity.level = v.lv
      entity.wearHero = v.wear_hero
      entity.lock = v.lock
      entity.group = v.group
      entity.delete = v.del
      local subAtt = v.infos
      if not table.isEmpty(subAtt) then
        entity.infos = {}
        for _, v2 in ipairs(subAtt) do
          table.insert(entity.infos, {
            type = v2.type,
            key = v2.key,
            val = v2.val,
            sort = v2.sort
          })
        end
        table.sort(entity.infos, function(a, b)
          return a.sort < b.sort
        end)
      end
    end
  end
  return haveDel
end

return this
