--------------------------------------------------------------------------------
--  File     : Smithy_Workplace
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
barrel_steam_time = 1.0
--------------------------------------------------------------------------------
function OnSerialize(system)
	barrel_steam = system:Serialize(barrel_steam)
	barrel_steam_time_dt = system:Serialize(barrel_steam_time_dt)
	bellows_count = system:Serialize(bellows_count)
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	barrel_steam = 0
	barrel_steam_time_dt = 0
	bellows_count = 0
	
	room:GetItem("CLAY_FORM"):Disable()
	room:GetItem("M_BELLOWS"):Disable()
	cstore:SetKey("Close_up_Smithy_Workplace", 0)
	
	local bellows = room:GetItem("BELLOWS_VIDEO")
	bellows:PlayAnimation()
	bellows:SetAnimPause(true)
	
	stars:Show("M_BELLOWS", "4")
	stars:Show("M_BARREL", "4")
	stars:Show("M_COAL", "4")
	stars:Show("M_FORGE", "4")
	stars:Show("METAL_BAR_FORM", "4")
	stars:Show("CLAY_FORM", "4")
	stars:Show("PLIERS_METAL", "4")
	
	stars:Show("CLAY_FORM_FORGE", "4")
	stars:Show("METAL_FORM_FORGE", "4")
