if( SERVER ) then
   
 	AddCSLuaFile( "shared.lua" ) 
 	SWEP.HoldType = "pistol";
 
 end 

 
 SWEP.Base = "ts2_base";
   
 SWEP.Spawnable			= false 
 SWEP.AdminSpawnable		= false 
   


SWEP.Primary.Sound = Sound( "Weapon_SMG1.Single" );

SWEP.ViewModel = Model( "models/weapons/v_fists.mdl" );
SWEP.WorldModel = Model( "models/frito/ziptie.mdl" );

SWEP.PrintName = "Zipties";
SWEP.TS2Desc = "Used to tie someone - Left click";
 
 SWEP.TS2HoldType = "FIST";

SWEP.Slot = 2;

SWEP.Primary.ClipSize = 0;
SWEP.Primary.DefaultClip = 0;
SWEP.Primary.Delay = .07;
SWEP.Primary.Automatic = true;

 SWEP.Primary.IronSightPos = Vector( 50, 5.5, -5.0 );
 SWEP.Primary.IronSightAng = Vector( 0.0, 0.0, 0.0 );

SWEP.Primary.HolsteredPos = Vector( 50.4, -4, -12.0 );

SWEP.ItemWidth = 1;
SWEP.ItemHeight = 1;
SWEP.IconCamPos = Vector( 41, 50, 50 );
SWEP.IconLookAt = Vector( -1, 0, 0 );
SWEP.IconFOV = 4;


function SWEP:Reload()

	return;

end

SWEP.NextPrimaryAttack = 0;

function SWEP:PrimaryAttack()

	if( CLIENT ) then return; end

	if( self.Owner:IsTied() ) then return; end

	if( CurTime() < self.NextPrimaryAttack ) then return; end

	local trace = { }
	trace.start = self.Owner:EyePos();
	trace.endpos = trace.start + self.Owner:GetAimVector() * 40;
	trace.filter = self.Owner;
	
	local tr = util.TraceLine( trace );

	if( tr.Entity:IsValid() and tr.Entity:IsPlayer() and not tr.Entity:IsTied() ) then
	
		local cantie = true;
		
		if( tr.Entity:GetTable().BeingTied ) then
		
			cantie = false;
		
		end
		
		if( not tr.Entity:GetTable().StanceATW ) then
		
			--Check if facing the player's base/possibly if player is allowing tie
		
		end
		
		if( cantie ) then
		
			tr.Entity:GetTable().BeingTied = true;
			tr.Entity:GetTable().BeingTiedBy = self.Owner;
			self.Owner:GetTable().Tying = true;
			self.Owner:GetTable().TyingTarget = tr.Entity;
			
			local thinkfunc = function( ply, done )
			
				local destroybars = function()
				
					if( self.Owner:IsValid() ) then
					
						self.Owner:GetTable().Tying = false;
						DestroyProcessBar( "tyingup", self.Owner );
					
					end
					
					if( tr.Entity:IsValid() ) then
					
						tr.Entity:GetTable().BeingTied = false;
						DestroyProcessBar( "beingtied", tr.Entity );
						
					end
				
				end
				
				if( not self.Owner:IsValid() or not tr.Entity:IsValid() ) then
				
					destroybars();
					return;
				
				end
				
				if( not self.Owner:KeyDown( IN_ATTACK ) ) then
				
					destroybars();
					return;					
				
				end
			
				if( self.Owner:GetTable().ZipTiePos ~= self.Owner:GetPos() or
					tr.Entity:GetTable().ZipTiePos ~= tr.Entity:GetPos() ) then
				
					destroybars();
					return;
					
				end
				
				if( not self.Owner:Alive() or not tr.Entity:Alive() ) then
				
					destroybars();
					return;
				
				end
				
				if( done ) then
				
					tr.Entity:TiedUpBy( self.Owner );
					destroybars();
				
				end
				
			end
			
			self.Owner:GetTable().ZipTiePos = self.Owner:GetPos();
			tr.Entity:GetTable().ZipTiePos = tr.Entity:GetPos();
			
			CreateProcessBar( "tyingup", "Ziptying", self.Owner );
				SetEstimatedTime( 1, self.Owner );
				SetThinkDelay( .33, self.Owner );
				SetThink( thinkfunc, self.Owner );
				SetColor( Color( 198, 137, 41, 200 ), self.Owner );
			EndProcessBar( self.Owner );
			
			CreateProcessBar( "beingtied", "Being ziptied", tr.Entity );
				SetEstimatedTime( 1, tr.Entity );
				SetThinkDelay( .33, tr.Entity );
				SetColor( Color( 198, 137, 41, 200 ), tr.Entity );
			EndProcessBar( tr.Entity );
		
		
		end
	
	end

