--------------------------------------------------------------------------------
--  File     : Hut_1st_floor_Lantern_MG
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
LOC_1FLOOR				= "Hut_1st_floor"

I_LANTERN				= "LANTERN"			--несобранный фонарь
I_SUPER_LANTERN			= "SUPER_LANTERN"	--волшебный фонарь
I_MATCHES				= "BOX_OF_MATCHES"	--спички

I_LIGHT					= "LIGHT"			--свет
I_LIGHT1				= "LIGHT1"
I_LIGHT2				= "LIGHT2"
I_LIGHT3				= "LIGHT3"
I_FIRE					= "FIRE"
I_FIRE_REFLECTION		= "FIRE_REFLECTION"
I_LANTERN_LIGHTEN		= "LANTERN_LIGHTEN"

I_WIRE					= "WIRE"			--проволка
I_WIRE_PLACED			= "WIRE_PLACED"	
I_WIRE_CURSOR			= "WIRE_ONCURSOR"
I_FORK					= "FORK"			--вилка
I_FORK_PLACED			= "FORK_PLACED"	
I_FORK_CURSOR			= "FORK_ONCURSOR"
I_MIRROR				= "MIRROR"			--зеркало
I_MIRROR_PLACED			= "MIRROR_PLACED"	
I_MIRROR_CURSOR			= "MIRROR_ONCURSOR"
I_LENS					= "LENS"			--линза
I_LENS_PLACED			= "LENS_PLACED"	
I_LENS_CURSOR			= "LENS_ONCURSOR"
I_MEDALLION				= "MEDALLION"		--медальон
I_MEDALLION_PLACED		= "MEDALLION_PLACED"
I_MEDALLION_CURSOR		= "MEDALLION_ONCURSOR"
--------------------------------------------------------------------------------
function OnSerialize(system)
	collect = system:Serialize(collect)
end	
--------------------------------------------------------------------------------
function OnFirstEntry()
	collect = 0
end
--------------------------------------------------------------------------------
function OnEntry ()
	ToGetItemFromInventory(I_WIRE_CURSOR, I_WIRE)
	ToGetItemFromInventory(I_FORK_CURSOR, I_FORK)
	ToGetItemFromInventory(I_MIRROR_CURSOR, I_MIRROR)
	ToGetItemFromInventory(I_LENS_CURSOR, I_LENS)
	ToGetItemFromInventory(I_MEDALLION_CURSOR, I_MEDALLION)
