local this = {}
local _existSelf = function(v)
  return v.player_id == L_PlayerStore:getPlayerId()
end

function this.syncHomeDatas(data)
  L_ProductStore:rsp_syncHomeProduct(data)
  L_HomeStore:rsp_syncHomeBuildingMap(data)
end

function this.syncAllHomeData(data, init)
  L_ProductStore:rsp_syncHomeProduct(data)
  L_HomeStore:rsp_syncHomeBuildingMap(data, init)
end

local WMC = L_Const.WorldMapCmdType
local Cmd2Func = {
  [WMC.WMCT_PLAYER] = function(data)
    this.syncPlayer(data.map_info.players)
  end,
  [WMC.WMCT_ACED] = function(data)
    this.syncPlayer(data.map_info.players, nil, true)
  end,
  [WMC.WMCT_QUIT] = function(data)
    L_HomeStore:rsp_quit(data)
  end,
  [WMC.WMCT_BUILD_LOCATE] = this.syncHomeDatas,
  [WMC.WMCT_BUILD_CREATE] = this.syncHomeDatas,
  [WMC.WMCT_BUILD_CREATE_FINISH] = this.syncHomeDatas,
  [WMC.WMCT_BUILD_UNLOCATE] = this.syncHomeDatas,
  [WMC.WMCT_HOME_LEVEL] = this.syncHomeDatas,
  [WMC.WMCT_PRODUCT_START] = this.syncHomeDatas,
  [WMC.WMCT_PRODUCT_FINISH] = this.syncHomeDatas,
  [WMC.WMCT_PRODUCT_CANCEL] = this.syncHomeDatas,
  [WMC.WMCT_HOUSE_REFRESH] = this.syncHomeDatas,
  [WMC.WMCT_FIELD_OPEN] = this.syncHomeDatas,
  [WMC.WMCT_FIELD_CLOSE] = this.syncHomeDatas,
  [WMC.WMCT_FIELD_PLANT] = this.syncHomeDatas,
  [WMC.WMCT_FIELD_WATER] = this.syncHomeDatas,
  [WMC.WMCT_FIELD_HARVEST] = this.syncHomeDatas,
  [WMC.WMCT_FIELD_UNPLANT] = this.syncHomeDatas,
  [WMC.WMCT_HATCH_REFRESH] = this.syncHomeDatas,
  [WMC.WMCT_HOME_INIT] = function(data)
    this.syncAllHomeData(data, true)
  end,
  [WMC.WMCT_HOME_REFRESH] = function(data)
    this.syncAllHomeData(data)
  end,
  [WMC.WMCT_INTERACT] = function(data)
    this.refreshCollectionItem(data)
  end
}

