# agentned


if $ARGS[0] = 'start':
	CLOSE ALL
	
	*clr & cla
	$loc = 'agentned'
	$loc_arg = $ARGS[0]
	$location_type = 'indoors'
	$menu_loc = 'agentned'
	$menu_arg = 'start'
	menu_off = 0
	
	gs'stat'
	gs'dina'
!!	gs 'housing', 'rent'
	
	'<center><b><font color = maroon>Kirsanova Real Estate Agency</font></b></center>'
	'<center><img <<$set_imgh>> src="images/locations/city/citycenter/realestate/agenstvo.jpg"></center>'
	*nl
	'This is the head office of the largest real estate agency in the Leningrad Oblast.'
	*nl
	'At the reception, you notice a large print banner:'
	'<b>"Please be aware that all sales transactions are made exclusively through your bank account. Rent payments are excluded from this policy."</b>'

	act 'View rentals': gt 'agentned', 'rent'
	act 'View properties for sale': gt 'agentned', 'buy'

	!! Get a list of the properties and their attributes that the player rented:
	!! Returns: $property_name[i], $property_code[i], $property_display[i], property_rent[i], property_days[i]
	gs 'homes_properties', 'get_rented_properties', 'home'
	count = ARRSIZE('$property_name')
	if count > 0:
		i = 0
		'<h4>Rent Information</h4>'
		:listing_rented_properties_loop
			'You have <B><<property_days[i]>> days</B> remaining on the lease of your <<$property_display[i]>>, and your monthly rent is <<func(''agentned'', ''format_price_string'', property_rent[i])>><b>₽</b>. '+iif(money >= property_rent[i],'You can make a rent <a href="exec: gs ''homes_properties'', ''pay_rent'', ''<<$property_code[i]>>'', ''cash'' & gt ''agentned'', ''start''">payment</a> in cash to extend it.','You can''t afford to pay your rent at the moment')
				!! TODO: Make the duplication way less.
				if $property_code[i] = 'city_apartment':
					act 'Cancel the lease of the <<$property_display[i]>>':
						cla & *nl
						!! TODO: 1 month notice period like in real life?
						'Attention: You won''t be able to return to your apartment when you cancel the lease.'
						act 'Return': gt'agentned', 'start'
						act 'Cancel it': gs 'homes_properties', 'cancel_rent', 'city_apartment' & gt'agentned', 'start'
					end
				elseif $property_code[i] = 'old_town_apartment':
					act 'Cancel the lease of the <<$property_display[i]>>':
						cla & *nl
						!! TODO: 1 month notice period like in real life?
						'Attention: You won''t be able to return to your apartment when you cancel the lease.'
						act 'Return': gt'agentned', 'start'
						act 'Cancel it': gs 'homes_properties', 'cancel_rent', 'old_town_apartment' & gt'agentned', 'start'
					end
				end
			i += 1
		if i < count: jump 'listing_rented_properties_loop'
	end
	killvar 'count' & killvar 'i'
	
	!! Get a list of the properties and their attributes that the player rented:
	!! $property_code[], $property_name[], $property_display[], $property_status_display[], $property_type[]
	!! property_construction_status, property_is_renovated[], property_sales_price[], property_renovation_value[]
	gs 'homes_properties', 'get_owned_properties', 'home'
	count = ARRSIZE('$property_name')
	if count > 0:
		'<h4>You own the following properties</h4>'
		i = 0
		:listing_owned_properties_loop
			$property_status_display[i]
			i += 1
		if i < count: jump 'listing_owned_properties_loop'
		*nl
		'We are at your service if you decide to <a href="exec:gt ''agentned'',''sell''">sell</a>.'
		*nl
	end
	gs 'homes_properties', 'clean_up_property_data'
	killvar 'count' & killvar 'i' & killvar 'j'
	act 'Leave': minut += 5 & gt 'city_center'
end

