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

M_PART1					= "M_PART1"		--маска для деревяшки 1
M_PART2					= "M_PART2"		--маска для деревяшки 2
M_PART3					= "M_PART3"		--маска для деревяшки 3
M_NAIL1					= "M_NAIL1"		--маска для гвоздя 1
M_NAIL2					= "M_NAIL2"		--маска для гвоздя 2
M_NAIL3					= "M_NAIL3"		--маска для гвоздя 3
M_NAIL4					= "M_NAIL4"		--маска для гвоздя 4

I_STOOL_BASE			= "STOOL_BASE"		--основа стула
I_STOOL					= "STOOL"			--стул
I_PART1					= "PART1"			--деревяшка1
I_PART1_FIXED			= "PART1_FIXED"
I_PART2					= "PART2"			--деревяшка2
I_PART2_FIXED			= "PART2_FIXED"
I_PART3					= "PART3"			--деревяшка3
I_PART3_FIXED			= "PART3_FIXED"
I_HAMMER				= "HAMMER"			--молоток
I_HAMMER1_D				= "HAMMER1_D"		--анимация 1 молотка
I_HAMMER2_D				= "HAMMER2_D"		--анимация 2 молотка
I_NAILS					= "NAILS"			--гвозди
I_NAIL1_1				= "NAIL1_1"			--гвоздь 1
I_NAIL1_2				= "NAIL1_2"
I_NAIL1_3				= "NAIL1_3"
I_NAIL1_4				= "NAIL1_4"
I_NAIL2_1				= "NAIL2_1"			--гвоздь 2
I_NAIL2_2				= "NAIL2_2"
I_NAIL2_3				= "NAIL2_3"
I_NAIL2_4				= "NAIL2_4"
I_NAIL3_1				= "NAIL3_1"			--гвоздь 3
I_NAIL3_2				= "NAIL3_2"
I_NAIL3_3				= "NAIL3_3"
I_NAIL3_4				= "NAIL3_4"
I_NAIL4_1				= "NAIL4_1"			--гвоздь 4
I_NAIL4_2				= "NAIL4_2"
I_NAIL4_3				= "NAIL4_3"
I_NAIL4_4				= "NAIL4_4"
--------------------------------------------------------------------------------
function OnSerialize(system)
	stool_ready = system:Serialize(stool_ready)
	hammerPos = system:Serialize(hammerPos)
	hammer = system:Serialize(hammer)
	hammerProc = system:Serialize(hammerProc)
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	cstore:SetKey("Stool_Ready", 0)

	DisableParts()

	stool_ready = 0
	hammerPos = ctypes:Vector2(0, 0)
	hammer = 0
	hammerProc = 0
end
--------------------------------------------------------------------------------
function OnEntry ()
	if (cursor:IsItemNameEqual(I_HAMMER)) or
	   (inventory:IsItemExisting(I_HAMMER)) then
		local hammer_1 = room:GetItem(I_HAMMER1_D)
		hammer_1:PlayAnimation()
		hammer_1:SetAnimPause(true)
		inventory.alt:CreateAndAddItem(hammer_1)
	  
		local hammer_2 = room:GetItem(I_HAMMER2_D)
		hammer_2:PlayAnimation()
		hammer_2:SetAnimPause(true)
		inventory.alt:CreateAndAddItem(hammer_2)
	end
	
	ChangeHammerOnCursor ()
