--------------------------------------------------------------------------------
--  File     : Witch's_house_ext_Magic_place
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
function OnSerialize(system)
	potion = system:Serialize(potion)
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	potion = 0
	cstore:SetKey("Pestle_MP", 0)
	cstore:SetKey("Potion_MP", 0)
	
	stars:Show("M_COOKING", "4")
	stars:Show("BOTTLE1_EMPTY", "4")
	stars:Show("BOTTLE2_EMPTY", "4")
	stars:Show("PESTLE_MAGIC_PLACE", "4")
	comment:Add("CMT_Cook_Player")
end
--------------------------------------------------------------------------------
function OnEntry ()
	if cursor:IsItemNameEqual("PESTLE") then	
		inventory.alt:AddItemFromCursor()
		room:SetItemOnCursor(room:GetItem("PESTLE_MAGIC_PLACE"))
	end
end
--------------------------------------------------------------------------------
function OnMouseClickOnBack()
	OnNeedReturnItemToRoom()
	chapter:ExitCloseup()
	return true
end
--------------------------------------------------------------------------------
function OnSetItemOnCursor (InventoryItem)
	if cursor:IsItemNameEqual("PESTLE") then	
		inventory.alt:AddItemFromCursor()
		room:SetItemOnCursor(room:GetItem("PESTLE_MAGIC_PLACE"))
	end
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if itemName == "M_COOKING" then						--место для ингридиентов
		if (potion <  3) or ((potion >=  5) and (potion <  8)) then 
			comment:Add("CMT_Cook1_Player")	
		elseif (potion == 3) or (potion == 8) then 
			comment:Add("CMT_Cook2_Player")
		elseif (potion == 4) or (potion == 9) then 
			comment:Add("CMT_Cook4_Player")
		end
		return true
	
	elseif itemName == "PESTLE_MAGIC_PLACE" then				--пестик
		room:SetItemOnCursor(item)
		return true
	
	elseif itemName == "BOTTLE1_EMPTY" then						--пустая бутылка 1
		room:SetItemOnCursor(item)
		return true
		
	elseif itemName == "BOTTLE2_EMPTY" then						--пустая бутылка 2
		room:SetItemOnCursor(item)
		return true
	
	elseif itemName == "OPEN_ROAD_POTION" then					--зелье дороги
		pick_panel:AddItems(itemName, false)
		cstore:SetKey("OpenRoadPotion", true)
		return true
		
	elseif itemName == "BRIDGE_POTION" then						--зелье моста
		pick_panel:AddItems(itemName, true)
		cstore:SetKey("Close_up_MagicPlace", true)
		room:MarkCompleted()
		return true
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)
	local itemName = item:GetName()

	if itemName == "BACKGROUND" then								--пестик на пень
		if cursor:IsItemNameEqual("PESTLE_MAGIC_PLACE") then
			cursor:RemoveItem()
			if inventory.alt:IsItemExisting("PESTLE") then
				inventory.alt:EraseOneItemFromCell("PESTLE")
			end
			room:GetItem("PESTLE_MAGIC_PLACE"):Show()
			return true
		end
		
		if cursor:IsItemNameEqual("BOTTLE1_EMPTY") then				--пустая бутылка 1
			cursor:RemoveItem()
			room:GetItem("BOTTLE1_EMPTY"):Show()
			return true
		end
		
		if cursor:IsItemNameEqual("BOTTLE2_EMPTY") then				--пустая бутылка 2
			cursor:RemoveItem()
			room:GetItem("BOTTLE2_EMPTY"):Show()
			return true
		end
	end
	
	if itemName == "M_COOKING" then
		if potion < 5 then
			--первое зелье
			if cursor:IsItemNameEqual("FLYTRAP_POISON") then 		--яд мухоловки
				cursor:RemoveItem()
				room:GetItem("FLYTRAP_POISON"):Show()
				room:GetItem("BOTTLE1_EMPTY"):Show()
				if room:GetItem("EGG"):IsVisible() then
					room:GetItem("EGG"):Hide()
					room:GetItem("EGG_POISON"):Show()
				end
				if room:GetItem("PLANTAIN_ROOT"):IsVisible() then
					room:GetItem("PLANTAIN_ROOT_SHADOW"):Hide()
					room:GetItem("N_ROOT_POTION"):Show()
				end
				potion = potion + 1
				room.sfx:Play ("pour_potion")	--звук "выливаем яд"
				return true
			end
			
			if cursor:IsItemNameEqual("VIOLET_ROOT") then 			--корень болотной фиалки
				cursor:RemoveItem()
				room:GetItem("PLANTAIN_ROOT"):Show()
				if room:GetItem("FLYTRAP_POISON"):IsVisible() then
					room:GetItem("N_ROOT_POTION"):Show()
				else
					room:GetItem("PLANTAIN_ROOT_SHADOW"):Show()
				end
				potion = potion + 1
				room.sfx:Play ("put_root")	--звук "кладем корень"
				return true
			end
			
			if cursor:IsItemNameEqual("EGG") then 					--яйцо
				cursor:RemoveItem()
				room:GetItem("EGG_SHELL"):Show()
				if room:GetItem("FLYTRAP_POISON"):IsVisible() then
					room:GetItem("EGG_POISON"):Show()
				else
					room:GetItem("EGG"):Show()
				end
				potion = potion + 1
				room.sfx:Play ("break_egg")		--звук "разбиваем яйцо"
				return true
			end
			
			if cursor:IsItemNameEqual("PESTLE_MAGIC_PLACE") then	--пестик на зелье
				if potion < 3 then
					comment:Add("CMT_Cook3_Player")
				else
					cursor:RemoveItem()
					if inventory.alt:IsItemExisting("PESTLE") then
						inventory.alt:EraseOneItemFromCell("PESTLE")
					end
					room:GetItem("PESTLE_MAGIC_PLACE"):Show()
					
					room:GetItem("PLANTAIN_ROOT"):Hide()
					room:GetItem("N_ROOT_POTION"):Hide()
					room:GetItem("EGG_POISON"):Hide()
					room:GetItem("FLYTRAP_POISON"):Hide()
					room:GetItem("POTION1_DONE"):Show()
					cstore:SetKey("Pestle_MP", 1)
					potion = 4
					room.sfx:Play ("mixing")	--звук "толчем все это колотушкой"
				end
				return true
			end
			
			if (cursor:IsItemNameEqual("BOTTLE1_EMPTY")) and 		--пузырек на зелье
			   (potion == 4) then				
				cursor:RemoveItem()
				room:GetItem("POTION1_DONE"):Hide()
				room:GetItem("OPEN_ROAD_POTION"):Show()
				potion = 5
				cstore:SetKey("Potion_MP", 1)
				room.sfx:Play ("fill_vial")		--звук "набираем в слянку зелье"
				return true
			end
			--end of первое зелье
		
		else
			--второе зелье
			if cursor:IsItemNameEqual("ACORN") then 			--паутина на куриной ноге
				cursor:RemoveItem()
				room:GetItem("ACORN"):Show()
				if room:GetItem("BRIDGE_POTION_UN"):IsVisible() then
					room:GetItem("N_ACORN_POTION"):Show()
				else
					room:GetItem("ACORN_SHADOW"):Show()
				end
				potion = potion + 1
				room.sfx:Play ("put_chicken_leg")	--звук "кладем куриную лапку"
				return true
			end
			
			if cursor:IsItemNameEqual("FUNGUS") then 				--гриб
				cursor:RemoveItem()
				room:GetItem("FUNGUS"):Show()
				if room:GetItem("BRIDGE_POTION_UN"):IsVisible() then
					room:GetItem("N_FUNGUS_POTION"):Show()
				else
					room:GetItem("FUNGUS_SHADOW"):Show()
				end
				potion = potion + 1
				room.sfx:Play ("put_root")	--звук "кладем гриб"
				return true
			end
			
			if cursor:IsItemNameEqual("BRIDGE_POTION_UN") then 		--настой Драконовой травы
				cursor:RemoveItem()
				room:GetItem("BRIDGE_POTION_UN"):Show()
				room:GetItem("BOTTLE2_EMPTY"):Show()
				if room:GetItem("ACORN"):IsVisible() then
					room:GetItem("ACORN_SHADOW"):Hide()
					room:GetItem("N_ACORN_POTION"):Show()
				end
				if room:GetItem("FUNGUS"):IsVisible() then
					room:GetItem("FUNGUS_SHADOW"):Hide()
					room:GetItem("N_FUNGUS_POTION"):Show()
				end
				potion = potion + 1
				room.sfx:Play ("pour_potion")	--звук "льем настой дракона"
				return true
			end
			
			if cursor:IsItemNameEqual("PESTLE_MAGIC_PLACE") then	--пестик на зелье
				if potion < 8 then
					comment:Add("CMT_Cook3_Player")
				else
					cursor:RemoveItem()
					room:GetItem("ACORN"):Hide()
					room:GetItem("N_ACORN_POTION"):Hide()
					room:GetItem("FUNGUS"):Hide()
					room:GetItem("N_FUNGUS_POTION"):Hide()
					room:GetItem("BRIDGE_POTION_UN"):Hide()
					room:GetItem("POTION2_DONE"):Show()
					cstore:SetKey("Pestle_MP", 2)
					potion = 9
					room.sfx:Play ("mixing")	--звук "толчем все это колотушкой"
				end
				return true
			end
			
			if (cursor:IsItemNameEqual("BOTTLE2_EMPTY")) and 		--пузырек на зелье
			   (potion == 9) then	
				cursor:RemoveItem()
				room:GetItem("POTION2_DONE"):Hide()
				room:GetItem("BRIDGE_POTION"):Show()
				potion = 10
				cstore:SetKey("Potion_MP", 2)
				room.sfx:Play ("fill_vial")		--звук "набираем в слянку зелье"
				return true
			end
			-- end of второе зелье
		end
	end
