

local meta = FindMetaTable( "Player" );

local RentOptions = 
{

	{ Length = 1, Price = .5 },
	{ Length = 2, Price = .7 }, 
	{ Length = 3, Price = .9 },
	
}

function TS.GetOptionDoorPrice( id, door )

	if( id == -1 ) then
		return 0;
	else
		return math.ceil( RentOptions[id].Price * door:GetTable().DoorPrice );
	end
	--return math.ceil( RentOptions[2].Price * door:GetTable().DoorPrice );

end

function meta:CanOwnDoor( door )

	if( door:GetTable().DoorPrice ) then
	
		if( door:GetTable().DoorFlags ) then
		
			if( self:IsCitizen() ) then
			
				if( string.find( door:GetTable().DoorFlags, "c" ) ) then
				
					return true;
				
				end
			
			elseif( self:IsCombine() ) then
				
				if( string.find( door:GetTable().DoorFlags, "C" ) ) then
				
					return true;
				
				end			
				
			end
		
		end
	
	end
	
	return false;

end

function meta:OpenDoorMenu( hitpos, door )

	CreateActionMenu( "Rent this property", self, hitpos - Vector( 0, 0, 10 ) );
	SetActionMenuEntity( door );
	if( door:GetTable().DoorPrice > 0 ) then
	
		if( string.find( door:GetTable().DoorFlags, "s" ) ) then
		
			local plur = "s";
			
			if( door:GetTable().DoorPrice == 1 ) then
				plur = "";
			end
			
			local saleprice = door:GetTable().DoorPrice * .5
			
			if( not door:HasNoOwners() ) then
				if door:OwnsDoor( self ) then
				CreateActionMenu( "Storage options", self, hitpos - Vector( 0, 0, 10 ) );
				SetActionMenuEntity( door );
				AddActionOption( "Sell Storage", "eng_sellproperty", saleprice );
				AddActionOption( "Give spare key", "eng_promptado", "" );
				AddActionOption( "Rename Property", "eng_promptpropname", "" );
				AddActionOption( "Look Inside Storage", "eng_lookinsidemapstorage", 1 );
				AddActionOption( "Close", "", "" );
				EndActionMenu();
				return;
				else
				CreateActionMenu( "This storage is already owned!", self, hitpos - Vector( 0, 0, 10 ) );
				SetActionMenuEntity( door );
				AddActionOption( "OK", "", "" );
				EndActionMenu();
				return;
				end
			end
			AddActionOption( "Own for " .. door:GetTable().DoorPrice .. " credit" .. plur, "eng_buystorage", door:GetTable().DoorPrice );
		
		else
			
			if( not door:HasNoOwners() ) then
				if door:OwnsDoor( self ) then
				CreateActionMenu( "Property Options", self, hitpos - Vector( 0, 0, 10 ) );
				SetActionMenuEntity( door );
				AddActionOption( "Cancel rent", "eng_sellproperty", math.Round( door:GetTable().DoorPrice * .1 ) );
				AddActionOption( "Give spare key", "eng_promptado", "" );
				AddActionOption( "Rename Property", "eng_promptpropname", "" );
				AddActionOption( "Close", "", "" );
				EndActionMenu();
				return;
				else
				CreateActionMenu( "This property is already rented out!", self, hitpos - Vector( 0, 0, 10 ) );
				SetActionMenuEntity( door );
				AddActionOption( "OK", "", "" );
				EndActionMenu();
				return;
				end
			end
	
			for k, v in pairs( RentOptions ) do
				local plur = "s";
				local plur2 = "s";
				local newprice = math.ceil( v.Price * door:GetTable().DoorPrice );
				if( v.Length == 1 ) then
					plur = "";
				end
				if( newprice == 1 ) then
					plur2 = "";
				end
				if( newprice == 0 ) then
					newprice = "free";
				else
					newprice = newprice .. " credit" .. plur2;
				end
				AddActionOption( v.Length .. " hour" .. plur .. " for " .. newprice, "eng_rentproperty", k );
			end
			
		end
		
	else
	
		if( string.find( door:GetTable().DoorFlags, "s" ) ) then
		
			if( not door:HasNoOwners() ) then
				if door:OwnsDoor( self ) then
				CreateActionMenu( "Property Options", self, hitpos - Vector( 0, 0, 10 ) );
				SetActionMenuEntity( door );
				AddActionOption( "Cancel rent", "eng_sellproperty", 0 );
				AddActionOption( "Give spare key", "eng_promptado", "" );
				AddActionOption( "Rename Property", "eng_promptpropname", "" );
				AddActionOption( "Close", "", "" );
				EndActionMenu();
				return;
				else
				CreateActionMenu( "This property is already owned!", self, hitpos - Vector( 0, 0, 10 ) );
				SetActionMenuEntity( door );
				AddActionOption( "OK", "", "" );
				EndActionMenu();
				return;
				end
			end
		
			AddActionOption( "Own for free", "eng_rentproperty", -1 );
			
		else
		
			if( not door:HasNoOwners() ) then
				if door:OwnsDoor( self ) then 
				CreateActionMenu( "Property Options", self, hitpos - Vector( 0, 0, 10 ) );
				SetActionMenuEntity( door );
				AddActionOption( "Cancel rent", "eng_sellproperty", 0 );
				AddActionOption( "Give spare key", "eng_promptado", "" );
				AddActionOption( "Rename Property", "eng_promptpropname", "" );
				AddActionOption( "Close", "", "" );
				EndActionMenu();
				return;
			else
				CreateActionMenu( "This property is already rented out!", self, hitpos - Vector( 0, 0, 10 ) );
				SetActionMenuEntity( door );
				AddActionOption( "OK", "", "" );
				EndActionMenu();
				return;
				end
			end
		
			AddActionOption( "Rent for free", "eng_rentproperty", -1 );
		
		end
		
	end
	EndActionMenu();

