local AShieldStructureUnit = import('/lua/aeonunits.lua').AShieldStructureUnit
local utilities = import('/lua/utilities.lua')
local EffectTemplate = import('/lua/EffectTemplates.lua')

local oldunit = AShieldStructureUnit

BOUNCER = Class(oldunit) {

#  AttackBeams = {'/effects/emitters/seraphim_othuy_beam_01_emit.bp'},
  AttackBeams = EffectTemplate.SUltraChromaticBeamGeneratorBeam,

  OnCreate = function(self)
    oldunit.OnCreate(self)
    self:ForkThread(self.jibakuthread)
    self:ForkThread(self.slavethread)
    self:ForkThread(self.targetThread)
  end,

  jibakuthread = function(self)
    WaitSeconds(15) 
    self:Kill(self,'normal',0)
  end,

  slavethread = function(self)
    WaitSeconds(1) 
    if self.target:IsDead() then
      self:Kill(self,'normal',0)
    end
  end,

  targetThread = function(self)
    #LOG(repr(self.DamageData))
    local beams = {}
    while true do
      local targets = {}
      targets = utilities.GetEnemyUnitsInSphere(self, self:GetPosition(), 10)
      if targets then
        for k, v in targets do
          DamageArea(self.instigator,self:GetPosition(),10,1,'normal',false)
          local target = v
          for k, v in self.AttackBeams do
            local beam = AttachBeamEntityToEntity(target, -1, self, -2, self:GetArmy(), v)
            table.insert(beams, beam)
            self.Trash:Add(beam)
          end
          #LOG(repr(k))
        end
      end
      WaitTicks(1)
      for k, v in beams do
        #LOG(repr(k))
        #LOG(repr(v))
        v:Destroy()
      end
    end
  end,

}



TypeClass = BOUNCER