--------------------------------------------------------------------------------
--  File     : Smithy_Chemical_lab
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
time_roast = 1.0
time_flower = 1.0
--------------------------------------------------------------------------------
function OnSerialize(system)
	roast = system:Serialize(roast)
	
	EndOffRoast()
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	--inventory:CheatLoadAndAddItem ("GIANT_FLOWER", "Dam", 0)
	--inventory:CheatLoadAndAddItem ("SALT_GLASS", "Mill_ext_Cellar", 0)
	
	room:GetItem("FLOWER_CUT"):Disable()
	roast = 0
	
	stars:Show("BOX_OF_MATCHES", "4")
	stars:Show("M_SPIRIT_LAMP", "4")
	stars:Show("M_CUT_PLACE", "4")
	stars:Show("FLOWER", "4")
	stars:Show("FLOWER_CUT", "4")
	stars:Show("PETRI_DISH1", "4")
	stars:Show("PETRI_DISH2", "4")
	stars:Show("PETRI_DISH_FLOWER", "4")
	stars:Show("PETRI_DISH_SALT", "4")
	stars:Show("BULB", "4")
	stars:Show("FUNNEL", "4")
	stars:Show("M_STAND", "4")
	stars:Show("PETRI_DISH_FLOWER_DONE", "4")
	stars:Show("PETRI_DISH_SALT_STAND", "4")
	stars:Show("CORK", "4")
end
--------------------------------------------------------------------------------
function OnMouseClickOnBack()
	OnNeedReturnItemToRoom()
	chapter:ExitCloseup()
	return true
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if itemName == "BOX_OF_MATCHES"  then						--спички
		room:SetItemOnCursor(item)
		room.sfx:Play ("take_matches")	--звук "взяли спички"
		return true
	end
	
	if (itemName == "FUNNEL")  and								--воронка
	   (item:IsEnabled()) then
		room:SetItemOnCursor(item)
		room.sfx:Play ("glass_take")	--звук "взяли воронку"
		return true
	end
	
	if itemName == "M_SPIRIT_LAMP"  then						--незаженная спиртовка
		comment:Add("CMT_Lamp_Player")
		return true
	end
	
	if (itemName == "PETRI_DISH1")  or							--пустые чашки петри
	   (itemName == "PETRI_DISH2") then
		comment:Add("CMT_Dishes_Player")
		return true
	end
	
	if (itemName == "BULB") or 									--колба
	   ((itemName == "FUNNEL_BULB") and (item:IsEnabled())) then	
		if (room:GetItem("BULB_FLOWER_SALT"):IsVisible()) or 
		   (room:GetItem("BULB_SALT_FLOWER"):IsVisible()) then
			comment:Add("CMT_Kerosene_Player")
			return true
		else
			comment:Add("CMT_Flask_Player")
			return true
		end
	end
	
	if itemName == "M_CUT_PLACE"  then							--место для цветка
		comment:Add("CMT_PlaceForFlower_Player")
		return true
	end
	
	if itemName == "FLOWER"  then								--ненарезанный цветок
		comment:Add("CMT_FlowerCut_Player")
		return true
	end
	
	if (itemName == "FLOWER_CUT") and (item:IsEnabled()) then	--нарезанный цветок
		room:SetItemOnCursor(item)
		room.sfx:Play ("flower_take")	--звук "взяли измельченное"
		return true
	end
	
	if itemName == "PETRI_DISH_FLOWER"  then					--чашка петри с нарезанным цветком
		room:SetItemOnCursor(item)
		room.sfx:Play ("glass_take")	--звук "взяли склянку"
		return true
	end
	
	if (itemName == "PETRI_DISH_FLOWER_DONE") and				--обожженный цветок
	   not(item:IsEnabled()) then
		room:SetItemOnCursor(item)
		if not(room:GetItem("BULB_SALT"):IsVisible()) then
			room:GetItem("M_STAND"):Show()	
		end
		room.sfx:Play ("glass_take")	--звук "взяли склянку с нарезанным цветком (с горелки)"
		return true
	end
	
	if itemName == "PETRI_DISH_SALT"  then						--чашка петри с солью
		room:SetItemOnCursor(item)
		room.sfx:Play ("glass_take")	--звук "взяли склянку"
		return true
	end
	
	if (itemName == "PETRI_DISH_SALT_DONE") and					--прокаленная соль
	   not(item:IsEnabled()) then
		room:SetItemOnCursor(item)
		if not(room:GetItem("BULB_FLOWER"):IsVisible()) then
			room:GetItem("M_STAND"):Show()	
		end
		room.sfx:Play ("glass_take")	--звук "взяли нагретую соль"
		return true
	end
	
	if (itemName == "FUNNEL_BULB") and							--воронка из колбы
	   not(item:IsEnabled()) then
		room:SetItemOnCursor(item)
		room.sfx:Play ("funnel_take")	--звук "воронка достается из бутыля"
		return true
	end
	
	if itemName == "CORK"  then									--пробка
		room:SetItemOnCursor(item)
		room.sfx:Play ("cork_take")	--звук "берется пробка"
		return true
	end
	
	if itemName == "NITROGLYCERINE" then						--нитроглицерин
		if room:GetItem("NITROGLYCERINE_CORK"):IsVisible() then
			room:GetItem("NITROGLYCERINE_CORK"):Hide()
			cstore:SetKey("Close_up_Smithy_Lab", true)
			pick_panel:AddItems(itemName, true)
			room:MarkCompleted()
			return true
		
		else
			comment:Add("CMT_Cork_Player")
			return true
		end
	end