end
--------------------------------------------------------------------------------
function OnMouseClickOnBack()
	OnNeedReturnItemToRoom()
	chapter:ExitCloseup()
	return true
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if itemName == "M_BELLOWS" then 							--меха	
		if item:IsEnabled() then
			item:Hide()
			
			local bellows = room:GetItem("BELLOWS_VIDEO")
			bellows:SetAnimPause(false)
			room:AddCtrlAnimation("BELLOWS_VIDEO", -1)
			
			local forge_light = room:GetItem("COAL_FORGE_ADD_LIGHT")
			forge_light:Show()
			local color = forge_light:GetColor()
			local color1 = forge_light:GetColor()
			color1:SetNormalAlpha(0)
			forge_light:SetColor(color1)
			forge_light.effects:PlayColor(color, 1.0)
			
			room:GetItem("FORGE"):Show()
			room.sfx:Play ("bellows")	--звук "раздуваем мехами огонь"
			room.sfx:PlayEx ("fireplace", true, 1, 0) --звук "огонь в кузнице (цикличный)"
			cstore:SetKey("Close_up_Smithy_Workplace", 1)
		else
			comment:Add("CMT_Bellows_Player")
		end
		return true	
	
	elseif itemName == "M_FORGE"  then							--горн
		if room:GetItem("COAL_FORGE_ADD_LIGHT"):IsVisible() then
			comment:Add("CMT_Forge3_Player")
		else
			if room:GetItem("COAL_FORGE_ADD"):IsVisible() then
				comment:Add("CMT_Forge2_Player")
			else
				comment:Add("CMT_Forge1_Player")
			end
		end
		return true
		
	elseif itemName == "M_BARREL"  then							--бочка с водой	
		comment:Add("CMT_Barrel_Player")
		return true		
		
	elseif itemName == "M_COAL"  then							--уголь
		comment:Add("CMT_Coal_Player")
		return true	
		
	elseif itemName == "METAL_BAR_FORM"  then					--ступка с металлическим бруском		
		item:Hide()
		room:SetItemOnCursor(room:GetItem("METAL_BAR_FORM_CUR"))
		room.sfx:Play ("take_form")	--звук "берем горшок"
		return true	
		
	elseif itemName == "CLAY_FORM"  then						--форма для глины
		if room:GetItem("CLAY_FORM"):GetCursorForDisabled() ~= "arrow" then
			if not(item:IsEnabled()) then
				if room:GetItem("KEY_HOT"):IsVisible() then
					comment:Add("CMT_ClayForm4_Player")
				elseif room:GetItem("CLAY_BURN"):IsVisible() then
					comment:Add("CMT_ClayForm3_Player")
				else
					comment:Add("CMT_ClayForm1_Player")
				end
			else
				room:SetItemOnCursor(room:GetItem("CLAY_FORM_CUR"))
				room:GetItem("CLAY_FORM"):Hide()
				room:GetItem("CLAY"):Hide()
				room:GetItem("CLAY_PRINT"):Hide()
			end
			return true
		end
	
	
	elseif (itemName == "PLIERS_METAL")  and						--щипцы								
		    (item:IsEnabled()) then
		room:SetItemOnCursor(item)
		room.sfx:Play ("take_holder")	--звук "берем щипцы"
		return true
	
	elseif itemName == "CLAY_FORM_FORGE"  then					--разогретая форма с глиной	
		comment:Add("CMT_ClayForm2_Player")
		return true	
		
	elseif itemName == "METAL_FORM_FORGE"  then					--ступка с расплавленным металлом
		comment:Add("CMT_MetalForm_Player")
		return true	
	
	elseif itemName == "RAVEN_KEY"  then						--ключ ворона									
		pick_panel:AddItems("RAVEN_KEY", true)
		cstore:SetKey("Close_up_Smithy_Workplace", 2)
		room:MarkCompleted()
		return true	
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)					
	local itemName = item:GetName()
	
	if itemName == "M_COAL" then								--лопата на уголь
		if cursor:IsItemNameEqual("SPADE_Q") then	
			item:Hide()		
			inventory.alt:AddItemFromCursor()
			local shovel_coal = room:GetItem("SHOVEL_COAL")
			shovel_coal:Show()
			shovel_coal:PlayAnimation()
			room:AddCtrlAnimation("SHOVEL_COAL", -1)
			room.sfx:Play ("use_spade")	--звук "берем на лопату уголь"
			return true
		end
	end
	
	if itemName == "CLAY_FORM"  then							--слепок на форму с глиной 
		if cursor:IsItemNameEqual("WAX_MOLD") then	
			cursor:RemoveItem()
			room:GetItem("CLAY_PRINT"):Show()
			room:GetItem("CLAY_FORM"):Enable()
			room.sfx:Play ("make_the_print")	--звук "делаем отпечаток из воска в глину"
			return true
		end
	end
		
	if cursor:IsItemNameEqual("SHOVEL_COAL_CUR") then			--лопата c углем на горн	
		if itemName == "M_FORGE" then													
			cursor:RemoveItem()
			room:GetItem("COAL_FORGE_ADD"):Show()
			room:GetItem("M_BELLOWS"):Enable()
			if inventory.alt:IsItemExisting ("SPADE_Q") then
				inventory.alt:EraseOneItemFromCell("SPADE_Q")
			end
			room.sfx:Play ("drop_coal")	--звук "бросаем уголь на топку"
			cstore:SetKey("Coal_Forge", true) --!!!
			return true
			
		else 	
			Back_SHOVEL_COAL_CUR()
			return false
		end
	end	
		
	if cursor:IsItemNameEqual("METAL_BAR_FORM_CUR") then	--ступка с металлическим бруском на горн
		if (itemName == "M_FORGE") or (itemName == "CLAY_FORM_FORGE") then	
			if room:GetItem("COAL_FORGE_ADD_LIGHT"):IsVisible() then
				cursor:RemoveItem()
				
				local metal_melt = room:GetItem("METAL_MELT")
				metal_melt:Show()
				metal_melt:PlayAnimation()
				room:AddCtrlAnimation("METAL_MELT", -1)
			
				if cstore:GetKey("Clay_Form_Forge") then
					room:GetItem("M_FORGE"):Hide()
				end
				room.sfx:Play ("put_form")	--звук "ставим горшок"
				cstore:SetKey("Metal_Form_Forge", true) --!!!
			else
				comment:Add("CMT_Applying1_Player")
				Back_METAL_BAR_FORM_CUR()
			end
			return true
			
		elseif itemName == "M_TABLE" then
			Back_METAL_BAR_FORM_CUR()
			return true
			
		else
			Back_METAL_BAR_FORM_CUR()
			return false
		end
	end
	
	if cursor:IsItemNameEqual("CLAY_FORM_CUR") then			--форма с глиной на горн
		if (itemName == "M_FORGE") or (itemName == "METAL_FORM_FORGE") then
			if room:GetItem("COAL_FORGE_ADD_LIGHT"):IsVisible() then
				cursor:RemoveItem()
				room:GetItem("CLAY_FORM_FORGE"):Show()
				room:GetItem("CLAY_FORM"):Disable ()
				if cstore:GetKey("Metal_Form_Forge") then
					room:GetItem("M_FORGE"):Hide()
				end
				room.sfx:Play ("put_print")	--звук "ставим глиняную форму"
				cstore:SetKey("Clay_Form_Forge", true) 	--!!!
			else
				comment:Add("CMT_Applying1_Player")
				Back_CLAY_FORM_CUR()
			end
			return true
			
		elseif itemName == "M_TABLE" then
			Back_CLAY_FORM_CUR()
			return true
			
		else
			Back_CLAY_FORM_CUR()
			return false
		end
	end
	
	if itemName == "CLAY_FORM_FORGE"  then						--щипцы на форму с обожженной глиной
		if cursor:IsItemNameEqual("PLIERS_METAL") then	
			cursor:RemoveItem()
			item:Hide()
			room:SetItemOnCursor(room:GetItem("CLAY_HOT_CUR"))
			room.sfx:Play ("take_print")	--звук "берем форму держателем"
			return true
		end
	end
	
	
	if cursor:IsItemNameEqual("CLAY_HOT_CUR") then				--форма с обожженной глиной на стол
		if itemName == "M_TABLE"  then								
			cursor:RemoveItem()
			room:SetItemOnCursor(room:GetItem("PLIERS_METAL"))
			room:GetItem("CLAY_FORM"):Show()
			room:GetItem("CLAY_BURN"):Show()
			room:GetItem("CLAY_PRINT"):Show()
			cstore:SetKey("Clay_Form_Table", true) 	--!!!
			room.sfx:Play ("put_print")	--звук "ставим глиняную форму"
			return true
			
		else
			Back_CLAY_HOT_CUR()
			room:SetItemOnCursor(room:GetItem("PLIERS_METAL"))
			return false
		end
	end
	
	if itemName == "METAL_FORM_FORGE"  then						--щипцы на ступку
		if cursor:IsItemNameEqual("PLIERS_METAL") then	
			cursor:RemoveItem()
			item:Hide()
			room:SetItemOnCursor(room:GetItem("METAL_HOT_CUR"))
			room.sfx:Play ("take_form_holder")	--звук "берем горшок держателем"
			return true
		end
	end
	
	if cursor:IsItemNameEqual("METAL_HOT_CUR") then				--ступку с металлом на форму с обожженной глиной
		if itemName == "CLAY_FORM"  then
			if room:GetItem("CLAY_PRINT"):IsVisible() then
				if room:GetItem("CLAY_BURN"):IsVisible() then
					cursor:RemoveItem()
					room:SetItemOnCursor(room:GetItem("PLIERS_METAL"))
					
					local metal_pour = room:GetItem("METAL_POUR")
					metal_pour:Show()
					metal_pour:PlayAnimation()
					room:AddCtrlAnimation("METAL_POUR", -1)
				
					cstore:SetKey("Metal_Form_Clay_Form", true) --!!!
					room.sfx:Play ("pour_metal")	--звук "заливаем метал в форму"
				else
					comment:Add("CMT_Applying2_Player")
					Back_METAL_HOT_CUR()
					
					room:SetItemOnCursor(room:GetItem("PLIERS_METAL"))
				end
				return true
			else
				comment:Add("CMT_Molt_Player")
				Back_METAL_HOT_CUR()
				room:SetItemOnCursor(room:GetItem("PLIERS_METAL"))
				return true
			end
		
		else
			Back_METAL_HOT_CUR()
			room:SetItemOnCursor(room:GetItem("PLIERS_METAL"))
			return false
		end
	end
	
	if itemName == "CLAY_FORM"  then							--щипцы на форму с обожженной глиной и ключом
		if cursor:IsItemNameEqual("PLIERS_METAL") then			
			if room:GetItem("KEY_HOT"):IsVisible() then
				cursor:RemoveItem()
				room:GetItem("CLAY_FORM"):Hide()
				room:GetItem("CLAY_BURN"):Hide()
				room:GetItem("CLAY_PRINT"):Hide()
				room:GetItem("KEY_HOT"):Hide()
				room:SetItemOnCursor(room:GetItem("CLAY_METAL_HOT_CUR"))
				room.sfx:Play ("take_print")	--звук "берем форму держателем"
				return true
			end
		end
	end
	
	if cursor:IsItemNameEqual("CLAY_METAL_HOT_CUR") then		--форму с ключом на бочку с водой
		if itemName == "M_BARREL"  then								
			cursor:RemoveItem()
			item:Hide()
			local barrel_steam_part = room:GetItem("BARREL_STEAM")
			barrel_steam_part:Show()
			barrel_steam_part:PlayAnimation()
			room:AddControlForParticleSys("BARREL_STEAM")
			barrel_steam = 1
			cstore:SetKey("Metal_Form_Barrel", true) --!!!
			room.sfx:Play ("put_into_water")	--звук "опускаем раскаленные предметы в воду (пшшшшшш)"
			return true
			
		elseif itemName == "M_TABLE"  then	
			Back_CLAY_METAL_HOT_CUR()
			room:SetItemOnCursor(room:GetItem("PLIERS_METAL"))
			return true
			
		else
			Back_CLAY_METAL_HOT_CUR()
			room:SetItemOnCursor(room:GetItem("PLIERS_METAL"))
			return false
		end
	end
	
	if itemName == "M_TABLE"  then								--форму с ключом на стол
		if cursor:IsItemNameEqual("CLAY_METAL_COLD_CUR") then	
			Table_CLAY_METAL_COLD_CUR()
			local pliers = room:GetItem("PLIERS_METAL")
			pliers:Show()
			pliers:Disable()
			stars:Hide("PLIERS_METAL")
			room.sfx:Play ("put_print")	--звук "ставим глиняную форму"
			return true
		end
	end
	
	-------
	if (itemName ~= "CLAY_FORM_FORGE") and					--щипцы не на ступку или форму
	   (itemName ~= "METAL_FORM_FORGE") and
	   (itemName ~= "CLAY_FORM") or
	   ((itemName == "CLAY_FORM") and not(room:GetItem("KEY_HOT"):IsVisible())) then
		if cursor:IsItemNameEqual("PLIERS_METAL") then	
			room.sfx:Play ("drop_holder")	--звук "кладем щипцы"
			BackItem("PLIERS_METAL")	
			return true	
		end
	end
