-- Items ti use
-- 02_cutter_part 03_screwdriver

function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/Minigames/05_cutter.lua', 'quest', 'data/Scenes/04_Bridge_Main.lua')
    
    game_state.scene_path = 'data/Scenes/Mnigames/05_cutter.lua';
    MiniGameScreen = screen;
    game_state.screen = screen;
    OBJECTS_AMOUNT = 6
    game_state.ignore_add_tool = true  -- для отображения ножниц уже после выхода с миниигры    
    
    Objects = {};
	
	for i = 1,OBJECTS_AMOUNT do
		Objects[i] = {};
	end
	
	if(HasGameState('ToolApplied', "02_cutter_part") == false) then
		ShowMonolog('C_04_MINIGAME_HINT_1', 'C_04_MINIGAME_HINT_1');
	elseif(HasGameState('ToolApplied', "03_screwdriver") == false) then
		ShowMonolog('C_04_MINIGAME_HINT_2', 'C_04_MINIGAME_HINT_2');
	end
	
    Objects[1].Obj = screen:GetObject('nozhnicy_part1');                        --правая часть
    Objects[2].Obj = screen:GetObject('nozhnicy_part1_shadow');                 --тень от правой части
    
    Objects[3].Obj = screen:GetObject('nozhnicy_part2');                        --левая часть
    Objects[4].Obj = screen:GetObject('nozhnicy_part2_shadow');                 --тень от левой части
    
	Objects[5].Obj = screen:GetObject('nozhnicy_vint');                         --винтик
    
    Objects[6].Obj = screen:GetObject('cutter');                                --собранные ножницы
    
    for i = 1, OBJECTS_AMOUNT do
		Objects[i].StartPos = Objects[i].Obj:GetPosition();
		Objects[i].Obj:SetVisible(false);
    end
    
    Objects[1].Obj:SetVisible(true);
    Objects[2].Obj:SetVisible(true);
	MiniGameScreen:GetObject('area_screwdriver'):SetVisible(false);
      
    RegisterScissorsToolApply();
    RegisterScrewdriverToolApply()
	----------------------------------------------------------------------------------------------------------
	 
	RegisterToolFindInfo('04_cutter', Objects[6].Obj:GetName(), function(tool_name)
													game_state.complited_minigame = true;
													game_state.cutter_foiunded = true;
													if (HasGameState('ClosedScene', game_state.scene_path) == false) then
														AddGameState('ClosedScene', game_state.scene_path);
													end
													if (HasGameState('MiniGameCompleted', '05_cutter') == false) then
														AddGameState('MiniGameCompleted', '05_cutter');
													end
													--game_state.level_mode = "mini_game"
													TimerDoString("StartLevel('data/Scenes/04_Bridge_Main.lua');", 0.1, MiniGameScreen:GetTimer())
                                                end,
                                                
                                                function(tool_name)
													game_state.complited_minigame = true;
													if (HasGameState('ClosedScene', game_state.scene_path) == false) then
														AddGameState('ClosedScene', game_state.scene_path);
													end
													if (HasGameState('MiniGameCompleted', '05_cutter') == false) then
														AddGameState('MiniGameCompleted', '05_cutter');
													end
													--game_state.level_mode = "mini_game"
													StartLevel('data/Scenes/04_Bridge_Main.lua');
                                                end);	                                                                
end

function auxAttachInPlace(object, parentObject)
	local scrX,scrY = object:GetScrX(), object:GetScrY();
	object:AttachTo(parentObject);
	object:SetScrXY(scrX, scrY);
end

function RegisterScissorsToolApply()
    RegisterToolApplyInfo('02_cutter_part', 'area_can', nil,  nil,   function(tool_name) 
																	for i = 3,4 do
																		Objects[i].Obj:SetVisible(true);
																		Objects[i].Obj:ChangeOpacity(0.0, 1.0, 0.5);
																		Objects[i].Obj:ChangeScale(0.5, 1.0, 0.2);
																	end
																	
																	PlaySound('04_PlaceScissorsPart')
																	
                                                                    game_state.scissors_used = true;	
																	MiniGameScreen:GetObject('area_can'):SetVisible(false);
																	MiniGameScreen:GetObject('area_screwdriver'):SetVisible(true);
                                                                    --RegisterScrewdriverToolApply();
                                                                end,
                                                                function(tool_name)                                                                 
                                                                    Objects[3].Obj:SetVisible(true);
                                                                    Objects[4].Obj:SetVisible(true);
                                                                    
                                                                    game_state.scissors_used = true;
																	MiniGameScreen:GetObject('area_can'):SetVisible(false);
																	MiniGameScreen:GetObject('area_screwdriver'):SetVisible(true);		
																	-- RegisterScrewdriverToolApply();
                                                                end);
end