end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)					
	local itemName = item:GetName()
	
	if cursor:IsItemNameEqual("BOX_OF_MATCHES") then				--спички
		if itemName == "M_SPIRIT_LAMP"  then
			item:Hide()
			cursor:RemoveItem()
			local fire = room:GetItem("SPIRIT_FIRE")
			fire:Show()
			fire:PlayAnimation()
			Roast()
			cstore:SetKey("matches_on_lamp", true) 	--!!!
			room.sfx:Play ("match")					--звук "подожгли спиртовку"
			room.sfx:PlayEx ("fire", true, 1, 0)	--звук "спиртовка горит (зацикленный)"
			return true
		else
			BackItem("BOX_OF_MATCHES")
			room.sfx:Play ("drop_matches")			--звук "положили спички"
		end
	end

	if cursor:IsItemNameEqual("FUNNEL") then						--воронка
		if itemName == "BULB"  then					
			cursor:RemoveItem()
			room:GetItem("FUNNEL_BULB"):Show()
			cstore:SetKey("funnel_on_bulb", true) 	--!!!
			room.sfx:Play ("funnel_insert")	--звук "вставили воронку"
			return true
		else
			BackItem("FUNNEL")
			room.sfx:Play ("glass_drop")	--звук "положили воронку"
		end
	end
	
	if (itemName == "PETRI_DISH1")  or								--цветок на чашку петри					
	   (itemName == "PETRI_DISH2") then
		if cursor:IsItemNameEqual("GIANT_FLOWER") then	
			comment:Add("CMT_Cut_Player")
			inventory:AddItemFromCursor()
			return true
		end
	end
	
	if itemName == "M_CUT_PLACE"  then								--цветок на стол
		if cursor:IsItemNameEqual("GIANT_FLOWER") then	
			item:Hide()
			cursor:RemoveItem()
			room:GetItem("FLOWER"):Show()
			room.sfx:Play ("flower_drop")	--звук "положили цветок"
			return true
		end
	end
	
	if itemName == "FLOWER"  then									--нож на цветок
		if cursor:IsItemNameEqual("KNIFE_Q") then	
			inventory.alt:AddItemFromCursor()
			
			local knife_flower = room:GetItem("KNIFE_FLOWER")
			knife_flower:Show()
			knife_flower:PlayAnimation()
			room:AddCtrlAnimation("KNIFE_FLOWER", -1)
			
			room.sfx:Play ("flower_cutting")	--звук "измельчили цветок ножом"
			return true
		end
	end
	
	if (itemName == "BULB") or (itemName == "FUNNEL_BULB") then		--нарезанный цветок на колбу
		if room:GetItem("FUNNEL_BULB"):IsVisible() then
			if cursor:IsItemNameEqual("FLOWER_CUT") then
				comment:Add("CMT_Roast_Player")
				BackItem("FLOWER_CUT")
				room.sfx:Play ("flower_put")	--звук "положили измельченное на стол"
				return true
			end
		end
		
		if cursor:IsItemNameEqual("PETRI_DISH_FLOWER") then
			if room:GetItem("FUNNEL_BULB"):IsVisible() then
				comment:Add("CMT_Roast_Player")
				BackItem("PETRI_DISH_FLOWER")
				room.sfx:Play ("glass_drop")	--звук "поставили склянку на стол"
				return true
			else
				comment:Add("CMT_NeedFunnel_Player")
				BackItem("PETRI_DISH_FLOWER")
				room.sfx:Play ("glass_drop")	--звук "поставили склянку на стол"
				return true
			end
		end
	end
	
	if cursor:IsItemNameEqual("FLOWER_CUT") then					--нарезанный цветок
		if (itemName == "PETRI_DISH1") or (itemName == "PETRI_DISH2") then
			cursor:RemoveItem()
			room:GetItem("PETRI_DISH1"):Hide()
			room:GetItem("PETRI_DISH_FLOWER"):Show()
			cstore:SetKey("cat_flower_on_dish", true) 	--!!!
			room.sfx:Play ("flower_put")	--звук "положили цветок"
			return true
		else
			BackItem("FLOWER_CUT")
			room.sfx:Play ("flower_put")	--звук "положили цветок"
		end
	end
	
	if cursor:IsItemNameEqual("PETRI_DISH_FLOWER") then				--чашка петри с нарезанным цветком
		if itemName == "M_STAND" then
			item:Hide()
			cursor:RemoveItem()
			room:GetItem("PETRI_DISH_FLOWER_STAND"):Show()
			room.sfx:Play ("glass_insert")	--звук "склянка на спиртовку"
			Roast()
			cstore:SetKey("flower_dish_on_lamp", true) 	--!!!
			return true
		else
			BackItem("PETRI_DISH_FLOWER")
			room.sfx:Play ("glass_drop")	--звук "поставили склянку на стол"
		end
	end

	if cursor:IsItemNameEqual("PETRI_DISH_FLOWER_DONE") then		--обожженный цветок
		if (itemName == "BULB") or (itemName == "FUNNEL_BULB")  then
			if room:GetItem("FUNNEL_BULB"):IsVisible() then
				cursor:RemoveItem()
				if room:GetItem("BULB_SALT"):IsVisible() then
					room:GetItem("BULB_SALT"):Hide()
					room:GetItem("BULB_SALT_FLOWER"):Show()
					FlowerAndSaltInBulb()
				else
					room:GetItem("BULB_FLOWER"):Show()
				end
				room.sfx:Play ("flower_pour")	--звук "измельченный жаренный цветок засыпается в воронку"
				cstore:SetKey("flower_done_on_bulb", true) 	--!!!
				return true
			else
				comment:Add("CMT_NeedFunnel_Player")
				Back_PETRI_DISH_FLOWER_DONE()
				room.sfx:Play ("glass_insert")	--звук "склянка на спиртовку"
				return true
			end
		else
			Back_PETRI_DISH_FLOWER_DONE()
			room.sfx:Play ("glass_insert")	--звук "склянка на спиртовку"
		end
	end

	
	if (itemName == "PETRI_DISH1")  or								--соль на чашку петри
	   (itemName == "PETRI_DISH2") then
		if cursor:IsItemNameEqual("SALT_GLASS") then	
			cursor:RemoveItem()
			room:GetItem("PETRI_DISH2"):Hide()
			room:GetItem("PETRI_DISH_SALT"):Show()
			cstore:SetKey("salt_on_dish", true) 	--!!!
			room.sfx:Play ("salt_pour")	--звук "высыпали соль в склянку"
			return true
		end
	end
	
	if (itemName == "BULB") or (itemName == "FUNNEL_BULB") then		--непрокаленная соль на колбу
	    if cursor:IsItemNameEqual("PETRI_DISH_SALT") then
			if room:GetItem("FUNNEL_BULB"):IsVisible() then
				comment:Add("CMT_Roast_Player")
				BackItem("PETRI_DISH_SALT")
				room.sfx:Play ("glass_drop")	--звук "поставили склянку на стол"
				return true
			else
				comment:Add("CMT_NeedFunnel_Player")
				BackItem("PETRI_DISH_SALT")
				room.sfx:Play ("glass_drop")	--звук "поставили склянку на стол"
				return true
			end
		end
	end

	if cursor:IsItemNameEqual("PETRI_DISH_SALT") then				--чашка петри с солью
		if itemName == "M_STAND" then
			item:Hide()
			cursor:RemoveItem()
			room:GetItem("PETRI_DISH_SALT_STAND"):Show()
			room.sfx:Play ("glass_insert")	--звук "склянка на спиртовку"
			Roast()
			cstore:SetKey("salt_dish_on_lamp", true) 	--!!!
			return true
		else
			BackItem("PETRI_DISH_SALT")
			room.sfx:Play ("glass_drop")	--звук "поставили склянку на стол"
		end
	end

	if cursor:IsItemNameEqual("PETRI_DISH_SALT_DONE") then			--прокаленная соль на колбу	
		if (itemName == "BULB") or (itemName == "FUNNEL_BULB")  then
			if room:GetItem("FUNNEL_BULB"):IsVisible() then
				cursor:RemoveItem()
				if room:GetItem("BULB_FLOWER"):IsVisible() then
					room:GetItem("BULB_FLOWER"):Hide()
					room:GetItem("BULB_FLOWER_SALT"):Show()
					FlowerAndSaltInBulb()
				else
					room:GetItem("BULB_SALT"):Show()
				end
				cstore:SetKey("salt_done_on_bulb", true) 	--!!!
				room.sfx:Play ("salt_pour")	--звук "соль засыпается в воронку"
				return true
			else
				comment:Add("CMT_NeedFunnel_Player")
				Back_PETRI_DISH_SALT_DONE()
				room.sfx:Play ("glass_insert")	--звук "склянка на спиртовку"
				return true	
			end
		else
			Back_PETRI_DISH_SALT_DONE()
			room.sfx:Play ("glass_insert")	--звук "склянка на спиртовку"
		end
	end
		
	if cursor:IsItemNameEqual("LAMP") then							--керосин на колбу	
		if (itemName == "BULB") or (itemName == "FUNNEL_BULB")  then	
			if (room:GetItem("BULB_SALT_FLOWER"):IsVisible()) or
			   (room:GetItem("BULB_FLOWER_SALT"):IsVisible()) then
				cursor:RemoveItem()
				room:GetItem("BULB_SALT_FLOWER"):Hide()
				room:GetItem("BULB_FLOWER_SALT"):Hide()
				room:GetItem("BULB"):Hide()
				room:GetItem("NITROGLYCERINE"):Show()
				room:GetItem("FUNNEL_BULB"):Disable()
				stars:Show("FUNNEL_BULB", "4")
				room:GetItem("FUNNEL"):Disable()
				stars:Hide("FUNNEL")
				cstore:SetKey("lamp_on_bulb", true) 	--!!!
				room.sfx:Play ("fuel")	--звук "из лампы заливается горючее в воронку"
				
				local kerosin_add = room:GetItem("KEROSIN_ADD")
				kerosin_add:Show()
				kerosin_add:PlayAnimation()
				room:AddControlForParticleSys("KEROSIN_ADD")
				return true
			end
		end	
	end
	
	if itemName == "BACKGROUND"  then								--воронка с колбы на стол
		if cursor:IsItemNameEqual("FUNNEL_BULB") then	
			cursor:RemoveItem()
			room:GetItem("FUNNEL"):Show()
			cstore:SetKey("funnel_bulb_on_table", true) 	--!!!
			room.sfx:Play ("glass_drop")	--звук "воронка кладется на стол"
			return true
		end
	end

	if cursor:IsItemNameEqual("CORK") then							--пробка
		if itemName == "NITROGLYCERINE"  then
			if not(room:GetItem("FUNNEL_BULB"):IsVisible()) then
				cursor:RemoveItem()
				room:GetItem("NITROGLYCERINE_CORK"):Show()
				room:GetItem("NITROGLYCERINE"):Disable()
				stars:Show("NITROGLYCERINE", "4")
				cstore:SetKey("cork_on_nitroglycerine", true) 	--!!!
				room.sfx:Play ("cort_insert")	--звук "пробка затыкает бутыль"
				return true
			else
				comment:Add("CMT_Funnel_Player")
				BackItem("CORK")
				room.sfx:Play ("cork_drop")	--звук "пробка кладется на стол"
				return true
			end
		else
			BackItem("CORK")
			room.sfx:Play ("cork_drop")	--звук "пробка кладется на стол"
		end
	end
