local this = class("playerStore", G_BaseStore)
this.event = {
  refreshBaseInfo = "refrshBase",
  refreshAttr = "refreshAttr",
  syncAttrInfoComplete = "syncAttrInfoComplete",
  refreshMapInfo = "refreshMapInfo",
  refreshModAvatar = "refreshModAvatar",
  modAvatarLoadFinish = "modAvatarLoadFinish",
  refreshPlayerStatus = "playerStore_refreshPlayerStatus",
  refreshPlayerMount = "playerStore_refreshPlayerMount",
  refreshMountStatus = "refreshMountStatus",
  playerLevelUp = "playerLevelUp",
  playerExpUp = "playerExpUp"
}
this:importPartialClass(require(L_R.store .. "player.playerState"))
this:importPartialClass(require(L_R.store .. "player.playerAction"))

function this:clear()
  this.super.clear(self)
  if self.staminaTimer then
    Timer.remove(self.staminaTimer)
    self.staminaTimer = nil
  end
end

function this:setPlayerName(name)
  self.data.baseInfo.playerName = name
end

function this:setPlayerId(id)
  self.data.baseInfo.id = id
end

function this:isPlayerLogin()
  return self.data.baseInfo.id ~= nil
end

function this:ResetExpUp(isBlock)
  self.expUpParam.isExpUpBlock = isBlock
  self.expUpParam.lastLv = self.data.baseInfo.lv
end

function this:refreshBaseInfo(baseInfo)
  local lastExp = L_PlayerStore:getExp()
  table.merge(self.data.baseInfo, baseInfo)
  local lastLv = self.data.baseInfo.lastLv
  local curLv = self.data.baseInfo.lv
  local curExp = self.data.baseInfo.exp
  self.expUpParam.curLv = self.data.baseInfo.lv
  self.expUpParam.curExp = self.data.baseInfo.exp
  if not math.isEmpty(lastLv) and lastLv ~= curLv then
    for i = lastLv + 1, curLv do
      L_ReddotManager:markNew(L_ReddotManager.DotDef.LevelUnlockHomeContent, i)
    end
    self.expUpParam.isLevelUp = true
    if not self.expUpParam.isExpUpBlock then
      self.expUpParam.lastLv = self.data.baseInfo.lastLv
      self.expUpParam.lastExp = lastExp
      self.expUpParam.isExpUpBlock = true
      L_UI:open("pagePlayerExpUp")
      self:call(self.event.playerLevelUp, lastLv)
    end
  elseif lastExp ~= curExp and lastLv == curLv then
    self.expUpParam.isLevelUp = false
    if not self.expUpParam.isExpUpBlock then
      self.expUpParam.lastLv = self.data.baseInfo.lastLv
      self.expUpParam.lastExp = lastExp
      self.expUpParam.isExpUpBlock = true
      L_UI:open("pagePlayerExpUp")
      self:call(self.event.playerExpUp, lastExp)
    end
  end
  self.data.baseInfo.lastLv = curLv
  lastExp = curExp
  C_EntityManager.AVATAR_OBJ_MODEL_INDEX = L_GameUtil.getHeroModelConstId()
  self:call(self.event.refreshBaseInfo, baseInfo)
end

function this:refreshModInfo(modInfo)
  self.data.modAvatar = modInfo
  self:call(self.event.refreshModAvatar, modInfo)
end

function this:refreshAttr(attr)
  local player_attr = self:getCurrency(attr.attr_id, true)
  player_attr.attr_val = attr.attr_val or 0
  player_attr.deadtime = attr.deadtime or 0
  self:call(self.event.refreshAttr, player_attr)
end

function this:refreshStamina()
  if self.staminaTimer then
    Timer.remove(self.staminaTimer)
    self.staminaTimer = nil
  end
  local scale, minus, limit = self:getMinuOrScale()
  if scale == nil or minus == nil then
    return
  end
  if 0 < minus then
    self.staminaTimer = Timer.once(minus, function()
      self:getTimeRunFunc()
      self.staminaTimer = Timer.repeated(scale, self.getTimeRunFunc, self)
    end)
  else
    self.staminaTimer = Timer.repeated(scale, self.getTimeRunFunc, self)
  end
end

function this:getMinuOrScale()
  local recover = self:getCurrency(L_Const.currencyType.staminaRecover)
  if recover == nil then
    return
  end
  local playerLevelTpl = L_GameTpl:getPlayerLevelTpl()
  local limit = tonumber(playerLevelTpl:getStamina(playerLevelTpl:getTplById(L_PlayerStore:getLv())))
  local scale = tonumber(L_GameConstTpl:getValue("STAMINA_TIME"))
  local minus = scale + recover.attr_val - L_TimeUtil.getServerTime()
  return scale, minus, limit
end

function this:getTimeRunFunc()
  local scale, minus, limit = self:getMinuOrScale()
  local stamina = self:getCurrency(L_Const.currencyType.stamina)
  if stamina and limit > stamina.attr_val then
    stamina.attr_val = stamina.attr_val + 1
    self:call(self.event.refreshAttr, stamina)
    local staminaRecover = self:getCurrency(L_Const.currencyType.staminaRecover)
    staminaRecover.attr_val = staminaRecover.attr_val + scale
    self:call(self.event.refreshAttr, staminaRecover)
  end
end

function this:modifyDisplayHero(hero)
  self.data.baseInfo.info.hero = {}
  for i, v in ipairs(hero) do
    local heroData = L_HeroStore:getHero(v.hero_id)
    table.insert(self.data.baseInfo.info.hero, {
      id = v.hero_id,
      lv = L_HeroStore:getHeroLevel(heroData),
      config = L_HeroStore:getHeroConfigId(heroData),
      pos = v.pos
    })
  end
end

function this:modifyDisplayPet(pet)
  self.data.baseInfo.info.pet = {}
  for i, v in ipairs(pet) do
    local petData = L_PetStore:getPetItem(v.pet_guid)
    table.insert(self.data.baseInfo.info.pet, {
      guid = v.pet_guid,
      id = petData.id,
      lv = petData.lv,
      pos = v.pos
    })
  end
end

function this:getIsNewPlayer()
  local playerName = self:getPlayerName()
  local isNew = string.find(playerName, "&") == 1
  return isNew
end

return this
