local TAirUnit = import('/lua/terranunits.lua').TAirUnit
local CDFRocketIridiumWeapon02 = import('/lua/cybranweapons.lua').CDFRocketIridiumWeapon02
local TAAGinsuRapidPulseWeapon = import('/lua/terranweapons.lua').TAAGinsuRapidPulseWeapon
local TANTorpedoAngler = import('/lua/terranweapons.lua').TANTorpedoAngler

tcau0201 = Class(TAirUnit) {
    Weapons = {
        Missile = Class(CDFRocketIridiumWeapon02) {},
        AAMissile = Class(TAAGinsuRapidPulseWeapon) {},
        Torpedo01 = Class(TANTorpedoAngler) {},
        },
		
    Parent = false,

    SetParent = function(self, parent, droneName)
        self.Parent = parent
        self.Drone = droneName
    end,

    OnCreate = function(self, builder, layer)
    TAirUnit.OnCreate(self,builder,layer)
        if not self:IsDead() then
 
            self:ForkThread(self.LaunchEffects)

            self.DroneMaxSpeed = 22
            self.DistanceToTarget = nil
            self.MyTarget = {}
        end
    end,

    LaunchEffects = function(self)
        if not self:IsDead() then
            WaitSeconds(2)
            if not self:IsDead() then
                self:HeartBeatDistanceCheck()
            end
        end
    end,

    HeartBeatDistanceCheck = function(self)
        self.AwayFromCarrier = false

        while self and not self:IsDead() and not self.Parent:IsDead() do
            WaitSeconds(2)
            if not self:IsDead() and not self.Parent:IsDead() then
                local dronePos = self:GetPosition()
                local parentPos = self.Parent:GetPosition()
                local distance = VDist2(dronePos[1], dronePos[3], parentPos[1], parentPos[3])
                if distance > 256 and self.AwayFromCarrier == false then
                        IssueStop({self})
                        IssueClearCommands({self})
                    self.AwayFromCarrier = true
                    self:ForkThread(self.GuardCarrier)

                elseif distance <= 256 and self.AwayFromCarrier == true then
                    self.AwayFromCarrier = false
				end
			end
		end
	end,

    OnMotionHorzEventChange = function(self, new, old)
        if not self:IsDead() and new == 'Stopped' and not self.Parent:IsDead() then 
            self:ForkThread(self.GuardCarrier)

        elseif not self:IsDead() and new == 'Stopping' and not self.Parent:IsDead() then
            self:ForkThread(self.GuardCarrier)    
        TAirUnit.OnMotionHorzEventChange(self, new, old)
		end
    end,

    GuardCarrier = function(self)
        if not self:IsDead() and not self.Parent:IsDead() then
            IssueClearCommands(self)
            IssueGuard({self}, self.Parent)
        end
    end,

    OnDamage = function(self, instigator, amount, vector, damagetype)
        TAirUnit.OnDamage(self, instigator, amount, vector, damagetype)
        if self:IsDead() == false and instigator and IsUnit(instigator) then
            IssueAttack({self}, instigator)
        end
    end,

    OnKilled = function(self, instigator, type, overkillRatio)
        IssueClearCommands(self)
		
        if not self.Parent:IsDead() then
            self.Parent:NotifyOfDroneDeath(self.Drone)
            table.removeByValue(self.Parent.DroneTable, self)
            self.Parent = nil
        end

        TAirUnit.OnKilled(self, instigator, type, overkillRatio)
    end,
}

TypeClass = tcau0201