end
--------------------------------------------------------------------------------
function OnUpdate (elapsedTime)
	
	if roast == 1 then
		flower_stand = room:GetItem("PETRI_DISH_FLOWER_STAND")
		if not(flower_stand.effects:IsPlayingColor()) then
			flower_stand:Hide()
			room:GetItem("PETRI_DISH_FLOWER_DONE"):Disable()
			roast = 2
		end
	end
	
	if roast == 3 then
		salt_stand = room:GetItem("PETRI_DISH_SALT_STAND")
		if not(salt_stand.effects:IsPlayingColor()) then
			salt_stand:Hide()
			room:GetItem("PETRI_DISH_SALT_DONE"):Disable()
			roast = 4
		end
	end
	
	local knife_flower = room:GetItem("KNIFE_FLOWER")
	local flower_cut = room:GetItem("FLOWER_CUT")
	
	if knife_flower:IsVisible() then
		if knife_flower:IsAnimPlaying () then
			if not(flower_cut:IsVisible()) then
				local frame = room:GetVideoFrame(knife_flower:GetName())
				if frame >= 35 then
					HideFade(room:GetItem("FLOWER"), time_flower)
					ShowFade(flower_cut, time_flower)
				end
			end
		end
	end

end
--------------------------------------------------------------------------------
-- Окончание анимации шинкования цветка
function OnAnimationComplete_KNIFE_FLOWER(forced)
	room:GetItem("KNIFE_FLOWER"):Hide()
	inventory:AddInventoryItem(inventory.alt:GetItem("KNIFE_Q"))
	inventory.alt:EraseOneItemFromCell("KNIFE_Q")
	
	room:GetItem("FLOWER"):Hide()
	room:GetItem("FLOWER_CUT"):Enable()
	
	if forced == true then
		local flower_cut = room:GetItem("FLOWER_CUT")
		flower_cut:Show()
		local color = flower_cut:GetColor()
		color:SetNormalAlpha(1)
		flower_cut:SetColor(color)
	end