end
--------------------------------------------------------------------------------
function OnMouseClickOnBack()
	OnNeedReturnItemToRoom()
	chapter:ExitCloseup()
	return true
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if (itemName == "BACKGROUND") and (item:IsEnabled()) then	--место для сборки фонаря
		comment:Add("CMT_Table_Player")
		return true
	end
	
	if itemName == I_LANTERN then								
		if collect == 0 then									--несобранный фонарь
			comment:Add("CMT_Lantern2_Player")
		elseif collect == 1 then								--собранный, но не заженный фонарь
			comment:Add("CMT_Lantern1_Player")
		end
		return true
	end
	
	if itemName == I_LANTERN_LIGHTEN then						--волшебный фонарь
		room:GetItem(I_LANTERN_LIGHTEN):Hide()
		room:GetItem(I_LIGHT):Hide()
		room:GetItem(I_WIRE_PLACED):Hide()
		room:GetItem(I_MEDALLION_PLACED):Hide()
		room:GetItem(I_MIRROR_PLACED):Hide()
		room:GetItem(I_LENS_PLACED):Hide()
		room:GetItem(I_FORK_PLACED):Hide()
		room:GetItem("LAMP"):Hide()
		
		cstore:SetKey("Close_up_Lantern", 3)
		pick_panel:AddItems(I_SUPER_LANTERN, true)
		room:MarkCompleted()
		return true
	end
	
	if itemName == I_WIRE then									--проволка
		item:Hide()
		room:SetItemOnCursor(room:GetItem(I_WIRE_CURSOR))
		return true
	elseif itemName == I_FORK then								--вилка
		item:Hide()
		room:SetItemOnCursor(room:GetItem(I_FORK_CURSOR))
		return true
	elseif itemName == I_MIRROR then							--зеркало
		item:Hide()
		room:SetItemOnCursor(room:GetItem(I_MIRROR_CURSOR))
		return true
	elseif itemName == I_LENS then								--линза
		item:Hide()
		room:SetItemOnCursor(room:GetItem(I_LENS_CURSOR))
		return true
	elseif itemName == I_MEDALLION then							--медальон
		item:Hide()
		room:SetItemOnCursor(room:GetItem(I_MEDALLION_CURSOR))
		return true
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)					
	local itemName = item:GetName()
	
	--на фонарь
	if (itemName == I_LANTERN) or
		(itemName == I_LENS_PLACED) or
		(itemName == I_MIRROR_PLACED) or
		(itemName == I_FORK_PLACED) or
		(itemName == I_WIRE_PLACED) or
		(itemName == I_MEDALLION_PLACED) then
		   
		if cursor:IsItemNameEqual(I_WIRE_CURSOR) then			--проволка	
			ToApplyItemOnLantern(I_WIRE_PLACED, I_WIRE)
			room.sfx:Play ("insert_wire")	--звук "вставили проволоку"
			return true
		end
		   
		if cursor:IsItemNameEqual(I_FORK_CURSOR) then			--вилка	
			ToApplyItemOnLantern(I_FORK_PLACED, I_FORK)
			room.sfx:Play ("insert_fork")	--звук "вставили вилку"
			return true
		end
			
		if (cursor:IsItemNameEqual(I_LENS_CURSOR)) and			--линза
			(room:GetItem(I_FORK_PLACED):IsVisible()) then
			ToApplyItemOnLantern(I_LENS_PLACED, I_LENS)
			room.sfx:Play ("insert_glass")	--звук "вставили линзу"
			return true
		end
			
		if (cursor:IsItemNameEqual(I_MEDALLION_CURSOR)) and		--медальон
		(room:GetItem(I_LENS_PLACED):IsVisible()) then		
			ToApplyItemOnLantern(I_MEDALLION_PLACED, I_MEDALLION)
			room.sfx:Play ("hang_amulet")	--звук "повесили медальон"
			if room:GetItem(I_MIRROR_PLACED):IsVisible() then
				AllSolved ()
			end
			return true
		end
			
		if (cursor:IsItemNameEqual(I_MIRROR_CURSOR)) and		--зеркало
			(room:GetItem(I_WIRE_PLACED):IsVisible()) then
			ToApplyItemOnLantern(I_MIRROR_PLACED, I_MIRROR)
			room.sfx:Play ("hang_mirror")	--звук "повесили зеркало"
			if room:GetItem(I_MEDALLION_PLACED):IsVisible() then
				AllSolved ()
			end
			return true
		end
	end
			
	--на стол
	if itemName == "BACKGROUND" then 				
		if cursor:IsItemNameEqual(I_WIRE_CURSOR) then			--проволка	
			ToPutItemOnTable(I_WIRE_CURSOR, I_WIRE)
			room.sfx:Play ("drop_wire")	--звук "положили проволку на стол"
			return true
		end
			
		if cursor:IsItemNameEqual(I_FORK_CURSOR) then			--вилка		
			ToPutItemOnTable(I_FORK_CURSOR, I_FORK)
			room.sfx:Play ("drop_fork")	--звук "положили вилку на стол"
			return true
		end
			
		if cursor:IsItemNameEqual(I_MIRROR_CURSOR) then			--зеркало		
			ToPutItemOnTable(I_MIRROR_CURSOR, I_MIRROR)
			room.sfx:Play ("drop_mirror")--звук "положили зеркало на стол"
			return true
		end
			
		if cursor:IsItemNameEqual(I_LENS_CURSOR) then			--линза		
			ToPutItemOnTable(I_LENS_CURSOR, I_LENS)
			room.sfx:Play ("drop_glass")--звук "положили линзу на стол"
			return true
		end
			
		if cursor:IsItemNameEqual(I_MEDALLION_CURSOR) then		--медальон		
			ToPutItemOnTable(I_MEDALLION_CURSOR, I_MEDALLION)
			room.sfx:Play ("drop_amulet")--звук "положили медальон на стол"
			return true
		end
	end
	
	if (itemName == I_LANTERN) and	(collect == 1) then			--спички на лампу
		if cursor:IsItemNameEqual(I_MATCHES) then
			cursor:RemoveItem()	
			item:Hide()
			room:GetItem(I_LANTERN_LIGHTEN):Show()
			room:GetItem(I_LIGHT):Show()
			room:GetItem("LAMP"):Show()
			
			music:Stop(true)
			music:PlayLoopWithDelay(10, "magic_light")
			cstore:SetKey("Close_up_Lantern", 2)
			room.sfx:Play ("fireshow")	--звук "огивом зажигаем волшебный фонарь"
			return true
		end
	end