end
--------------------------------------------------------------------------------
function OnMouseClickOnBack()
	OnNeedReturnItemToRoom()
	chapter:ExitCloseup()
	return true
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if  ((itemName == I_STOOL_BASE)  or							--сломанный стул
			(itemName == I_PART1_FIXED) or 
			(itemName == I_PART2_FIXED) or
			(itemName == I_PART3_FIXED) or	
			(itemName == M_PART1) or
			(itemName == M_PART2) or
			(itemName == M_PART3)) and (stool_ready < 4) then	
		comment:Add("CMT_Stool_Player")
		return true
	
	elseif  ((itemName == I_STOOL_BASE) or						--целый стул
			(itemName == I_PART1_FIXED) or 
			(itemName == I_PART2_FIXED) or
			(itemName == I_PART3_FIXED)) 
			and (stool_ready == 8) 
			and not(room:GetItem(I_HAMMER1_D):IsVisible()) 
			and not(room:GetItem(I_HAMMER2_D):IsVisible()) then
		room:GetItem(I_STOOL_BASE):Hide()
		room:GetItem(I_PART1_FIXED):Hide()
		room:GetItem(I_PART2_FIXED):Hide()
		room:GetItem(I_PART3_FIXED):Hide()
		room:GetItem(I_NAIL1_4):Hide()
		room:GetItem(I_NAIL2_4):Hide()
		room:GetItem(I_NAIL3_4):Hide()
		room:GetItem(I_NAIL4_4):Hide()
		room:GetItem(I_STOOL):Show()
		pick_panel:AddItems(I_STOOL, true)
		cstore:SetKey("Stool_Ready", 3)
		cstore:SetKey("Mask_Stool",1)
		return true
	
	elseif  ((itemName == M_NAIL1) or							--гвозди
			(itemName == M_NAIL2) or 
			(itemName == M_NAIL3) or
			(itemName == M_NAIL4)) and
			not(room:GetItem(I_HAMMER1_D):IsVisible()) and 
			not(room:GetItem(I_HAMMER2_D):IsVisible()) then
		comment:Add("CMT_Nails_Player")
		return true
	
	elseif itemName == I_PART1 then								--деревяшка1
		SetPartOnCursor(item, room:GetItem(I_PART1_FIXED))
		return true
	elseif itemName == I_PART2 then								--деревяшка2
		SetPartOnCursor(item, room:GetItem(I_PART2_FIXED))
		return true
	elseif itemName == I_PART3 then								--деревяшка3
		SetPartOnCursor(item, room:GetItem(I_PART3_FIXED))
		return true
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)
	local itemName = item:GetName()

	if cursor:IsItemNameEqual(I_PART1_FIXED) then				
		SetPartOnPlace(itemName, M_PART1, room:GetItem(I_PART1), room:GetItem(I_PART1_FIXED))
		return true
	end
	if cursor:IsItemNameEqual(I_PART2_FIXED) then	
		SetPartOnPlace(itemName, M_PART2, room:GetItem(I_PART2), room:GetItem(I_PART2_FIXED))
		return true
	end
	if cursor:IsItemNameEqual(I_PART3_FIXED) then		
		SetPartOnPlace(itemName, M_PART3, room:GetItem(I_PART3), room:GetItem(I_PART3_FIXED))
		return true
	end
	
	if (cursor:IsItemNameEqual(I_NAILS)) 						--гвозди на сломанный стул
		and (stool_ready == 3) then						
		if 	(itemName == I_STOOL_BASE) or						
			(itemName == I_PART1_FIXED) or 
			(itemName == I_PART2_FIXED) or
			(itemName == I_PART3_FIXED) then
			cursor:RemoveItem()
			
			EnableParts()
			
			room:GetItem(I_NAIL1_1):Show()
			room:GetItem(I_NAIL2_1):Show()
			room:GetItem(I_NAIL3_1):Show()
			room:GetItem(I_NAIL4_1):Show()
			room:GetItem(M_NAIL1):Show()
			room:GetItem(M_NAIL2):Show()
			room:GetItem(M_NAIL3):Show()
			room:GetItem(M_NAIL4):Show()
			stool_ready = 4
			room.sfx:Play ("put_nails")					--звук "ставим гвозди"
			return true
		end
	end
	
	if cursor:IsItemNameEqual(I_HAMMER1_D) or 			--молоток на гвозди
	   cursor:IsItemNameEqual(I_HAMMER2_D) then		

		if itemName == M_NAIL1 then	
			BumpHammerOnNails(room:GetItem(I_HAMMER1_D), 1, 493/1440, -411/1080)
			return true
		end
		
		if itemName == M_NAIL2 then
			BumpHammerOnNails(room:GetItem(I_HAMMER2_D), 2, -213/1440, 286/1080)
			return true
		end
		
		if itemName == M_NAIL3 then	
			BumpHammerOnNails(room:GetItem(I_HAMMER2_D), 3, 15/1440, 448/1080)
			return true
		end
		
		if itemName == M_NAIL4 then	
			BumpHammerOnNails(room:GetItem(I_HAMMER1_D), 4, 726/1440, -246/1080)
			return true
		end
	end