end
--------------------------------------------------------------------------------
function OnNeedReturnItemToRoom()
	BackItem("BOX_OF_MATCHES")
	BackItem("FUNNEL")
	BackItem("FLOWER_CUT")
	BackItem("PETRI_DISH_FLOWER")
	Back_PETRI_DISH_FLOWER_DONE()
	BackItem("PETRI_DISH_SALT")
	Back_PETRI_DISH_SALT_DONE()
	BackItem("FUNNEL_BULB")
	BackItem("CORK")
	EndOffRoast()
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function FlowerAndSaltInBulb()
	comment:Add("CMT_Kerosene_Player")
	cstore:SetKey("Garbage_HO", 2)
end
--------------------------------------------------------------------------------
function BackItem(itemName)
	if cursor:IsItemNameEqual(itemName) then
		cursor:RemoveItem()
		room:GetItem(itemName):Show()
	end
end
--------------------------------------------------------------------------------
function Back_PETRI_DISH_FLOWER_DONE()
	if cursor:IsItemNameEqual("PETRI_DISH_FLOWER_DONE") then
		cursor:RemoveItem()
		room:GetItem("PETRI_DISH_FLOWER_DONE"):Show()
		room:GetItem("M_STAND"):Hide()
	end
end
--------------------------------------------------------------------------------
function Back_PETRI_DISH_SALT_DONE()
	if cursor:IsItemNameEqual("PETRI_DISH_SALT_DONE") then
		cursor:RemoveItem()
		room:GetItem("PETRI_DISH_SALT_DONE"):Show()
		room:GetItem("M_STAND"):Hide()
	end