end
--------------------------------------------------------------------------------
function OnUpdate (elapsedTime)

	local shovel_coal = room:GetItem("SHOVEL_COAL")
	
	if shovel_coal:IsVisible() then
		if shovel_coal:IsAnimPlaying () then

			local coal_pile = room:GetItem("COAL_PILE")
			if not(coal_pile:IsVisible()) then
				
				local frame = room:GetVideoFrame(shovel_coal:GetName())
				if frame >= 14 then
					coal_pile:Show()
					local color = coal_pile:GetColor()
					local color1 = coal_pile:GetColor()
					color1:SetNormalAlpha(0)
					coal_pile:SetColor(color1)
					coal_pile.effects:PlayColor(color, 0.5)
				end
			end
		end
	end
	
	if barrel_steam == 1 then
		barrel_steam_time_dt = barrel_steam_time_dt + elapsedTime
		if barrel_steam_time_dt >= barrel_steam_time then
			barrel_steam = 2
			room:SetItemOnCursor(room:GetItem("CLAY_METAL_COLD_CUR"))
		end
	end
	
	if bellows_count == 1 then
		local bellows = room:GetItem("BELLOWS_VIDEO")
		bellows:PlayAnimation()
		room:AddCtrlAnimation("BELLOWS_VIDEO", -1)
		bellows_count = 2
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации зачерпывания угля лопатой
function OnAnimationComplete_SHOVEL_COAL(forced)
	room:GetItem("SHOVEL_COAL"):Hide()
	
	if forced == true then
		room:GetItem("M_COAL"):Show()
		
		local coal_pile = room:GetItem("COAL_PILE")
		local color = coal_pile:GetColor()
		color:SetNormalAlpha(1)
		coal_pile:SetColor(color)
		coal_pile:Hide()
		
		if inventory.alt:IsItemExisting ("SPADE_Q") then
			inventory:AddInventoryItem(inventory.alt:GetItem ("SPADE_Q"))
			inventory.alt:EraseOneItemFromCell("SPADE_Q")
		end
	else
		room:SetItemOnCursor(room:GetItem("SHOVEL_COAL_CUR"))
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации раздувания мехами огня
function OnAnimationComplete_BELLOWS_VIDEO(forced)
	
	if forced == true then
		local forge_light = room:GetItem("COAL_FORGE_ADD_LIGHT")
		forge_light.effects:StopColor()
		local color = forge_light:GetColor()
		color:SetNormalAlpha(1)
		forge_light:SetColor(color)
		
		room:VideoSeekToEnd(room:GetItem("BELLOWS_VIDEO"))
		
		bellows_count = 2
		
	else
		bellows_count = bellows_count + 1
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации плавления метала
function OnAnimationComplete_METAL_MELT(forced)
	room:GetItem("METAL_MELT"):Hide()
	room:GetItem("METAL_FORM_FORGE"):Show()
