function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/Minigames/02_lighter.lua', 'quest', 'data/Scenes/02_Dupens_House_Main.lua')
    
    MiniGameScreen = screen;
    
    game_state.can_used = false;
    game_state.lighter_parts_used = false;
    game_state.skip_minigame = false;
    game_state.complited = false;
        
    Objects = {};
	
	for i = 1,10 do
		Objects[i] = {};
	end
	
    Objects[1].Obj = screen:GetObject('batl_01');           --Банка с керасином
    Objects[2].Obj = screen:GetObject('krishkal');          --Крышка с баночки
    
    Objects[3].Obj = screen:GetObject('denal_01');          --каркас зажигалки
    Objects[4].Obj = screen:GetObject('denal_02');          --ватка чистая
    Objects[5].Obj = screen:GetObject('denal_03');          --ролик
    Objects[6].Obj = screen:GetObject('denal_04');          --серцевина пустая
    
	Objects[7].Obj = screen:GetObject('denal_02_ispachk');  --ватка пропитанная
    Objects[8].Obj = screen:GetObject('denal_05');          --ватка пропитанная вставленная
    Objects[9].Obj = screen:GetObject('bint_smol');         --серцевина вставленная
    
    Objects[10].Obj = screen:GetObject('zashigalka');       --собранная зажигалка
    
    for i = 1,10 do
		Objects[i].StartPos = Objects[i].Obj:GetPosition();
		Objects[i].Obj:SetVisible(false);
    end
    
	Objects[4].NeedPos = Objects[1].Obj:GetPosition();
    Objects[4].applied = function()
		Objects[7].Obj:SetVisible(true);
		Objects[4].Obj:SetVisible(false);
		game_status = "none" 
		
		TimerDoString('game_status = "cotton_wet"', 0.5, play_field:GetTimer());
		Objects[7].Obj:ChangeOpacity(0.0, 1.0, 0.5);
		Objects[7].Obj:ChangeScale(0.5, 1.0, 0.2);
		
		SaveGame(1);
	end

	Objects[7].NeedPos = Objects[6].Obj:GetPosition();
    Objects[7].applied = function()
		Objects[7].Obj:SetVisible(false);
		Objects[9].Obj:SetVisible(true);
		game_status = "none" 
		
		TimerDoString('game_status = "cotton_wet_push"', 0.5, play_field:GetTimer());
		Objects[9].Obj:ChangeOpacity(0.0, 1.0, 0.5);
		Objects[9].Obj:ChangeScale(0.5, 1.0, 0.2);
		auxAttachInPlace(Objects[9].Obj, Objects[6].Obj);
		SaveGame(2);
	end
	
	Objects[6].NeedPos = Objects[3].Obj:GetPosition();
    Objects[6].applied = function()
		Objects[8].Obj:SetVisible(true);
		Objects[6].Obj:SetVisible(false);
		Objects[9].Obj:SetVisible(false);
		game_status = "none" 
		
		TimerDoString('game_status = "whell"', 0.5, play_field:GetTimer());
		Objects[8].Obj:ChangeOpacity(0.0, 1.0, 0.5);
		Objects[8].Obj:ChangeScale(0.5, 1.0, 0.2);
		SaveGame(3);
	end
	
	Objects[5].NeedPos = Objects[3].Obj:GetPosition();
    Objects[5].applied = function()
        -- Final (lighter gathered)
		Objects[8].Obj:SetVisible(false);
		Objects[5].Obj:SetVisible(false);
		Objects[3].Obj:SetVisible(false);
		Objects[10].Obj:SetVisible(true);
		
        game_status = "none" 
		PlaySound('02_MG_Gather_Lighter');
		
		TimerDoString('game_status = "continue"', 0.5, play_field:GetTimer());
		Objects[10].Obj:ChangeOpacity(0.0, 1.0, 0.5);
		Objects[10].Obj:ChangeScale(0.5, 1.0, 0.2);
		StartQuestMode()
				
		SaveGame(4);
	end
	
    game_status = "none" 
    
	RegisterToolFindInfo('02_lighter', Objects[10].Obj:GetName(), function(tool_name)
													game_state.complited = true;
													if (HasGameState('ClosedScene', game_state.scene_path) == false) then
														AddGameState('ClosedScene', game_state.scene_path);
													end
													if (HasGameState('MiniGameCompleted', '02_lighter') == false) then
														AddGameState('MiniGameCompleted', '02_lighter');
													end
													
													TimerDoString("StartLevel('"..game_state.parent_scene.."')", 0.4, MiniGameScreen:GetTimer())
                                                end,
                                                
                                                function(tool_name)
													game_state.complited = true;
                                                end);
                                                
    
    if(HasGameState('ToolFound', "02_can") and HasGameState('ToolFound', "02_lighter_parts") )then
		RegisterVialToolApply();
		RegisterLighterPartToolApply();
    end
    
    if(HasGameState('ToolFound', "02_can") and HasGameState('ToolFound', "02_lighter_parts")) then
		if(game_state.can_used == false and game_state.lighter_parts_used == false) then
			ShowMonolog('C_02_MINI_GAME_LIGHTER_2', 'C_02_MINI_GAME_LIGHTER_2');
		end
	else
		ShowMonolog('C_02_MINI_GAME_LIGHTER_1', 'C_02_MINI_GAME_LIGHTER_1');
	end
	
	LoadGame();