end
--------------------------------------------------------------------------------
function Roast()
	
	if room:GetItem("SPIRIT_FIRE"):IsVisible() then
		local flower_stand = room:GetItem("PETRI_DISH_FLOWER_STAND")
		if flower_stand:IsVisible() then
			HideFade(flower_stand, time_roast)
			ShowFade(room:GetItem("PETRI_DISH_FLOWER_DONE"), time_roast)
			
			local flower_heating = room:GetItem("FLOWER_HEATING")
			flower_heating:Show()
			flower_heating:PlayAnimation()
			room:AddControlForParticleSys("FLOWER_HEATING")
			
			room.sfx:Play ("heat")	--звук "склянка накалилась"
			roast = 1
		end
		
		local salt_stand = room:GetItem("PETRI_DISH_SALT_STAND")
		if salt_stand:IsVisible() then
			HideFade(salt_stand, time_roast)
			ShowFade(room:GetItem("PETRI_DISH_SALT_DONE"), time_roast)
			
			local salt_heating = room:GetItem("SALT_HEATING")
			salt_heating:Show()
			salt_heating:PlayAnimation()
			room:AddControlForParticleSys("SALT_HEATING")
			
			room.sfx:Play ("heat")	--звук "склянка накалилась"
			roast = 3
		end
	end
