local Util = import('/lua/utilities.lua')
local oldUnit = QuantumGateUnit
QuantumGateUnit = Class(oldUnit) {

	OnStopBeingBuilt = function(self,builder,layer)
		oldUnit.OnStopBeingBuilt(self,builder,layer)
		LOG('onstopbeingbuilt')
 		self:ForkThread(ButtonThread, self)
		self:ForkThread(GateEffectChecker, self)
	end,
	
    CheckBuildRestriction = function(self, target_bp)
        if self:CanBuild(target_bp.BlueprintId) and not self.Teleporting == true then
            return true
        else
            return false
        end
    end,
	
}
function GateEffectChecker(self)
	self.unitcount = 0
	self.Teleporting = false
	self.UnitsTeleported = {}
	self.teleporters = {}
	self.Sender = false
	self.Reciever = false
	self.FeedbackPulse = false
	local aiBrain = self:GetAIBrain()
	while true do
		#LOG(self.FeedbackPulse)
		#LOG(self.unitcount)
		#LOG(self:IsIdleState())
		if self.unitcount > 0 and self.Teleporting == false and self.FeedbackPulse == false then
			if self.Sender == true then
				ChangeState(self, self.TeleportSendState)
			end
			if self.Reciever == true then
    			ChangeState(self, self.TeleportRecieveState)
    		end
		end
		if self.unitcount == 0 and self.Teleporting == true or self.FeedbackPulse == true and self.Teleporting == true then
       		if self.Sender == true and self:IsIdleState() == true or self.FeedbackPulse == true then
       			#LOG('not sending any units, shutting down')
       			self.Teleporting = false
				self:PlayCloseFlashes()
        		self:CleanupSendEffectsBag()
        		self.Sender = false
        		if not self:IsDead() then
        		    self:SetBusy(false)
       				ChangeState(self, self.IdleState)
       			end
       		end
			if self.Reciever == true and table.getn(self.teleporters) == 0 or self.FeedbackPulse == true then
				#LOG('not recieving any units, shutting down')
				self.Teleporting = false
				self:PlayCloseFlashes()
				self:CleanupRecieveEffectsBag()
				self.Reciever = false
				if not self:IsDead() then
					self:SetBusy(false)
					ChangeState(self, self.IdleState)
       			end
       		end
		end
		WaitTicks(1)
	end
end

function ButtonThread(self)
	self:SetScriptBit('RULEUTC_ProductionToggle', true)
	local aiBrain = self:GetAIBrain()
	while true and not self:IsDead() do
		if self:HasValidTeleportDest() == true and not self.Reciever == true and not self:IsUnitState('Building')  then
			self.TeleportDest = true
			#LOG(self.TeleportDest)
		else
			self.TeleportDest = false
			#LOG(self.TeleportDest)
		end
		if self:GetScriptBit('RULEUTC_ProductionToggle') == false and self.TeleportDest == true then
			#LOG('looking for units')
			local rallypos = self:GetRallyPoint()
			local unitstoteleport = aiBrain:GetUnitsAroundPoint(categories.MOBILE, rallypos, 15, 'ALLY')
			for k, v in unitstoteleport do
				if v:IsIdleState() == true and not v:IsUnitState('BeingBuilt') and not v.BeingTeleported then
					LOG('Gate r stealing ur units!!ohno')
					IssueTransportLoad( {v}, self)
				end
			end
		end
		WaitTicks(1)
	end
end