if $ARGS[0] = 'buy':
	*clr & cla
	$loc = 'agentned'
	$loc_arg = $ARGS[0]
	$location_type = 'indoors'
	$menu_loc = 'agentned'
	$menu_arg = 'buy'
	menu_off = 0
	gs 'housing', 'sale'
	gs 'homes_properties', 'clean_up_property_data'
	'<center><b><font color = maroon>Real Estate Agency</font></b></center>'
	'<center><img <<$set_imgh>> src="images/locations/city/citycenter/realestate/agenstvo.jpg"></center>'
	*nl
	'<h4> The purchase of any properties requires a bank account. The Agency is not conducting business in cash in compliance with the Anti-Money Laundering Regulations</h4>'
	*nl
	minut += 5
	gs 'stat'
	
	act 'Return':gt'agentned', 'start'
	
	!! "Returns": 
	!! $property_code[], $property_name[], $property_display[], $property_agency_name[], 
	!! property_construction_status,property_is_renovated[], property_sales_price[],
	!! property_type[]
	gs 'homes_properties', 'get_properties_for_sale'
	count = ARRSIZE('$property_name')
	if count > 0:
		i = 0
		'<h4>We have the following properties for sales</h4>'
		:listing_properties_for_sale_loop	
			if property_is_rented[i] ! 0:
				'You could buy the <<$property_display[i]>> you''re currently renting, for <<func(''agentned'', ''format_price_string'', property_sales_price[i])>> <b>₽</b>.'
			else	
				'A(n) <<$property_display[i]>> is available for sale for <<func(''agentned'', ''format_price_string'', property_sales_price[i])>> <b>₽</b>.'
			end
			if karta + bankDebtLimit >= property_sales_price[i]: 
				if $property_code[i] = 'city_apartment': act 'Buy the <<$property_display[i]>>': gs 'agentned', 'buy_property', 'city_apartment'
				if $property_code[i] = 'village_cottage': act 'Buy the <<$property_display[i]>>': gs 'agentned', 'buy_property', 'village_cottage'
				if $property_code[i] = 'matryona_mansion': act 'Buy the <<$property_display[i]>>': gs 'agentned', 'buy_property', 'matryona_mansion'
				if $property_code[i] = 'city_house': act 'Buy the <<$property_display[i]>>': gs 'agentned', 'buy_property', 'city_house'
			end
			i += 1
		if i < count: jump 'listing_properties_for_sale_loop'
	else
		'We currently have no properties listed for sale.'
	end
	gs 'homes_properties', 'clean_up_property_data'
	killvar 'count' & killvar 'i'
end

if $ARGS[0] = 'buy_property':
	*clr & cla
	menu_off = 1
	
	gs 'homes_properties', 'get_property_sales_info', $ARGS[1]
	minut += 30
	karta -= property_sales_price
	
	gs 'homes_properties', 'buy_property', $ARGS[1], property_sales_price
	$set_homeyn = input("Would you like to set this as your new home? (yes/no)")
	if $set_homeyn = 'yes': gs 'homes_properties', 'set_home', $ARGS[1]
	killvar '$set_homeyn'

	!!TODO: this etoexhib will need some rethinking I get what it means by now, but some better name would help a lot.
	!!NOTE: It means how "famous" Sveta is for being an exhibitionist.
	if $ARGS[1] = 'village_cottage': etoexhib = 0
	gs'stat'

	'<center><b><font color = maroon>Real Estate Agency</font></b></center>'
	'<center><img <<$set_imgh>> src="images/locations/city/citycenter/realestate/agenstvo_paperwork.jpg"></center>'
	*nl
	'You spend half an hour filling in the paperwork to buy the <<$property_display>>. <<func(''agentned'', ''format_price_string'', property_sales_price)>><b>₽</b> has been removed from your bank account.'
	killvar 'property' & killvar 'property_code' & killvar 'property_sales_price' & killvar 'property_display'
	act 'Return':gt'agentned', 'start'
end