end
--------------------------------------------------------------------------------
-- Окончание анимации выливания расплавленного метала в форму
function OnAnimationComplete_METAL_POUR(forced)
	room:GetItem("METAL_POUR"):Hide()
	room:GetItem("KEY_HOT"):Show()
end
--------------------------------------------------------------------------------
function OnNeedReturnItemToRoom()
	BackItem("PLIERS_METAL")
	Back_SHOVEL_COAL_CUR()
	Back_METAL_BAR_FORM_CUR()
	Back_CLAY_FORM_CUR()
	Back_CLAY_HOT_CUR()
	Back_METAL_HOT_CUR()
	Back_CLAY_METAL_HOT_CUR()
	if cursor:IsItemNameEqual("CLAY_METAL_COLD_CUR") then	
		Table_CLAY_METAL_COLD_CUR()
	end
	if barrel_steam == 1 then
		barrel_steam = 2
		Table_CLAY_METAL_COLD_CUR()
	end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function BackItem(itemName)
	if cursor:IsItemNameEqual(itemName) then
		cursor:RemoveItem()
		room:GetItem(itemName):Show()
	end
end
--------------------------------------------------------------------------------
function Back_SHOVEL_COAL_CUR()
	if cursor:IsItemNameEqual("SHOVEL_COAL_CUR") then
		cursor:RemoveItem()
		room:GetItem("M_COAL"):Show()
		room:GetItem("COAL_PILE"):Hide()
		if inventory.alt:IsItemExisting ("SPADE_Q") then
			inventory:AddInventoryItem(inventory.alt:GetItem ("SPADE_Q"))
			inventory.alt:EraseOneItemFromCell("SPADE_Q")
		end
	end
