local this = class("gameEventData")
local _GameEventTpl = L_GameTpl:getGameEventsTpl()

function this:ctor(id)
  self.id = id
  self.start_time = 0
  self.end_time = 0
  self.hasEventData = false
  self._lastRefreshTime = 0
  local tpl = _GameEventTpl:getTplById(id)
  local eType = tpl and _GameEventTpl:getType(tpl) or "nil"
  local gameEventModelPath = L_GameEventStore.gameEventModels[eType]
  if gameEventModelPath then
    local cls = require(L_R.store .. "gameEvent.models." .. gameEventModelPath)
    self._gameEvent = cls.new(id, eType, self)
    self._type = eType
  else
    errorf("\230\180\187\229\138\168" .. id .. "\231\177\187\229\158\139" .. eType .. "\228\184\141\229\173\152\229\156\168 \230\137\190\231\173\150\229\136\146\239\188\129")
  end
end

function this:getType()
  return self._type
end

function this:populate(data)
  self.start_time = data.start_time
  self.end_time = data.end_time
end

function this:isShow()
  local tpl = _GameEventTpl:getTplById(self.id)
  local showBegin, showEnd = _GameEventTpl:getStartShowTime(tpl), _GameEventTpl:getEndShowTime(tpl)
  showBegin, showEnd = L_TimeUtil.parseConfigDatetime(showBegin), L_TimeUtil.parseConfigDatetime(showEnd)
  local currTime = L_TimeUtil.getServerTime()
  return showBegin <= currTime and showEnd >= currTime and self:isUnlock(tpl)
end

function this:isUnlock()
  local result = L_SystemUnlockStore:getSystemIsOpen(L_SystemConst.enum.gameAct)
  local tpl = _GameEventTpl:getTplById(self.id)
  local conditions = _GameEventTpl:getCondition(tpl)
  local canShow = true
  local complete = true
  for i, v in ipairs(conditions) do
    if not L_ConditionManager:singleIsComplete(v) then
      complete = false
      break
    end
  end
  if not complete then
    canShow = _GameEventTpl:getUnlockShow(tpl) == 1 and true or false
  end
  return result and canShow
end

function this:isValid()
  return self:isUnlock() and self.start_time <= L_TimeUtil.getServerTime() and self.end_time >= L_TimeUtil.getServerTime()
end

function this:canParticipateIn()
  return self:isValid() and self:checkIfHasEventData()
end

function this:getGameEvent()
  if self._lastRefreshTime < self.start_time then
    self._gameEvent:initData()
  end
  return self._gameEvent
end

function this:populateEventData(data)
  self._lastRefreshTime = data.time
  if table.isEmpty(data.data) then
    return
  end
  self._gameEvent:populate(data.data)
  self.hasEventData = true
end

function this:getActivityId()
  return self.id
end

function this:getStartTime()
  return self.start_time
end

function this:getEndTime()
  return self.end_time
end

function this:checkIfHasEventData()
  return self.hasEventData == true
end

function this:dispose()
  if self._gameEvent.dispose then
    self._gameEvent:dispose()
  end
end

return this