end
--------------------------------------------------------------------------------
function EndOffRoast()
	if roast == 1 then
		room:GetItem("PETRI_DISH_FLOWER_STAND"):Hide()
		
		local flower_done = room:GetItem("PETRI_DISH_FLOWER_DONE")
		flower_done.effects:StopColor()
		local color = flower_done:GetColor()
		color:SetNormalAlpha(1)
		flower_done:SetColor(color)
		
		flower_done:Disable()
		roast = 2
	end
	
	if roast == 3 then
		room:GetItem("PETRI_DISH_SALT_STAND"):Hide()
		
		local salt_done = room:GetItem("PETRI_DISH_SALT_DONE")
		salt_done.effects:StopColor()
		local color = salt_done:GetColor()
		color:SetNormalAlpha(1)
		salt_done:SetColor(color)
		
		salt_done:Disable()
		roast = 4
	end
end
-----------------Проявление по фэйду--------------------------------------------
function ShowFade(item, time_effect)
	item:Show()
	local color = item:GetColor()
	local color1 = item:GetColor()
	color:SetNormalAlpha(1)
	color1:SetNormalAlpha(0)
	item:SetColor(color1)
	item.effects:PlayColor(color, time_effect)
end
-----------------Исчезновение по фэйду--------------------------------------------
function HideFade(item, time_effect)
	local color = item:GetColor()
	color:SetNormalAlpha(0)
	item.effects:PlayColor(color, time_effect)
end