end

function LoadGame()
	local State = 0;
	
	for i = 1,4 do
		if(HasGameState('MINI_GAME_02_STATE', "0"..i)) then
			State = i;
			break;
		end
	end
	
	if(State > 0) then
		if(State == 1) then --уже намочена ватка
			game_status = "cotton_wet";
			Objects[7].Obj:SetVisible(true);
			Objects[4].Obj:SetVisible(false);
		end
		if(State == 2) then --ватку вставили в серцевину
			game_status = "cotton_wet_push";
			Objects[7].Obj:SetVisible(false);
			Objects[4].Obj:SetVisible(false);
			Objects[9].Obj:SetVisible(true);
			auxAttachInPlace(Objects[9].Obj, Objects[6].Obj);
		end
		if(State == 3) then --вставили серцевину
			game_status = "whell";
			Objects[7].Obj:SetVisible(false);
			Objects[4].Obj:SetVisible(false);
			Objects[8].Obj:SetVisible(true);
			Objects[6].Obj:SetVisible(false);
			Objects[9].Obj:SetVisible(false);
		end
		if(State == 4) then --вставили ролик, собрали зажигалку
			game_status = "continue";
			Objects[7].Obj:SetVisible(false);
			Objects[4].Obj:SetVisible(false);
			Objects[8].Obj:SetVisible(false);
			Objects[6].Obj:SetVisible(false);
			Objects[9].Obj:SetVisible(false);
			Objects[5].Obj:SetVisible(false);
			Objects[3].Obj:SetVisible(false);
			Objects[10].Obj:SetVisible(true);
			
			StartQuestMode()
		end
	end
end

function SaveGame(State)
	for i = 1,4 do
		if(HasGameState('MINI_GAME_02_STATE', "0"..i)) then
			RemoveGameState('MINI_GAME_02_STATE', "0"..i)
		end
	end
	
	AddGameState('MINI_GAME_02_STATE', "0"..State)
end

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

function RegisterVialToolApply()
    RegisterToolApplyInfo('02_can', 'area_can', nil,  nil,   function(tool_name)        
																	for i = 1,2 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
																	
																    game_state.can_used = true;
																	if(game_state.lighter_parts_used == true) then
																		StartMiniGame()
																	end
                                                                end,
                                                                function(tool_name)                                                                 
                                                                    Objects[1].Obj:SetVisible(true);
																	Objects[2].Obj:SetVisible(true);
                                                               
                                                                game_state.can_used = true;
																	if(game_state.lighter_parts_used == true) then
																		StartMiniGame()
																	end
                                                                end);
end

function RegisterLighterPartToolApply()
    RegisterToolApplyInfo('02_lighter_parts', 'area_can', nil, nil, function(tool_name)      
																	for i = 3,6 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
	
                                                                    game_state.lighter_parts_used = true;
                                                                    if(game_state.can_used == true) then
                                                                        StartMiniGame()
																    end
                                                                    
                                                                end,
                                                                function(tool_name)                                                                 
                                                                    for i = 3,6 do
																		Objects[i].Obj:SetVisible(true);
																	end
                                                                    game_state.lighter_parts_used = true;
                                                                    if(game_state.can_used == true) then
																		StartMiniGame()
																	end
                                                                end);
end

function StartMiniGame()
	game_state.level_mode = 'mini_game';
	play_field:GetLevelScreen():GetPanel():SetMode(PM_MINIGAME)
	MiniGameScreen:GetObject('area_can'):SetVisible(false);
	game_status = "cotton";
	if HasGameState('MINI_GAME_02_STATE', "04") == false then
	    ShowMonolog('C_02_MINI_GAME_LIGHTER_5', 'C_02_MINI_GAME_LIGHTER_5');
	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 OnSceneFree(screen)
    DefaultOnSceneFree()
    FreeData(screen)
end

function OnUpdate(dt)
    DefaultOnUpdate(dt)
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 StartDrag(Obj)
	DragObject = Obj;
	DragObject.Obj:SetZ(DragObject.Obj:GetZ() + 200);
	game_state.custom_cursors[DragObject.Obj:GetName()] = 'CursorHand';
    game_state.ignore_on_highlight[DragObject.Obj:GetName()] = true;
    
    game_state.disable_default_zoom_exit = true
end

