local this = {}
local tInsert = table.insert

function this:rsp_syncRogueInfo(data)
  if not data then
    return
  end
  if data.status and data.status ~= L_RogueConst.state.continue then
    self.data.curRogue.status = data.status
    self.data.curRogue.start_time = data.info.start_time
    self.data.curRogue.use_time = data.info.use_time
    self:call(self.event.onRogueSettle)
    return
  end
  self.data.rewardStorage = {}
  local info = data.info
  for k, v in pairs(info) do
    local handleFunc = L_RogueConst.handler[k]
    if self[handleFunc] then
      self[handleFunc](self, v)
    else
      self.data.curRogue[k] = v
    end
  end
  if data.need_dels then
    local delInfo = data.need_dels
    for i, v in pairs(delInfo) do
      local type = v.type
      local keys = v.keys
      local propertyName = L_RogueConst.propertyName[type]
      if self.data.curRogue[propertyName] then
        for i2, v2 in pairs(keys) do
          self.data.curRogue[propertyName][v2] = nil
        end
      end
      local handleFunc = L_RogueConst.handler[propertyName]
      if self[handleFunc] then
        self[handleFunc](self, {})
      end
    end
  end
  if self:getInRogue() then
    self:call(self.event.refreshRogueInfo)
  end
end

function this:sync_pos(data)
  self.data.curRogue.pos = L_Vector3.new(data.x / 100, data.y / 100, data.z / 100)
end

function this:sync_room(data)
  local room = data
  self.data.curRogue.room = room
  self.data.curRogue.room.next_rooms = {}
  for _, v in ipairs(room.next_rooms) do
    local nextRoom = v
    self.data.curRogue.room.next_rooms[nextRoom.pass_order] = nextRoom
  end
end

function this:sync_attrs(data)
  for i, v in pairs(data) do
    self.data.curRogue.attrs[v.attr_id] = v.attr_val
  end
end

function this:sync_infos(data)
  self.data.curRogue.infos = self.data.curRogue.infos or {}
  for _, v in ipairs(data) do
    if not self.data.curRogue.infos[v] then
      tInsert(self.data.rewardStorage, {
        type = L_Const.resType.rogueInfo,
        id = v
      })
    end
    local info = {id = v}
    self.data.curRogue.infos[info.id] = info
  end
end

function this:sync_echos(data)
  self.data.curRogue.echos = self.data.curRogue.echos or {}
  for _, v in ipairs(data) do
    if not self.data.curRogue.echos[v.id] then
      tInsert(self.data.rewardStorage, {
        type = L_Const.resType.rogueEcho,
        id = v.id
      })
    end
    local echo = v
    self.data.curRogue.echos[echo.id] = echo
  end
end

function this:sync_had_heros(data)
  self.data.curRogue.had_heros = data
end

function this:sync_heros(data)
  if not self.data.curRogue.heroIdList then
    self.data.curRogue.heroIdList = {}
  end
  if not table.isEmpty(data) then
    for _, v in ipairs(data) do
      local hero = v
      local guid = L_HeroStore:getHeroGuid(hero)
      if 10 < guid then
        tInsert(self.data.curRogue.heroIdList, guid)
        self.data.curRogue.heros[guid] = hero
      end
    end
  end
end

function this:sync_pets(data)
  if not self.data.curRogue.had_pets then
    self.data.curRogue.had_pets = {}
  end
  local petInfoTpl = L_GameTpl:getPetTpl()
  if not table.isEmpty(data) then
    for _, v in pairs(data) do
      tInsert(self.data.curRogue.had_pets, v.guid)
      local entity = self:getRoguePet(v.guid, true)
      local pet_tpl = petInfoTpl:getTplById(v.config_id)
      if v.pet_name == "" or nil then
        local pet_name = petInfoTpl:getName(pet_tpl, v.color)
        entity.pet_name = pet_name
      else
        entity.pet_name = v.pet_name
      end
      entity.guid = v.guid
      entity.id = v.config_id
      entity.comprehension = v.comprehension or nil
      entity.skills = v.inherent_skills
      entity.feature = v.feature
      entity.lv = v.lv
      entity.exp = v.exp
      entity.rank = 1
      entity.hero_id = v.hero_id
      entity.petId = v.config_id
      entity.hero_id = v.hero_id
      entity.gene_skills = v.unlock_gene_skills
      entity.gene_value = L_PetStore:getPetGeneValue(v.gene_infos, v.gene_state)
      entity.is_lock = v.is_lock
      entity.gene_infos = v.gene_infos
      entity.gene_state = v.gene_state
      entity.ur_pos = v.ur_pos
    end
  end
end

function this:sync_ornaments(data)
  if not table.isEmpty(data) then
    for _, v in pairs(data) do
      local entity = self:getRogueAccessory(v.guid, true)
      entity.accessoryId = v.id
      entity.level = v.lv
      entity.wearHero = v.wear_hero
      entity.exp = v.exp
      entity.lock = v.lock
      entity.group = v.group
      local subAtt = v.infos
      if not table.isEmpty(subAtt) then
        entity.infos = {}
        for i, v2 in ipairs(subAtt) do
          table.insert(entity.infos, {
            type = v2.type,
            key = v2.key,
            val = v2.val
          })
        end
      end
    end
  end
end

function this:sync_spiritrons(data)
  if not table.isEmpty(data) then
    for _, v in pairs(data) do
      local entity = self:getRogueSpiritron(v.guid, true)
      entity.guid = v.guid
      entity.spiritronId = v.id
      entity.spiritronLv = v.lv
      entity.star = v.advance
      entity.exp = v.exp
      entity.lock = v.lock
      entity.spiritronHero = v.wear_hero
      entity.rank = v.rank
    end
  end
end

function this:sync_rewards(data)
  if not table.isEmpty(data) then
    for _, v in ipairs(data) do
      local entity = self:getRogueReward(v.guid, true)
      table.merge(entity, v.reward)
    end
  end
  self:call(self.event.refreshRogueReward)
end

function this:sync_now_effect(data)
  local reward = require(L_R.store .. "rogue.data.rogueReward").new()
  table.merge(reward, data)
  self.data.curRogue.now_effect = reward
  self:call(self.event.refreshNowEffect)
end

function this:sync_triggers(data)
  if not table.isEmpty(data) then
    self.data.curRogue.triggers = self.data.curRogue.triggers or {}
    for _, v in pairs(data) do
      local triggers = v
      triggers.triggers = {}
      for __, t in pairs(v.triggers) do
        local trigger = t
        triggers.triggers[trigger.guid] = trigger
      end
      self.data.curRogue.triggers[triggers.type] = triggers
    end
  end
end

function this:sync_trigger_effects(data)
  for _, v in ipairs(data or {}) do
    local triggerEffect = v
    self.data.curRogue.trigger_effects[triggerEffect.guid] = triggerEffect
  end
end

function this:sync_room_events(data)
  for _, v in pairs(data) do
    local entity = self:getRogueEvent(v.guid, true)
    table.merge(entity, v)
  end
  self:call(self.event.refreshRoomEvent)
end

function this:sync_shops(data)
  self.data.curRogue.shops = self.data.curRogue.shops or {}
  for _, v in pairs(data) do
    local shop = v
    self.data.curRogue.shops[shop.guid] = shop
  end
end

function this:sync_dialogs(data)
  self.data.curRogue.dialogs = self.data.curRogue.dialogs or {}
  for _, v in pairs(data) do
    local dialog = v
    self.data.curRogue.dialogs[dialog.guid] = dialog
  end
end

return this