end
--------------------------------------------------------------------------------
function OnUpdate (elapsedTime)

	if (hammer > 0) and (hammer < 5) then
		cursor:RemoveItem()
		
		local itemHammer = room:GetItem(I_HAMMER2_D)
		if (hammer == 1) or (hammer == 4) then
			itemHammer = room:GetItem(I_HAMMER1_D)
		end
		
		if (hammerProc == 0) or (hammerProc == 3) or (hammerProc == 6) then
			itemHammer:PlayAnimation()
			hammerProc = hammerProc + 1
			room:AddCtrlAnimation(itemHammer:GetName(), -1)
		end
		
		if (hammerProc == 1) or (hammerProc == 4) or (hammerProc == 7) then
			local frame = room:GetVideoFrame(itemHammer:GetName())
			if frame >= 3 then
				hammerProc = hammerProc + 1
				room.sfx:Play("beat_nail")				--звук "забиваем гвоздь"
				if hammer == 1 then
					HammerOnNails(I_NAIL1_1, I_NAIL1_2, I_NAIL1_3, I_NAIL1_4, M_NAIL1)
				elseif hammer == 2 then
					HammerOnNails(I_NAIL2_1, I_NAIL2_2, I_NAIL2_3, I_NAIL2_4, M_NAIL2)
				elseif hammer == 3 then
					HammerOnNails(I_NAIL3_1, I_NAIL3_2, I_NAIL3_3, I_NAIL3_4, M_NAIL3)
				elseif hammer == 4 then
					HammerOnNails(I_NAIL4_1, I_NAIL4_2, I_NAIL4_3, I_NAIL4_4, M_NAIL4)
				end
			end
		end
		
		if hammerProc == 9 then
			if stool_ready < 8 then
							
				local itemHammer1 = inventory.alt:GetItem(I_HAMMER2_D)
				if (hammer == 1) or (hammer == 4) then
					itemHammer1 = inventory.alt:GetItem(I_HAMMER1_D)
				end
				cursor:SetIventoryItem(itemHammer1)
			
			else
				Win()
			end
			itemHammer:SetVisible(false)
			hammer = 0
			hammerProc = 0
		end
	end
	
end
--------------------------------------------------------------------------------
function OnNeedReturnItemToRoom()
	SaveParts(I_PART1_FIXED, I_PART1)
	SaveParts(I_PART2_FIXED, I_PART2)
	SaveParts(I_PART3_FIXED, I_PART3)
	
	if (cursor:IsItemNameEqual(I_HAMMER1_D)) or
	   (cursor:IsItemNameEqual(I_HAMMER2_D)) then
	   cursor:RemoveItem()
	end
	
	if inventory.alt:IsItemExisting(I_HAMMER1_D) then
		inventory.alt:EraseOneItemFromCell(I_HAMMER1_D)
	end
	if inventory.alt:IsItemExisting(I_HAMMER2_D) then
		inventory.alt:EraseOneItemFromCell(I_HAMMER2_D)
	end
	
	if hammerProc ~= 0 then
		ExtrEndAnimHammer()
	end
end
--------------------------------------------------------------------------------
function OnSetItemOnCursor (InventoryItem)
	ChangeHammerOnCursor ()
