local this = class("idleBehaviorSystem", L_EcsConst.system)
local PERSONAL_WAIT_TIME = L_GameClientTpl:getValue("PERSONAL_WAIT_TIME")

function this:ctor(parent)
  this.super.ctor(self, parent)
  self.curTime = 0
  self.constIdleAnimTime = PERSONAL_WAIT_TIME
  self.onStateEnterHandler = self.onStateEnterHandler or handler(self, self.onStateEnter)
  self.isRunning = true
end

function this:onDestroy()
end

function this:onSetMainHero(main, sou, setType)
  if sou then
    local oldFsm = sou:getFsmComponent()
    if oldFsm then
      oldFsm:removeOnStateEnterHandler(self.onStateEnterHandler)
    end
  end
  local fsmComponent = main:getFsmComponent()
  if not fsmComponent then
    return
  end
  fsmComponent:addOnStateEnterHandler(self.onStateEnterHandler)
  self.constIdleAnimTime = PERSONAL_WAIT_TIME
  self.curTime = C_Time.time
end

function this:onSetMainMount(mount, sou, setType)
end

function this:onSetMainGm(main, sou, setType)
end

function this:onStateEnter(fsmType)
  if fsmType == L_CommonUtil.enumToNum(L_Const.fsmType.idle) then
    self.constIdleAnimTime = PERSONAL_WAIT_TIME
    self.curTime = C_Time.time
  else
    self.curTime = nil
  end
end

function this:InitIdleAnim()
  self.isRunning = true
end

function this:StopIdleAnim()
  self.isRunning = false
end

function this:onUpdate()
  if self.isRunning == false then
    return
  end
  if self.curTime then
    local time = C_Time.time
    if time >= self.curTime + self.constIdleAnimTime then
      self.curTime = time
      self:doIdleAnim()
    end
  end
end

function this:doIdleAnim()
  local mainHero = self.parent:getMainHero()
  if mainHero == nil then
    return
  end
  mainHero:getCsEntity().state:ChangeState(C_EFsmStateType.Personal)
end

return this