!! NOTE: When a property is sold, it can be bought back. To avoid a guaranteed income from the 
!!       "I buy property for X, renovate it, sell it for X+Y, then buy it for X, then repeat" loop,
!!       the sales price of the property is updated with the price that it was sold for.
if $ARGS[0] = 'sell':
	*clr & cla
	$loc = 'agentned'
	$loc_arg = $ARGS[0]
	$location_type = 'indoors'
	$menu_loc = 'agentned'
	$menu_arg = 'sell'
	menu_off = 0
	gs 'homes_properties', 'clean_up_property_data'
	
	'<center><b><font color = maroon>Real Estate Agency</font></b></center>'
	'<center><img <<$set_imgh>> src="images/locations/city/citycenter/realestate/agenstvo.jpg"></center>'
	*nl
	'<h4>Any purchase or sale of a property requires a bank account. The Agency is not conducting business in cash in compliance with the Anti-Money Laundering Regulations</h4>'
	*nl
	act 'Return': gs 'homes_properties', 'clean_up_property_data' & gt'agentned', 'start'
	
	if bankAccount = 1:
		!! Get a list of the properties and their attributes that the player rented:
		!! $property_code[], $property_name[], $property_display[], $property_status_display[], $property_type[]
		!! property_construction_status, property_is_renovated[], property_sales_price[], property_renovation_value[]
		gs 'homes_properties', 'get_owned_properties', 'home'
		
		count = ARRSIZE('$property_name')
		if count > 0:
			'<h4>Available offers for your properties</h4>'
			i = 0
			:listing_offers_on_owned_properties_loop
				if $property_code[i] ! 'matryona_mansion' or ($property_code[i] = 'matryona_mansion' and $property_construction_status[i] = 0):
					!! NOTE: Offer: (last sales price + rand(-10%, +10%)) + (renovation value + rand(0%,+10%)
					!! 		 No need to check what has been renovated, the only thing that matters is the total value of the renovation
					!! 		 If Sveta sells, this will be stored as the new sales price of the property
					
					offer = property_sales_price + property_renovation_value + (property_sales_price*rand(-10,10))/100 + (property_renovation_value*rand(0,10))/100
					'We have found a buyer for your <<$property_display[i]>>. They will offer <<func(''agentned'', ''format_price_string'', offer)>><b>₽</b>, minus 5% for fees and taxes to the value of <<func(''agentned'', ''format_price_string'', ( (offer*5)/100) )>>, bringing the total to <<func(''agentned'', ''format_price_string'', ( (offer*95)/100 ) )>>'
					if $property_code[i] = 'city_apartment': act 'Sell the <<$property_display[i]>>': gs 'agentned', 'finalise_sale', 'city_apartment'
					if $property_code[i] = 'village_cottage': act 'Sell the <<$property_display[i]>>': gs 'agentned', 'finalise_sale', 'village_cottage'
					if $property_code[i] = 'matryona_mansion': act 'Sell the <<$property_display[i]>>': gs 'agentned', 'finalise_sale', 'matryona_mansion'
					if $property_code[i] = 'city_house': act 'Sell the <<$property_display[i]>>': gs 'agentned', 'finalise_sale', 'city_house'
				end
				i += 1
			if i < count: jump 'listing_offers_on_owned_properties_loop'
			
		end
		gs 'homes_properties', 'clean_up_property_data'
		killvar 'count' & killvar 'i' & killvar 'j'
		act 'Leave': minut += 5 & gt 'city_center'

!! NOTE: Keeping this here for future renovation related reference.
!!{ 
	if bankAccount = 1:
			if rembedr = 1 and remsitr = 1 and remkorr = 1 and remvanr = 1 and remkuhr = 1:
				!Renovated city residential apartment
				offer1 = ((800000 + 250000) + rand(-100000, 100000)) 
			else
				offer1 = (800000 + rand(-100000, 100000))
			end
		
			if func('homes_properties', 'property_renovated', 'village_cottage') = 1: 
				!Renovated communal cottage
				offer2 = ((prop_price['village_cottage'] + 200000) + rand(-10000, 10000))
			else
				!Communal cottage
				offer2 = (prop_price['village_cottage'] + rand(-5000, 5000))
			end
}
	else
		'<b>You need to open a bank account to sell your property</b>'
	end
end

if $ARGS[0] = 'finalise_sale':
	*clr
	menu_off = 1
	gs 'homes_properties', 'get_property_sales_info', $ARGS[1]
	'<center><b><font color = maroon>Real Estate Agency</font></b></center>'
	'<center><img <<$set_imgh>> src="images/locations/city/citycenter/realestate/agenstvo_paperwork.jpg"></center>'
	minut += 30
	'You spend thirty minutes filling out the paperwork for the sale of your <<$property_display>>. <<func(''agentned'', ''format_price_string'', ( (offer*95)/100) )>><b>₽</b> has been paid into your bank account.'
	gs 'homes_properties', 'sell_property', $ARGS[1], offer
	karta += (offer*95)/100
	delact 'Sell the <<$property_display>>'
	gs 'stat'
	killvar 'property_display' & killvar 'property_sales_price'
end


if $ARGS[0] = 'rent':
	*clr & cla
	$loc = 'agentned'
	$loc_arg = $ARGS[0]
	$location_type = 'indoors'
	$menu_loc = 'agentned'
	$menu_arg = 'rent'
	menu_off = 0
	'<center><b><font color = maroon>Real Estate Agency</font></b></center>'
	'<center><img <<$set_imgh>> src="images/locations/city/citycenter/realestate/agenstvo.jpg"></center>'
	*nl
	minut += 5
	gs 'stat'
	
	act 'Return': killvar 'rent_code' & killvar 'monthly_rent' & gt'agentned', 'start'
	
	gs 'homes_properties', 'get_properties_for_rent', 'home'
	count = ARRSIZE('property_name')

	if count > 0:
		i = 0
		'<h4>Properties currently available for rent</h4>'
		:listing_properties_for_rent_loop
			'We have a(n) <<$property_display[i]>> avaible for a monthly rent of <<func(''agentned'', ''format_price_string'', property_rent[i])>><b>₽</b>.'
			monthly_rent[$property_code[i]] = property_rent[i]
			if $property_code[i] = 'city_apartment': act 'View the <<$property_display[i]>> details': gt 'agentned', 'view_property_details', 'city_apartment', monthly_rent['city_apartment']
			if $property_code[i] ='old_town_apartment': act 'View the <<$property_display[i]>> details': gt 'agentned', 'view_property_details', 'old_town_apartment', monthly_rent['old_town_apartment']
			i += 1
		if i < count: jump 'listing_properties_for_rent_loop'
	else
		'We currently have no properties listed for rent.'
	end
	*nl
	gs 'homes_properties', 'clean_up_property_data'
	killvar 'count' & killvar 'i'
end

if $ARGS[0] = 'view_property_details':
	*clr & cla
	menu_off = 1
	'<center><b><font color = maroon>Real Estate Agency</font></b></center>'
	'<center><img <<$set_imgh>> src="images/locations/city/citycenter/realestate/agenstvo.jpg"></center>'
	*nl
	'<<func(''agentned'', ''get_rent_detail_dialogue'', $ARGS[1] )>> The receptionist says as he starts pulling out the paperwork, "Rent is <<func(''agentned'', ''format_price_string'', ARGS[2] )>><b>₽</b> and leases have to be renewed every 30 days. Payment will be automatically deducted from your account or you can come into the office to pay. Last, but not least, you must make pay the first month when you sign for the lease."'
	'<br>He pulls out a pen and slides the paperwork onto the counter. "Interested? You can move in right away," he says calmly.'
	
	if money >= ARGS[2] or karta+bankDebtLimit >= ARGS[2]:
		act 'Agree and pay': 
			*clr & cla
			cls
			minut += 30

			if money >= ARGS[2]:
				money -= ARGS[2]
			else
				karta -= ARGS[2]
			end

			gs 'homes_properties', 'rent_property', $ARGS[1]
			if func( 'homes_properties', 'get_accessible_property_count', 'home') = 1:
				gs 'homes_properties', 'set_home', $ARGS[1]
			else
				$set_homeyn = input("Would you like to set this as your new home? (yes/no)")
				if $set_homeyn = 'yes': gs 'homes_properties', 'set_home', $ARGS[1]
				killvar '$set_homeyn'
			end
			
			gs'stat'
			'<center><b><font color = maroon>Real Estate Agency</font></b></center>'
			'<center><img <<$set_imgh>> src="images/locations/city/citycenter/realestate/agenstvo_paperwork.jpg"></center>'
			*nl
			'You take the pen then start filling out the paperwork. After writing for half an hour, you hand over the money and receive the keys to your new apartment.'
			act 'Return':gt'agentned', 'start'
		end
	else
		'You do not have enough money with you or in your bank account to pay the rent.'
	end	

	act 'Decline':
		*clr & cla
		minut += 5
		'<center><b><font color = maroon>Real Estate Agency</font></b></center>'
		'<center><img <<$set_imgh>> src="images/locations/city/citycenter/realestate/agenstvo.jpg"></center>'
		*nl
		'You shake your head, "Actually, I need more time to think about this."'
		'<br>The receptionist seems a bit miffed, but says nothing as he takes back the paperwork and stuffs it back into his desk drawer.'

		act 'Return':gt'agentned', 'start'
	end
	 killvar 'rent_code' & killvar 'monthly_rent'
end

!! $ARGS[1] - the code of the property.
if $ARGS[0] = 'get_rent_detail_dialogue':
	if $ARGS[1] = 'old_town_apartment': 
		$result = '"We have a small apartment available right now that is in Pushkin’s city center. It’s a little bit pricey, but it’s been recently renovated to be more modern. It looks quite nice, so you won’t find a place like that outside of Pushkin."'
	elseif $ARGS[1] = 'city_apartment': 
		$result = '"We have a flat available right now in the St. Petersburg residential area."'
	else
		gs 'agentned', 'get_rent_detail_dialogue_hook', $ARGS[1]
		$result = $agency_rental_dialogue
		killvar '$agency_rental_dialogue'
	end 
end

!! $ARGS[1] - the code of the property.
if $ARGS[0] = 'get_rent_detail_dialogue_hook':
	!! In a mod just redirect this call and set the value of the variable below to the text
	!! that should be used for the property by the agency.
	$agency_rental_dialogue = ''
end

!! Call: `func('agentned', 'format_price_string', ARGS[1])
!! ARGS[1] - the price (as a number)
if $ARGS[0] = 'format_price_string':
	$worknum = STR(ARGS[1])
	worknumlength = LEN($worknum)
	pos1 = 1
	:format_price_string_loop
		$split = $MID($worknum, pos1, 1)
		if (worknumlength - 1)/3 ! (worknumlength-2)/3:
			$result += $split+','
		else
			$result += $split
		end
		worknumlength -= 1
		pos1 += 1
	if pos2 <= worknumlength: jump 'format_price_string_loop'
	killvar 'pos1' & killvar 'pos2' & killvar 'worknum' & killvar 'worknumlength' & killvar 'split'
end
--- agentned ---------------------------------