end
--------------------------------------------------------------------------------
function Back_METAL_BAR_FORM_CUR()
	if cursor:IsItemNameEqual("METAL_BAR_FORM_CUR") then
		cursor:RemoveItem()
		room:GetItem("METAL_BAR_FORM"):Show()
		room.sfx:Play ("put_form")	--звук "ставим горшок"
	end
end
--------------------------------------------------------------------------------
function Back_CLAY_FORM_CUR()
	if cursor:IsItemNameEqual("CLAY_FORM_CUR") then
		cursor:RemoveItem()
		room:GetItem("CLAY"):Show()
		room:GetItem("CLAY_FORM"):Show()
		room:GetItem("CLAY_PRINT"):Show()
		room.sfx:Play ("put_print")	--звук "ставим глиняную форму"
	end
end
--------------------------------------------------------------------------------
function Back_CLAY_HOT_CUR()
	if cursor:IsItemNameEqual("CLAY_HOT_CUR") then
		cursor:RemoveItem()
		room:GetItem("CLAY_FORM_FORGE"):Show()
		room:GetItem("PLIERS_METAL"):Show()
	end
end
--------------------------------------------------------------------------------
function Back_METAL_HOT_CUR()
	if cursor:IsItemNameEqual("METAL_HOT_CUR") then
		cursor:RemoveItem()
		room:GetItem("METAL_FORM_FORGE"):Show()
		room:GetItem("PLIERS_METAL"):Show()
	end
end
--------------------------------------------------------------------------------
function Back_CLAY_METAL_HOT_CUR()
	if cursor:IsItemNameEqual("CLAY_METAL_HOT_CUR") then
		cursor:RemoveItem()
		room:GetItem("CLAY_FORM"):Show()
		room:GetItem("CLAY_BURN"):Show()
		room:GetItem("CLAY_PRINT"):Show()
		room:GetItem("KEY_HOT"):Show()
		room:GetItem("PLIERS_METAL"):Show()
	end
end
--------------------------------------------------------------------------------
function Table_CLAY_METAL_COLD_CUR()
	cursor:RemoveItem()
	room:GetItem("M_TABLE"):Hide()
	room:GetItem("CLAY_FORM"):Show()
	room:GetItem("CLAY_FORM"):SetCursorForDisabled("arrow")
	room:GetItem("CLAY_BURN"):Show()
	room:GetItem("CLAY_PRINT"):Show()
	room:GetItem("RAVEN_KEY"):Show()
end
