local this = {}

function this:req_enterHome(creator_id, callBack)
  if creator_id == nil then
    creator_id = L_PlayerStore:getPlayerId()
  end
  local operatorSendSystem = L_PlayerManager:getOperatorSendSystem()
  if operatorSendSystem then
    operatorSendSystem:shutdownSyncPosition()
  end
  local data = {creator_id = creator_id}
  L_Net:sendMessage(MsgGenCode.CSProtoEnterHome, data, function(rspData)
    if callBack then
      callBack()
    end
  end)
end

function this:req_exitHome(callBack)
  local operatorSendSystem = L_PlayerManager:getOperatorSendSystem()
  if operatorSendSystem then
    operatorSendSystem:shutdownSyncPosition()
  end
  L_Net:sendMessage(MsgGenCode.CSProtoWorldQuitHome, nil, function(rspData, errorCode)
    if errorCode == 0 and callBack then
      callBack()
    end
  end)
end

function this:req_levelUpMainBuilding(homeId, callBack)
  local data = {u32 = homeId}
  L_Net:sendMessage(MsgGenCode.CSProtoMainBuildLvUp, data, function(rspData, errorCode)
    if errorCode == 0 and callBack then
      callBack()
    end
  end)
end

function this:req_buildCreate(guid, build_id, block_id, anchor, direction, callBack)
  local data
  if guid then
    data = {guid = guid}
  else
    local dataLocate = {
      block_id = block_id,
      anchor = math.floor(anchor),
      direction = direction
    }
    data = {build_id = build_id, locate = dataLocate}
  end
  L_Net:sendMessage(MsgGenCode.CSProtoBuildCreate, data, function(rspData)
    if callBack then
      callBack(rspData)
    end
  end)
end

function this:req_buildCreateFinish(guid, callBack)
  local data = {u32 = guid}
  L_Net:sendMessage(MsgGenCode.CSProtoBuildCreateFinish, data, function(rspData)
    if callBack then
      callBack(rspData)
    end
  end)
end

function this:req_locateBuilding(guid, build_id, block_id, anchor, direction, callBack)
  local dataLocate = {
    block_id = block_id,
    anchor = math.floor(anchor),
    direction = direction
  }
  local data = {
    guid = guid,
    build_id = build_id,
    locate = dataLocate
  }
  AzurWorld.HomeMgr:ReqLocateBuilding(guid, build_id, block_id, anchor, direction, callBack)
end

function this:req_BuildUnlocate(guid, callBack)
  local data = {guid = guid}
  L_Net:sendMessage(MsgGenCode.CSProtoBuildUnlocate, data, callBack)
end

function this:req_changeBelt(type, list)
  L_Net:sendMessage(MsgGenCode.CSProtoHomeShortcutChange, {
    shortcut_bar = {
      {type = type, item_id = list}
    }
  })
end

function this:rsp_syncBuildingBag(data)
  for _, v in pairs(data) do
    if not math.isEmpty(v.build_id) then
      self.data.buildingBag[v.build_id] = self.data.buildingBag[v.build_id] or require(L_R.store .. "home.data.homeBuildingInfo").new()
      local entity = self.data.buildingBag[v.build_id]
      entity.build_id = v.build_id
      entity.total_num = v.total_num
      entity.used_num = v.used_num
      entity.unlock = v.unlock
    end
  end
  self:call(self.event.refreshBuildingBag)
end

function this:rsp_generalInfo(data)
  if data.home_name == nil then
    return
  end
  self.data.homeName = data.home_name
  self:call(self.event.refreshHomeInfo)
end

function this:rsp_syncHomeBelt(data)
  for _, belt in ipairs(data) do
    self.data.beltItems[belt.type] = belt.item_id
  end
  printf("\229\174\182\229\155\173\229\191\171\230\141\183\230\160\143\231\137\169\229\147\129id", self.data.beltItems)
  self:call(self.event.refreshBelt)
end

function this:rsp_syncHomeBuildingMap(data, errcode, init)
  if init then
    self:clearMap()
  end
  local mapInfo = data.map_info
  if table.isEmpty(mapInfo) or table.isEmpty(mapInfo.home) then
    return
  end
  self.data.currMapId = data.map_id
  local homeData = mapInfo.home
  for i, v in ipairs(homeData.builds or {}) do
    self.data.buildingDatas[v.guid] = v
    setmetatable(v, {
      __newindex = function(t, k, v)
        errorf("\228\184\141\232\166\129\229\190\128store\230\149\176\230\141\174\233\135\140\230\148\190\229\165\135\230\128\170\231\154\132\229\175\185\232\177\161\239\188\129\239\188\129")
      end
    })
  end
  for i, v in ipairs(homeData.del_builds or {}) do
    self.data.buildingDatas[v] = nil
  end
  self:call(self.event.syncWorldMapHomeItem, homeData.builds)
  if data.cmd == L_Const.WorldMapCmdType.WMCT_HOME_LEVEL then
    self:call(self.event.world_home_upgrade)
  end
  if not table.isEmpty(homeData.builds) or not table.isEmpty(homeData.del_builds) then
    L_ProductStore:call(L_ProductStore.event.refreshHomeProduct)
  end
end

function this:rsp_quit(data)
  if self.data.currMapId == data.map_id then
    self.data.currMapId = 0
  end
end

function this:clearMap()
  self.data.buildingDatas = {}
end

function this:rsp_syncHomeUnlock(data)
  for _, v in ipairs(data.infos) do
    if v.type == L_HomeConst.HomeLevelUnlockModuleType.Block then
      self.data.tempUnlockBlock[v.id] = true
    end
    if v.type ~= L_HomeConst.HomeLevelUnlockModuleType.Technology then
      L_FlyMsgManager:showHomeContentUnlock(v)
    end
  end
  self:call(self.event.syncHomeContentUnlock)
end

function this:rsp_CSProtoHomeBuildLoginSync(data, errcode, init)
  L_SatietyStore:initSatietyServer()
end

function this:syncHomeMapDatas()
  self:call(self.event.refreshModuleHomeCenterBuildingManageReceiveAllDot)
end

function this:req_modifyHomeName(name, callback)
  local data = {name = name}
  L_Net:sendMessage(MsgGenCode.CSProtoSetHomeName, data, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc then
      if callback then
        callback()
      end
    elseif errorCode == L_Const.errorCode.ErrCodeInvalidNick then
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_idcard_4"))
    end
  end)
end

function this:rsp_petEatingFinishNotify(data, errcode)
  if errcode ~= L_Const.errorCode.ErrCodeSucc then
    return
  end
  local eventData = {}
  eventData.petGuid = data.pet_guid
  eventData.foodId = data.food_id
  self:call(self.event.refreshPetEatFinishList, eventData)
end

function this:reqPetStationHomeHub(petGuid)
  local csHomeStore = L_HomeStore:getCsHomeStore()
  csHomeStore:ReqPetStationHomeHub(petGuid)
end

function this:reqPetExitHomeHub(petGuid)
  local csHomeStore = L_HomeStore:getCsHomeStore()
  csHomeStore:ReqPetExitHomeHub(petGuid)
end

function this:reqChangeHomeHubBuildingPos(homeHubTabType, sourcePos, targetPos)
  local csHomeStore = L_HomeStore:getCsHomeStore()
  csHomeStore:ReqChangeHomeHubBuildingPos(homeHubTabType, sourcePos, targetPos)
end

function this:reqReceiveAllBuildingDataInHomeCenter()
  local csHomeStore = self:getCsHomeStore()
  csHomeStore:ReqReceiveAllBuildingDataInHomeCenter()
end

return this