function this.socket_9103(data, errorCode)
  printf("lua", "9103\229\164\132\231\144\134\228\184\173" .. tostring(data.cmd))
  if Cmd2Func[data.cmd] then
    Cmd2Func[data.cmd](data, errorCode)
  end
  if data.cmd == L_Const.WorldMapCmdType.WMCT_ENTER then
    L_AreaStore:rsp_swicthScene(data)
    local sPlayer = data.map_info.players
    local needSwitchScene = table.find(sPlayer, _existSelf) ~= nil
    this.syncPlayer(sPlayer, not needSwitchScene)
  end
  if data.cmd == L_Const.WorldMapCmdType.WMCT_RETRACT then
    local sPlayer = data.map_info.players
    for _, mapInfo in ipairs(sPlayer) do
      local move = mapInfo.move[#mapInfo.move]
      local position = {}
      position.x = move.pos.x and move.pos.x * 0.01 or 0
      position.y = move.pos.y and move.pos.y * 0.01 or 0
      position.z = move.pos.z and move.pos.z * 0.01 or 0
      local rotation = {}
      rotation.x = 0
      rotation.y = move.angle and move.angle * 0.01 or 0
      rotation.z = 0
      AzurWorld.transferMgr:ClientTransferStartFlow(position, rotation, C_ETranslateLoadingType.UIBlack)
    end
  end
  L_HomeStore:syncHomeMapDatas()
end

function this.refreshCollectionItem(data)
  if not (data and data.map_info) or not data.map_info.objs then
    return
  end
  for _, obj in pairs(data.map_info.objs) do
    local worldMapId = obj.obj_id
    L_MapStore:refreshCollectionItem(worldMapId)
  end
end

function this.syncPlayer(sPlayer, isSend, stopTransfer)
  local playerId = L_PlayerStore:getPlayerId()
  for _, v in ipairs(sPlayer) do
    local isSelf = v.player_id == playerId
    if isSelf then
      L_PlayerStore:rsp_syncPlayerMapInfo(v, isSend, stopTransfer)
    end
  end
end

function this.syncPlayerMove(sPlayer)
  for _, v in ipairs(sPlayer) do
    local isSelf = v.player_id == L_PlayerStore:getPlayerId()
    if isSelf then
      L_PlayerStore:rsp_syncPlayerMapInfo(v)
    end
  end
end

function this.socket_9105(data, errorCode)
  if table.isEmpty(data) then
    return
  end
  L_SceneStore:rsp_syncMapPoint(data)
end

function this.getCsRewardsData(rewards, dropIds)
  local rewardsData = {}
  local dropTpl = L_GameTpl:getDropTpl()
  if rewards ~= nil then
    for i, v in pairs(rewards) do
      local splitNum = 0
      if dropIds ~= nil then
        for _, dropId in ipairs(dropIds) do
          local dropTplData = dropTpl:getDropGroup(dropId)
          for _, drop in ipairs(dropTplData) do
            if dropTpl:getItemId(drop) == v.itemid then
              splitNum = splitNum + dropTpl:getSplitNum(drop)
            end
          end
        end
      end
      local parse = L_ItemManager:parseItem(v.itemtype, v.itemid)
      table.insert(rewardsData, {
        guid = v.guid,
        itemId = v.itemid,
        itemNum = v.itemnum,
        itemType = v.itemtype,
        quality = parse.quality or 0,
        splitNum = splitNum
      })
    end
  end
  return rewardsData
end

function this.socket_9133(data, errorCode)
  if errorCode == L_Const.errorCode.ErrCodeSucc then
    if data.objs ~= nil then
      for _, v in ipairs(data.objs) do
        local objData = v
        this._onGetReward(objData.rewards, objData.pos, objData.drop_ids)
      end
    end
    if data.rewards ~= nil then
      local rewards = this.getCsRewardsData(data.rewards.rewards)
      for _, k in pairs(data.objs) do
        local pos = k.pos
        pos.x = pos.x * 0.01
        pos.y = pos.y * 0.01
        pos.z = pos.z * 0.01
        local position = L_Vector3.new(pos.x, pos.y, pos.z)
        AzurWorld.DropItemMgr:DropRewardPrefab(position, rewards, 0)
      end
    end
  end
end

function this.socket_9406(data, errorCode)
  if errorCode == L_Const.errorCode.ErrCodeSucc then
    this._onGetReward(data.rewards, data.pos)
  end
end

function this.socket_9404(playableFinish, pos)
  local rewardUnit = AzurWorld.PlayableUnitMgr:GetUnit(playableFinish.playId)
  if rewardUnit ~= nil then
    local pos = {}
    pos.x = rewardUnit.FinishRewardPosition.x * 100
    pos.y = rewardUnit.FinishRewardPosition.y * 100
    pos.z = rewardUnit.FinishRewardPosition.z * 100
    this._onGetReward(playableFinish.reward, pos)
  end
end

function this._onGetReward(rewards, pos, dropIds)
  if rewards == nil then
    return
  end
  if rewards.rewards ~= nil then
    local pageRewardDataList = {}
    for i = #rewards.rewards, 1, -1 do
      local rewardUnit = rewards.rewards[i]
      if rewardUnit.itemtype == L_Const.resType.petEgg or rewardUnit.itemtype == L_Const.resType.pet then
        table.insert(pageRewardDataList, rewardUnit)
        table.remove(rewards.rewards)
      end
    end
    local newRewards = require("ui.manager.reward.rewardData").new()
    newRewards.src = rewards.src
    newRewards:constructList(pageRewardDataList)
    L_RewardManager:ShowReward(newRewards)
    local tempRewards = this.getCsRewardsData(rewards.rewards, dropIds)
    pos.x = pos.x * 0.01
    pos.y = pos.y * 0.01
    pos.z = pos.z * 0.01
    local position = L_Vector3.new(pos.x, pos.y, pos.z)
    AzurWorld.DropItemMgr:DropRewardPrefab(position, tempRewards, 0)
  else
    print("\229\165\150\229\138\177\230\149\176\230\141\174\228\184\186\231\169\186, rewards.rewards == nil")
  end
end

function this.socket_9137(data, errorCode)
  if errorCode == L_Const.errorCode.ErrCodeSucc and data.u32s ~= nil then
    local _worldSpawnerTpl = L_GameTpl:getWorldSpawnerTpl()
    local _worldMapTpl = L_GameTpl:getWorldMapTpl()
    for k, v in pairs(data.u32s) do
      L_MapStore:setObjDiscovered(AzurWorldInstance.CurWorldId, v)
      local tpl = _worldMapTpl:getTplById(AzurWorldInstance.CurWorldId, v)
      if tpl then
        local spawnerId = _worldMapTpl:getSpawnerId(tpl)
        local spawnerTpl = _worldSpawnerTpl:getTplById(spawnerId)
        if spawnerTpl then
          local objectType = _worldSpawnerTpl:getObjectType(spawnerTpl)
          local banTypes = L_Config:getConfig("game", "WORLD_FIRST_DISCOVERED_NOTIPS")
          for _, banType in ipairs(banTypes) do
            if banType == objectType then
              return
            end
          end
          local content = L_WordsTpl:getValue("notice_world_first_discovered", {
            [0] = _worldSpawnerTpl:getName(spawnerTpl)
          })
          L_FlyMsgManager:showNormalMsg(content)
        end
      end
    end
  end
end

return this