function StopDrag()
	if(DragObject ~= nil) then
		if(math.abs(DragObject.NeedPos.x - DragObject.Obj:GetX()) < 100 and math.abs(DragObject.NeedPos.y - DragObject.Obj:GetY()) < 100) then
			DragObject.applied();
		else
			DragObject.Obj:Move(DragObject.StartPos.x - DragObject.Obj:GetX(), DragObject.StartPos.y - DragObject.Obj:GetY(), 0,2);
		end
		game_state.custom_cursors[DragObject.Obj:GetName()] = nil;
        game_state.ignore_on_highlight[DragObject.Obj:GetName()] = nil;
		DragObject = nil;
	end
	
	game_state.disable_default_zoom_exit = false
end

function UpdateDrag()
	if(DragObject ~= nil) then
		DragObject.Obj:SetXY(GetMousePos().x, GetMousePos().y);
	end
end

function SkipMiniGame()
	if(game_state.skip_minigame == false and game_state.complited == false) then
		for i = 3,9 do
			Objects[i].Obj:SetVisible(false);
		end
		
		Objects[10].Obj:SetVisible(true);
		game_status = "none" 
		
		TimerDoString('game_status = "continue"', 0.5, play_field:GetTimer());
				
		Objects[10].Obj:ChangeOpacity(0.0, 1.0, 0.5);
		Objects[10].Obj:ChangeScale(0.5, 1.0, 0.2);
		game_state.skip_minigame = true;
		
        StartQuestMode()	

        SaveGame(4)
	end
end

function OnObjectMouseDown(object)
    local is_wrong_click = not DefaultOnObjectMouseDown(object)
    local obj_name = object:GetName()
    local pf = play_field

	if(game_status == "cotton" and obj_name == Objects[4].Obj:GetName()) then
		StartDrag(Objects[4]);
	end
	
	if(game_status == "cotton_wet" and obj_name == Objects[7].Obj:GetName()) then
		StartDrag(Objects[7]);
	end
	
	if(game_status == "cotton_wet_push" and obj_name == Objects[6].Obj:GetName()) then
		StartDrag(Objects[6]);
	end
	
	if(game_status == "whell" and obj_name == Objects[5].Obj:GetName()) then
		StartDrag(Objects[5]);
	end
	
	if(obj_name == "area_can" and game_status == "none" and HasGameState('ToolFound', "02_can") and HasGameState('ToolFound', "02_lighter_parts")) then
		if(game_state.lighter_parts_used == false) then
			ShowMonolog('C_02_MINI_GAME_LIGHTER_4', 'C_02_MINI_GAME_LIGHTER_4');
		elseif (game_state.can_used == false) then
			ShowMonolog('C_02_MINI_GAME_LIGHTER_3', 'C_02_MINI_GAME_LIGHTER_3');
		end
	end
	
    DefaultOnObjectMouseDownProcessed(is_wrong_click);
end

function OnObjectMouseUp(object)
	StopDrag();
    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)
	if(title == "CloseInfoWindow" and game_state.complited == true) then
		TimerDoString("StartLevel('"..game_state.parent_scene.."');", 0.65, MiniGameScreen:GetTimer())
	end
    DefaultOnButtonPress(title)
end

function OnMouseMove(previous_pos, current_pos)
    DefaultOnMouseMove(previous_pos, current_pos)
    
    UpdateDrag();
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/02_Lighter/mng_bg.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('mng_bg',anim,512,384,98,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lighter/batl_01.png',0,0,220,289,1,0)
	anim:SetHotSpot(110,144)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('batl_01',anim,648,191,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lighter/denal_01.png',0,0,289,268,1,0)
	anim:SetHotSpot(144,134)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('denal_01',anim,312,454,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lighter/denal_03.png',0,0,152,169,1,0)
	anim:SetHotSpot(76,84)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('denal_03',anim,567,438,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lighter/krishkal.png',0,0,144,134,1,0)
	anim:SetHotSpot(72,67)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('krishkal',anim,724,247,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lighter/denal_04.png',0,0,146,209,1,0)
	anim:SetHotSpot(73,104)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('denal_04',anim,747,383,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lighter/denal_02.png',0,0,152,129,1,0)
	anim:SetHotSpot(76,64)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('denal_02',anim,369,223,100,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,555,314,201,0,80,50)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lighter/zashigalka.png',0,0,230,312,1,0)
	anim:SetHotSpot(115,156)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('zashigalka',anim,512,244,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lighter/bint_smol.png',0,0,65,63,1,0)
	anim:SetHotSpot(32,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bint_smol',anim,772,454,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lighter/denal_02_ispachk.png',0,0,97,63,1,0)
	anim:SetHotSpot(48,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('denal_02_ispachk',anim,381,228,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lighter/denal_05.png',0,0,213,214,1,0)
	anim:SetHotSpot(106,107)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('denal_05',anim,357,409,100,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