end
--------------------------------------------------------------------------------
function OnRemoveItemOnCursor  (InventoryItem)
	local itemName = InventoryItem:GetName()
	
	if ((itemName == I_HAMMER1_D) or 
		(itemName == I_HAMMER2_D)) and 
		(hammer == 0) then
		
		if inventory:IsItemExisting(itemName) then
			inventory:EraseOneItemFromCell(itemName)
		end
		
		local itemHammer = inventory.alt:GetItem (I_HAMMER)
		inventory:AddInventoryItem(itemHammer)
		inventory.alt:EraseOneItemFromCell(I_HAMMER)
	end
	
	if hammer == 5 then
		hammer = 0
	end
end
--------------------------------------------------------------------------------
function OnMouseEnterOnItem (item)
	local itemName = item:GetName()
	
	if (itemName == M_NAIL1) or (itemName == M_NAIL4) then
		if cursor:IsItemNameEqual(I_HAMMER2_D) then
			SetHammerOnCursor (I_HAMMER1_D)
		end
	end
	
	if (itemName == M_NAIL2) or (itemName == M_NAIL3) then
		if cursor:IsItemNameEqual(I_HAMMER1_D) then
			SetHammerOnCursor (I_HAMMER2_D)
		end
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации удара молотком
function OnAnimationComplete_HAMMER1_D(forced)
	EndAnimHammer(forced)
end
function OnAnimationComplete_HAMMER2_D(forced)
	EndAnimHammer(forced)
end
function EndAnimHammer(forced)
	if forced == true then
		ExtrEndAnimHammer()
		
	else
		hammerProc = hammerProc + 1
	end
end
--------------------------------------------------------------------------------
function ExtrEndAnimHammer()
	if hammer == 1 then
		ExitHammerOnNails(I_NAIL1_1, I_NAIL1_2, I_NAIL1_3, I_NAIL1_4, M_NAIL1)
	elseif hammer == 2 then
		ExitHammerOnNails(I_NAIL2_1, I_NAIL2_2, I_NAIL2_3, I_NAIL2_4, M_NAIL2)
	elseif hammer == 3 then
		ExitHammerOnNails(I_NAIL3_1, I_NAIL3_2, I_NAIL3_3, I_NAIL3_4, M_NAIL3)
	elseif hammer == 4 then
		ExitHammerOnNails(I_NAIL4_1, I_NAIL4_2, I_NAIL4_3, I_NAIL4_4, M_NAIL4)
	end
		
	if stool_ready < 8 then
		if inventory.alt:IsItemExisting(I_HAMMER) then	
			local itemHammer = inventory.alt:GetItem (I_HAMMER)
			inventory:AddInventoryItem(itemHammer)
			inventory.alt:EraseOneItemFromCell(I_HAMMER)
		end
	else
		Win()
	end
		
	room:GetItem(I_HAMMER1_D):Hide()
	room:GetItem(I_HAMMER2_D):Hide()
	hammer = 0
	hammerProc = 0
end
--------------------------------------------------------------------------------
----------------стул готов------------------------------------------------------
function Win()
	if stool_ready == 8 then
		if inventory.alt:IsItemExisting(I_HAMMER) then
			inventory.alt:EraseOneItemFromCell(I_HAMMER)
		end
		room:GetItem(I_STOOL_BASE):SetCursorForNormal("hand")
		room:GetItem(I_PART1_FIXED):SetCursorForNormal("hand")
		room:GetItem(I_PART2_FIXED):SetCursorForNormal("hand")
		room:GetItem(I_PART3_FIXED):SetCursorForNormal("hand")
		room:GetItem(I_NAIL1_4):SetClickable(false)
		room:GetItem(I_NAIL2_4):SetClickable(false)
		room:GetItem(I_NAIL3_4):SetClickable(false)
		room:GetItem(I_NAIL4_4):SetClickable(false)
		cstore:SetKey("Stool_Ready", 2)
		room:MarkCompleted()
	end