end
--------------------------------------------------------------------------------
function OnClickNonISpyOnInventory () 
	if cursor:IsItemNameEqual("PESTLE_MAGIC_PLACE") then
		if inventory.alt:IsItemExisting("PESTLE") then
			cursor:RemoveItem()
			inventory:AddInventoryItem(inventory.alt:GetItem("PESTLE"))
			inventory.alt:EraseOneItemFromCell("PESTLE")
		end
	end
end
--------------------------------------------------------------------------------
function OnNeedReturnItemToRoom()					
	if cursor:IsItemNameEqual("PESTLE_MAGIC_PLACE") then
		if inventory.alt:IsItemExisting("PESTLE") then
			cursor:RemoveItem()
			inventory:AddInventoryItem(inventory.alt:GetItem("PESTLE"))
			inventory.alt:EraseOneItemFromCell("PESTLE")
		else
			cursor:RemoveItem()
			room:GetItem("PESTLE_MAGIC_PLACE"):Show()
		end
	end
	
	if cursor:IsItemNameEqual("BOTTLE1_EMPTY") then
		cursor:RemoveItem()
		room:GetItem("BOTTLE1_EMPTY"):Show()
	end
	
	if cursor:IsItemNameEqual("BOTTLE2_EMPTY") then
		cursor:RemoveItem()
		room:GetItem("BOTTLE2_EMPTY"):Show()
	end
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Cheat_WitchHouseExtMagicPlace(value)				--выполнить первое зелье  !!!
	room:GetItem("PESTLE_MAGIC_PLACE"):Show()
	room:GetItem("EGG_SHELL"):Show()
	potion = 5
end