function RegisterScrewdriverToolApply()
    RegisterToolApplyInfo('03_screwdriver', 'area_screwdriver', nil,  nil,   function(tool_name)      
																	Objects[5].Obj:SetVisible(true);
																	Objects[5].Obj:Rotate(M_PI * 2, 1.0)
																	
																	game_state.screwdriver_used = true;
																	MiniGameScreen:GetObject('area_screwdriver'):SetVisible(false);
																	game_status = "complited";  
																	PlayCyclicSound('04_MG_CutterScrew', 0.1);
																	TimerDoString('StopCyclicSound("04_MG_CutterScrew")', 1.0, game_state.screen:GetTimer());
																	TimerDoString('ScrewdriverApply(true)', 1.0, play_field:GetTimer());  
                                                                end,
                                                                function(tool_name)                                                                 
																	Objects[5].Obj:SetVisible(true);
                                                                    game_state.screwdriver_used = true;

																	MiniGameScreen:GetObject('area_screwdriver'):SetVisible(false);
																	game_status = "complited";
																	TimerDoString('ScrewdriverApply(false)', 0.0, play_field:GetTimer());  
                                                                end);
end


function ScrewdriverApply(sound)
	for i = 1, 5 do
		Objects[i].Obj:SetVisible(false);
	end
	
	Objects[6].Obj:SetVisible(true);
	if sound then 
	    PlaySound('04_MG_CutterGather');
	end
	
	--StartQuestMode()
end

function OnSceneFree(screen)
    DefaultOnSceneFree()
    FreeData(screen)
end

function OnUpdate(dt)
    DefaultOnUpdate(dt)
    
    if game_state.cutter_foiunded
        and game_state.fly_tools:GetNReferences() == 0
        and game_state.locker:GetNReferences() == 0
        and game_state.parent_scene ~= nil
        and not game_state.closing
    then
        game_state.closing = true;
        SetCursor('CursorDefault');
        StartLevel(game_state.parent_scene);
    end
end

function OnAllItemsFound()
    DefaultOnAllItemsFound()
end

function OnLose()
    DefaultOnLose()
end

function OnExit()
    DefaultOnExit()
end

function OnClose()
    DefaultOnClose()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end


function UpdateDrag()
end

function SkipMiniGame()
	if(game_state.complited_minigame == false) then
		MiniGameScreen:GetObject('area_can'):SetVisible(false);
		MiniGameScreen:GetObject('area_screwdriver'):SetVisible(false);
		
		ScrewdriverApply(true);
	end
end

function StartQuestMode()
	--game_state.level_mode = 'quest'
	--play_field:GetLevelScreen():GetPanel():SetMode(PM_QUEST)
	
	local Panel = play_field:GetLevelScreen():GetPanel()
	
	Panel:SetDelayTimer( Panel:GetMaxDelayTimer() )
	Panel:StopHintTimer()
end

function OnObjectMouseDown(object)
    local is_wrong_click = not DefaultOnObjectMouseDown(object)
    local obj_name = object:GetName()
    local pf = play_field
	
	if(obj_name == "area_can" and HasGameState('ToolApplied', "02_cutter_part") == false) then
		ShowMonolog('C_04_MINIGAME_HINT_1', 'C_04_MINIGAME_HINT_1');
	end
	if(obj_name == "area_screwdriver" and HasGameState('ToolApplied', "03_screwdriver") == false) then
		ShowMonolog('C_04_MINIGAME_HINT_2', 'C_04_MINIGAME_HINT_2');
	end

    DefaultOnObjectMouseDownProcessed(is_wrong_click);
end

function OnObjectMouseUp(object)
    DefaultOnObjectMouseUp(object)
end

function OnObjectFound(object)
    DefaultOnObjectFound(object)
end

function OnToolFound(tool)
    DefaultOnToolFound(tool)
end

function OnToolApply(tool, object)
    DefaultOnToolApply(tool, object)
end

function OnObjectReachPanel(object)
    DefaultOnObjectReachPanel(object)
end

function OnButtonPress(title)
    DefaultOnButtonPress(title)
end

function OnMouseMove(previous_pos, current_pos)
    DefaultOnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
    DefaultOnSetTool(tool)
end

function LevelLoaded(level)
end

function OnSceneDestroy(playfield)
    DefaultOnSceneDestroy(playfield)
end

--------------------------------------------------------------------------------
-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function LoadData(screen)
	local anim = CreateAnimation('data/Texture/Minigames/05_cutter/background.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('background',anim,512,384,99,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/05_cutter/nozhnicy_part1.png',0,0,352,590,1,0)
	anim:SetHotSpot(176,295)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('nozhnicy_part1',anim,579,353,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/05_cutter/nozhnicy_part1_shadow.png',0,0,417,635,1,0)
	anim:SetHotSpot(208,317)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('nozhnicy_part1_shadow',anim,592,349,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/05_cutter/nozhnicy_part2.png',0,0,354,592,1,0)
	anim:SetHotSpot(177,296)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('nozhnicy_part2',anim,434,350,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/05_cutter/nozhnicy_part2_shadow.png',0,0,440,646,1,0)
	anim:SetHotSpot(220,323)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('nozhnicy_part2_shadow',anim,460,360,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/05_cutter/nozhnicy_vint.png',0,0,49,48,1,0)
	anim:SetHotSpot(24,24)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('nozhnicy_vint',anim,507,250,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_can',anim,560,327,209,0,100,80)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_screwdriver',anim,509,252,210,0,10,10)
	local anim = CreateAnimation('data/Texture/Minigames/05_cutter/cutter.png',0,0,629,662,1,0)
	anim:SetHotSpot(314,331)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('cutter',anim,503,369,100,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