end

SWEP.NextSecondaryAttack = 0;

function SWEP:SecondaryAttack()

	if( CLIENT ) then return; end

	if( self.Owner:IsTied() ) then return; end

	if( CurTime() < self.NextSecondaryAttack ) then return; end

	local trace = { }
	trace.start = self.Owner:EyePos();
	trace.endpos = trace.start + self.Owner:GetAimVector() * 40;
	trace.filter = self.Owner;
	
	local tr = util.TraceLine( trace );

	if( tr.Entity:IsValid() and tr.Entity:IsPlayer() and tr.Entity:IsTied() ) then
	
		local cantie = true;
		
		if( tr.Entity:GetTable().BeingUnTied ) then
		
			cantie = false;
		
		end
		
		if( not tr.Entity:GetTable().StanceATW ) then
		
			--Check if facing the player's base/possibly if player is allowing tie
		
		end
		
		if( cantie ) then
		
			tr.Entity:GetTable().BeingUnTied = true;
			tr.Entity:GetTable().BeingUnTiedBy = self.Owner;
			self.Owner:GetTable().UnTying = true;
			self.Owner:GetTable().UnTyingTarget = tr.Entity;
			
			local thinkfunc = function( ply, done )
			
				local destroybars = function()
				
					if( self.Owner:IsValid() ) then
					
						self.Owner:GetTable().UnTying = false;
						DestroyProcessBar( "untyingup", self.Owner );
					
					end
					
					if( tr.Entity:IsValid() ) then
					
						tr.Entity:GetTable().BeingUnTied = false;
						DestroyProcessBar( "beinguntied", tr.Entity );
						
					end
				
				end
				
				if( not self.Owner:IsValid() or not tr.Entity:IsValid() ) then
				
					destroybars();
					return;
				
				end
				
				if( not self.Owner:KeyDown( IN_ATTACK2 ) ) then
				
					destroybars();
					return;					
				
				end
			
				if( self.Owner:GetTable().ZipTiePos ~= self.Owner:GetPos() or
					tr.Entity:GetTable().ZipTiePos ~= tr.Entity:GetPos() ) then
				
					destroybars();
					return;
					
				end
				
				if( not self.Owner:Alive() or not tr.Entity:Alive() ) then
				
					destroybars();
					return;
				
				end
				
				if( done ) then
				
					tr.Entity:UnTiedUpBy( self.Owner );
					destroybars();
				
				end
				
			end
			
			self.Owner:GetTable().ZipTiePos = self.Owner:GetPos();
			tr.Entity:GetTable().ZipTiePos = tr.Entity:GetPos();
			
			CreateProcessBar( "untyingup", "Breaking Zipties", self.Owner );
				SetEstimatedTime( 1, self.Owner );
				SetThinkDelay( .33, self.Owner );
				SetThink( thinkfunc, self.Owner );
				SetColor( Color( 198, 137, 41, 200 ), self.Owner );
			EndProcessBar( self.Owner );
			
			CreateProcessBar( "beinguntied", "Breaking Zipties", tr.Entity );
				SetEstimatedTime( 1, tr.Entity );
				SetThinkDelay( .33, tr.Entity );
				SetColor( Color( 198, 137, 41, 200 ), tr.Entity );
			EndProcessBar( tr.Entity );
		
		
		end
	
	end

end