end

function meta:UnownallDoors()

	if( not self:GetTable().Owned ) then return; end

	for k, v in pairs( self:GetTable().Owned ) do
	
		v:UnownDoor( self );
	
	end

end

function meta:OwnProperty( door, id )

	local price = TS.GetOptionDoorPrice( id, door );

	if( not self:CanAfford( price ) ) then
	
		self:PrintMessage( 3, "You cannot afford this." );
		return;
	
	end
	
	self:TakeMoney( price );
	self:OwnDoor( door );

	local parent = door:GetTable().PropertyParent;
	
	for k, v in pairs( TS.MapDoors ) do
	
		if( v.PropertyParent == parent and v.PropertyParent ~= 7 ) then
		--if( v.PropertyParent == child ) then
		
			if( not v.Door:OwnsDoor( self ) ) then
					self:OwnDoor( v.Door );
			end
			
		end
	
	end
	
	local mins = 0
	local secs = 0
	
	if( id == 1 or -1 ) then
	mins = 1 * 60
	elseif( id == 2 ) then
	mins = 2 * 60
	elseif( id == 3 ) then
	mins = 3 * 60
	end
	
	local secs = mins * 60
	
	local rentwarn = "Your rent runs out on a property in 1 minute!"
	local rentloss = "You didn't pay rent, and have lost a property!"
	
	if( id == -1 ) then
		rentwarn = "Your rent runs out on a free property in 1 minute!"
		rentloss = "Your rent time has run out on a free property!"
		self:GetTable().OwnsFreeProperty = true;
	end
	
	timer.Simple( secs - 60, function()
			door:UnownProperty( self )
			self:PrintMessage( 3, rentwarn );
	end );
	
	timer.Simple( secs, function()
			door:UnownProperty( self )
			self:PrintMessage( 3, rentloss );
	end );

end