end
--------------молоток по гвоздю (применение)--------------------
function BumpHammerOnNails(itemHammer, ham, x, y)
	hammer = ham
	itemHammer:SetVisible(true)
	
	local SceneSize = room:GetNoZoomSize()
	hammerPos = ctypes:Vector2(math.floor(SceneSize.x * x), math.floor(SceneSize.y * y))
	itemHammer:SetPosition(hammerPos)
end
--------------молоток по гвоздю (анимация удара)--------------------
function HammerOnNails(nail1, nail2, nail3, nail4, m_nail)
	if 	room:GetItem(nail1):IsVisible() then
		room:GetItem(nail1):Hide()
		room:GetItem(nail2):Show()
	elseif room:GetItem(nail2):IsVisible() then
		room:GetItem(nail2):Hide()
		room:GetItem(nail3):Show()
	elseif room:GetItem(nail3):IsVisible() then
		room:GetItem(nail3):Hide()
		room:GetItem(nail4):Show()
		room:GetItem(m_nail):Hide()
		stool_ready = stool_ready + 1
	end
end
----------------молоток по гвоздю (экстренный выход)---------------
function ExitHammerOnNails(nail1, nail2, nail3, nail4, m_nail)
	room:GetItem(nail1):Hide()
	room:GetItem(nail2):Hide()
	room:GetItem(nail3):Hide()
	room:GetItem(nail4):Show()
	
	local mask_nail = room:GetItem(m_nail)
	if mask_nail:IsVisible() then
		mask_nail:Hide()
		stool_ready = stool_ready + 1
	end
end
----------------поднять деревяшку на курсор------------------------
function SetPartOnCursor (part, part_fixed)
	part:Hide()
	part_fixed:Show()
	room:SetItemOnCursor(part_fixed)
	room.sfx:Play ("take_wood_detail")						--звук "взяли ножку стула"
	
	EnableParts()
end
----------------поместить деревяшку на место----------------------
function SetPartOnPlace(name, m_name, part, part_fixed)
	if name == m_name then									--деревяшка на место для деревяшки
		cursor:RemoveItem()
		room:GetItem(m_name):Hide()
		part_fixed:Show()
		stool_ready = stool_ready + 1
		if (stool_ready == 3) then
			cstore:SetKey("Stool_Ready", 1)
		end
		room.sfx:Play ("put_wood_detail")					--звук "вставили ножку"
		
		DisableParts()
	
	elseif name == "BACKGROUND" then						--деревяшка на фон
		cursor:RemoveItem()
		part:Show()
		room.sfx:Play ("drop_wood_detail")					--звук "положили ножку стула"
		
		DisableParts()
	end
end
----------------поднять молоток на курсор---------------------
function SetHammerOnCursor (hammer_name)
	hammer = 5
	local itemHammer = inventory.alt:GetItem(hammer_name)
	cursor:SetIventoryItem(itemHammer)
end
---------------поменять молоток на курсоре---------------------
function ChangeHammerOnCursor ()
	if cursor:IsItemNameEqual(I_HAMMER) then
		inventory.alt:AddItemFromCursor()
		local itemHammer = inventory.alt:GetItem(I_HAMMER1_D)
		cursor:SetIventoryItem(itemHammer)
	end
end
----вспомогательная функция для сохранения деревяшек---------
function SaveParts(itemName1, itemName2)
	if cursor:IsItemNameEqual(itemName1) then
		cursor:RemoveItem()
		room:GetItem(itemName2):Show()
		
		DisableParts()
	end
end
-----------------дисейблит деревяшки------------------------
function DisableParts()
	room:GetItem(I_PART1_FIXED):Disable()
	room:GetItem(I_PART2_FIXED):Disable()
	room:GetItem(I_PART3_FIXED):Disable()
	room:GetItem(I_STOOL_BASE):Disable()
end
-----------------энейблит деревяшки------------------------
function EnableParts()
	room:GetItem(I_STOOL_BASE):Enable()
	room:GetItem(I_PART1_FIXED):Enable()
	room:GetItem(I_PART2_FIXED):Enable()
	room:GetItem(I_PART3_FIXED):Enable()
end