end
--------------------------------------------------------------------------------
function OnSetItemOnCursor (InventoryItem)
	ToGetItemFromInventory(I_WIRE_CURSOR, I_WIRE)
	ToGetItemFromInventory(I_FORK_CURSOR, I_FORK)
	ToGetItemFromInventory(I_MIRROR_CURSOR, I_MIRROR)
	ToGetItemFromInventory(I_LENS_CURSOR, I_LENS)
	ToGetItemFromInventory(I_MEDALLION_CURSOR, I_MEDALLION)
end
--------------------------------------------------------------------------------
function OnClickNonISpyOnInventory () 
	ToPutItemToInventory(I_WIRE_CURSOR, I_WIRE)
	ToPutItemToInventory(I_FORK_CURSOR, I_FORK)
	ToPutItemToInventory(I_MIRROR_CURSOR, I_MIRROR)
	ToPutItemToInventory(I_LENS_CURSOR, I_LENS)
	ToPutItemToInventory(I_MEDALLION_CURSOR, I_MEDALLION)
end
--------------------------------------------------------------------------------
function OnNeedReturnItemToRoom()
	if inventory.alt:IsItemExisting(I_WIRE) then
		ToPutItemToInventory(I_WIRE_CURSOR, I_WIRE)
	else
		ToPutItemOnTable(I_WIRE_CURSOR, I_WIRE)
	end
	
	if inventory.alt:IsItemExisting(I_FORK) then
		ToPutItemToInventory(I_FORK_CURSOR, I_FORK)
	else
		ToPutItemOnTable(I_FORK_CURSOR, I_FORK)
	end
	
	if inventory.alt:IsItemExisting(I_MIRROR) then
		ToPutItemToInventory(I_MIRROR_CURSOR, I_MIRROR)
	else
		ToPutItemOnTable(I_MIRROR_CURSOR, I_MIRROR)
	end
	
	if inventory.alt:IsItemExisting(I_LENS) then
		ToPutItemToInventory(I_LENS_CURSOR, I_LENS)
	else
		ToPutItemOnTable(I_LENS_CURSOR, I_LENS)
	end
	
	if inventory.alt:IsItemExisting(I_MEDALLION) then
		ToPutItemToInventory(I_MEDALLION_CURSOR, I_MEDALLION)
	else
		ToPutItemOnTable(I_MEDALLION_CURSOR, I_MEDALLION)
	end
end
--------------------------------------------------------------------------------
-------------------------------фонарь собран------------------------------------
function AllSolved () 
	collect = 1
	
	room:GetItem(I_LENS_PLACED):SetClickable(false)
	room:GetItem(I_FORK_PLACED):SetClickable(false)
	room:GetItem(I_MIRROR_PLACED):SetClickable(false)
	room:GetItem(I_MEDALLION_PLACED):SetClickable(false)
	room:GetItem(I_WIRE_PLACED):SetClickable(false)
end
----------------------положить части фонаря на стол-----------------------------
function ToPutItemOnTable(nameItemOnCursor, nameItemOnTable)
	if cursor:IsItemNameEqual(nameItemOnCursor) then
		cursor:RemoveItem()
		room:GetItem(nameItemOnTable):Show()
		if inventory.alt:IsItemExisting(nameItemOnTable) then
			inventory.alt:EraseOneItemFromCell(nameItemOnTable)
		end
		room:GetItem("BACKGROUND"):Disable()
	end
end
----------------------положить части фонаря в инвентарь-----------------------------
function ToPutItemToInventory(nameItemOnCursor, nameItemInInventory)
	if cursor:IsItemNameEqual(nameItemOnCursor) then
		if inventory.alt:IsItemExisting(nameItemInInventory) then
			cursor:RemoveItem()
			inventory:AddInventoryItem(inventory.alt:GetItem(nameItemInInventory))
			inventory.alt:EraseOneItemFromCell(nameItemInInventory)
		end
	end
end
------------------------взять части фонаря из инвентаря-------------------------
function ToGetItemFromInventory(nameItemOnCursor, nameItemInInventory)
	if cursor:IsItemNameEqual(nameItemInInventory) then
		inventory.alt:AddItemFromCursor()
		room:SetItemOnCursor(room:GetItem(nameItemOnCursor))
	end
end
------------------------применить части фонаря на фонарь-------------------------
function ToApplyItemOnLantern(nameItemPlaced, nameItemInInventory)
	cursor:RemoveItem()
	local itemPlaced = room:GetItem(nameItemPlaced)
	itemPlaced:Show()
	if inventory.alt:IsItemExisting(nameItemInInventory) then
		inventory.alt:EraseOneItemFromCell(nameItemInInventory)
	end
	room:GetItem("BACKGROUND"):Disable()
end