function meta:OwnDoor( door ) 

	if( door:GetTable().MainOwner == nil ) then
		door:GetTable().MainOwner = self;
		door:GetTable().MainOwnerSteamID = self:SteamID();
		door:SetNWInt( "Owned", 1 );
	else
		if( door:GetTable().Owners == nil ) then door:GetTable().Owners = { }; end
		if( door:GetTable().OwnersBySteamID == nil ) then door:GetTable().OwnersBySteamID = { }; end
		 
		door:GetTable().OwnersBySteamID[#door:GetTable().Owners + 1] = self:SteamID();
		door:GetTable().Owners[#door:GetTable().Owners + 1] = self;	
		
	end					
	
end

local meta = FindMetaTable( "Entity" );

function meta:GetDoorOwnersList()

	local tbl = { }
	
	for k, v in pairs( self:GetTable().Owners ) do
	
		table.insert( tbl, v );
	
	end
	
	table.insert( tbl, self:GetTable().MainOwner );
	
	return tbl;

end

function meta:UnownProperty( ply )

	local parent = self:GetTable().PropertyParent;
	
	--self:UnownDoor( ply );
	
	for k, v in pairs( TS.MapDoors ) do
	
		if( v.PropertyParent == parent ) then
		
			if( v.Door:OwnsDoor( ply ) ) then
				v.Door:UnownDoor( ply );
			end
			
			
		end
	
	end

end

function meta:UnownDoor( ply )

	if( self:GetTable().MainOwner == ply ) then
	
		self:GetTable().MainOwner = nil;
		self:GetTable().MainOwnerSteamID = nil;
		
		if( self:GetTable().DoorPrice == 0 ) then
					ply:GetTable().OwnsFreeProperty = false;
		end
		
		self:SetNWInt( "Owned", 0 );
	
	else
	
		if( self:GetTable().Owners == nil ) then self:GetTable().Owners = { }; end
		if( self:GetTable().OwnersBySteamID == nil ) then self:GetTable().OwnersBySteamID = { }; end
		
		for k, v in pairs( self:GetTable().Owners ) do
		
			if( v == ply ) then
			
				self:GetTable().Owners[k] = nil;
				self:GetTable().OwnersBySteamID[k] = nil;
				return;
			
			end
		
		end
	
	end
						

end

function meta:HasNoOwners()

	if( self:GetTable().MainOwner == nil ) then
	
		if( self:GetTable().Owners == nil ) then return true; end
	
		local nvalid = 0;
		
		for k, v in pairs( self:GetTable().Owners ) do
		
			if( v:IsValid() ) then
			
				nvalid = nvalid + 1;
			
			end
		
		end
		
		if( nvalid == 0 ) then return true; end
	
	end

	return false;

end

function meta:OwnsDoor( ply )

	if( ply:IsCombine() or ply:HasInventory( "Metrocop Jacket" ) ) then			
		if( string.find( self:GetTable().DoorFlags, "o" ) ) then			
			return true;		
		end		
	end

	if( self:GetTable().MainOwner == ply ) then
		
		return true;
		
	else
		
		if( self:GetTable().Owners == nil ) then self:GetTable().Owners = { }; end
	
		for k, v in pairs( self:GetTable().Owners ) do
			
			if( v == ply ) then
				
				return true;
					
			end
			
		end
		
	end	
	
	return false;

end

--  not really to do with door ownership

function meta:SplodeDoor()

		local pos = self:GetPos();
		local ang = self:GetAngles();
		local model = self:GetModel();
		local skin = self:GetSkin();

		local exp = ents.Create( "env_explosion" );
		exp:SetKeyValue( "spawnflags", 128 );
		exp:SetPos( pos );
		exp:Spawn();
		exp:Fire( "explode", "", 0 );
		
		local exp = ents.Create( "env_physexplosion" );
		exp:SetKeyValue( "magnitude", 150 );
		exp:SetPos( pos );
		exp:Spawn();
		exp:Fire( "explode", "", 0 );
			
		timer.Simple( 1.0, exp.Remove, exp );
		
		self:BreakDoor();
		
end

function meta:BreakDoor()

	local pos = self:GetPos();
	local ang = self:GetAngles();
	local model = self:GetModel();
	local skin = self:GetSkin();

		self:SetNotSolid( true );
		self:SetNoDraw( true );
		self:Fire( "open", "", 0 );
					
		local function ResetDoor( door, fakedoor )
			door:SetNotSolid( false );
			door:SetNoDraw( false );
			door:GetTable().BoobyTrapped = false;
			fakedoor:Remove();
		end			

		local ent = ents.Create( "prop_physics" );
		ent:SetPos( pos );
		ent:SetAngles( ang );
		ent:SetModel( model );
		if( skin ) then
			ent:SetSkin( skin );
		end
		ent:Spawn();
												
		timer.Simple( 300, ResetDoor, self, ent );

end
