-- Items to use
-- 05_matchbox 05_match

function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/Minigames/04_lamp.lua', 'quest', 'data/Scenes/05_Gandarmery_Wart.lua')
    
    play_field:IgnoreOnTestPoint('cvet');
    
	MiniGameScreen = screen;
    
    game_state.can_used = false;
    game_state.lighter_parts_used = false;
    game_state.skip_minigame = false;
    game_state.complited = false;
    game_state.minigame_state = "none"
    
    OBECTS_AMOUNT = 8;
    
    Objects = {};
	
	for i = 1,OBECTS_AMOUNT do
		Objects[i] = {};
	end
	
    Objects[1].Obj = screen:GetObject('spichka');                                   --спичка
    
    Objects[2].Obj = screen:GetObject('korobok');                                   --спичечный коробок
    Objects[3].Obj = screen:GetObject('korobok_shadow');                            --тень от спичечного коробка
    
    Objects[4].Obj = screen:GetObject('lamp');                                      --огонь в лампе
	Objects[5].Obj = screen:GetObject('cvet');                                      --glow от огня в лампе 
	Objects[8].Obj = screen:GetObject('ten');                                       --тень от лампы

	Objects[6].Obj = screen:GetObject('fire_match');                                --огонь на спичке
	Objects[7].Obj = screen:GetObject('spichka_shadow');                            --тень спички
	
	auxAttachInPlace(Objects[6].Obj, Objects[1].Obj);

    for i = 1, OBECTS_AMOUNT do
		Objects[i].Obj:SetVisible(false);
	end

    need_hint = false
    
	if not HasGameState('ToolFound', "05_matchbox") or not HasGameState('ToolFound', "05_match") then
		ShowMonolog('C_05_CANT_FIRE_LAMP_HINT', 'C_05_CANT_FIRE_LAMP_HINT');
	else
		if (HasGameState('ToolApplied', "05_matchbox") == false or HasGameState('ToolApplied', "05_match") == false) then
		    --ShowMonolog('C_05_CAN_FIRE_LAMP_HINT', 'C_05_CAN_FIRE_LAMP_HINT');
		    need_hint = true
		end
		
		RegisterMatchBoxToolApply();
		RegisterMatchToolApply();
	end
    
    if(HasGameState('MINI_GAME_04_LAMP_STATE', "1") == true) then                   -- спичка уже взята
		StartDrag(Objects[1]);
		Objects[7].Obj:SetVisible(false);
		game_state.minigame_state = 'state_1';
	end
	
	if(HasGameState('MINI_GAME_04_LAMP_STATE', "2") == true) then                   -- спичку зажгли
		Objects[6].Obj:SetVisible(true);
		game_state.minigame_state = 'state_2';
	end	
	
	if(HasGameState('MINI_GAME_04_LAMP_STATE', "3") == true) then                   -- лампу зажгли
		Objects[1].Obj:SetVisible(false);
		Objects[6].Obj:SetVisible(false);
		Objects[4].Obj:SetVisible(true);
		Objects[5].Obj:SetVisible(true);
		Objects[8].Obj:SetVisible(true);
		
		need_hint = false
		DragObject = nil;
		game_state.minigame_state = 'finish';
		ActiveLamp();
	end
	
	--
	if need_hint == true then
	    ShowMonolog('C_05_CAN_FIRE_LAMP_HINT', 'C_05_CAN_FIRE_LAMP_HINT');
	end
end

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

function RegisterMatchBoxToolApply()
    RegisterToolApplyInfo('05_matchbox', 'area', nil,  nil,     function(tool_name)        
																    for i = 2,3 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.match_box_used = true;
																    if(game_state.match_used == true) then
																	    StartMiniGame();
																    end
                                                                end,
                                                                function(tool_name)                                                                 
                                                                    Objects[2].Obj:SetVisible(true);
																	Objects[3].Obj:SetVisible(true);
                                                                    
                                                                    game_state.match_box_used = true;	
																	if(game_state.match_used == true) then
																		StartMiniGame();
																   end													
                                                                end);
end

function RegisterMatchToolApply()
    RegisterToolApplyInfo('05_match', 'area', nil, nil,         function(tool_name)      
																	Objects[1].Obj:SetVisible(true);
																	Objects[1].Obj:ChangeOpacity(0.0, 1.0, 0.5);
																	Objects[1].Obj:ChangeScale(0.5, 1.0, 0.2);
																	Objects[7].Obj:SetVisible(true);
																	Objects[7].Obj:ChangeOpacity(0.0, 1.0, 0.5);
																	Objects[7].Obj:ChangeScale(0.5, 1.0, 0.2);
																	
																	game_state.match_used = true; 
																	if(game_state.match_box_used == true) then
																		StartMiniGame();
																    end                                                                   
                                                                end,
                                                                function(tool_name)                                                                 
																	Objects[1].Obj:SetVisible(true);
																	Objects[7].Obj:SetVisible(true);
																	
                                                                    game_state.match_used = true;
                                                                    if(game_state.match_box_used == true) then
																		StartMiniGame();
																	end                                                                   

                                                                end);
end

function ActiveLamp()
	RegisterToolFindInfo('05_lamp', 	'area_circle_3',        function(tool_name)
                                                                    game_state.complited = true;
                                                                    --[[MiniGameScreen:GetObject('cvet'):ChangeOpacity(1.0, 0.0, 0.2);
                                                                    MiniGameScreen:GetObject('lampa_shadow'):ChangeOpacity(1.0, 0.0, 0.2);
                                                                    MiniGameScreen:GetObject('lampa'):ChangeOpacity(1.0, 0.0, 0.2);
                                                                    MiniGameScreen:GetObject('lamp'):ChangeOpacity(1.0, 0.0, 0.2);
                                                                    MiniGameScreen:GetObject('ten'):ChangeOpacity(1.0, 0.0, 0.2);
                                
                                                                    MiniGameScreen:GetObject('area_circle_3'):SetVisible(false);]]
                                                                    
                                                                    if (HasGameState('ClosedScene', game_state.scene_path) == false) then
                                                                        AddGameState('ClosedScene', game_state.scene_path);
                                                                    end
                                                                    if (HasGameState('MiniGameCompleted', '04_lamp') == false) then
                                                                        AddGameState('MiniGameCompleted', '04_lamp');
                                                                    end
                                                                    StartLevel(game_state.parent_scene)
                                                                end,
                                                                
                                                                function(tool_name)
                                                                    StartLevel(game_state.parent_scene)
                                                                end);
end

function StartMiniGame()
	game_state.level_mode = 'mini_game';
	play_field:GetLevelScreen():GetPanel():SetMode(PM_MINIGAME)
	MiniGameScreen:GetObject('area'):SetVisible(false);
	need_hint = true
end

function StartQuestMode()
	--game_state.level_mode = 'quest';
	--play_field:GetLevelScreen():GetPanel():SetMode(PM_QUEST)
	play_field:GetLevelScreen():GetPanel():SetDelayTimer( play_field:GetLevelScreen():GetPanel():GetMaxDelayTimer() )
	PanelOnObjectMouseLeave(play_field:GetLevelScreen():GetPanel():GetObject('button_r_skip'))
	play_field:GetLevelScreen():GetPanel():StopHintTimer()
end

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

function OnUpdate(dt)
    DefaultOnUpdate(dt)
   -- UpdateDrag();
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)
    if not DragObject then
        DefaultOnObjectMouseLeave(object)
    end
end

function OnObjectMouseDown(object)
    local is_wrong_click = not DefaultOnObjectMouseDown(object)
    local obj_name = object:GetName()
    local pf = play_field
    
    if obj_name == 'background' and game_state.minigame_state == 'finish' and not IsAboveReturnFromZoomZone() then
        ShowMonolog('C_05_GET_LAMP_HINT', 'C_05_GET_LAMP_HINT');
    end

	if(obj_name == "area") then
		if not HasGameState('ToolFound', "05_matchbox") or not HasGameState('ToolFound', "05_match") then
			ShowMonolog('C_05_CANT_FIRE_LAMP_HINT', 'C_05_CANT_FIRE_LAMP_HINT');
		elseif (HasGameState('ToolFound', "05_match") == false) then
			ShowMonolog('C_05_CAN_FIRE_LAMP_HINT', 'C_05_CAN_FIRE_LAMP_HINT');
		end
	end
		
	if(obj_name == 'area_circle' and DragObject == nil and game_state.minigame_state == 'none') then
		StartDrag(Objects[1]);
		Objects[7].Obj:SetVisible(false);
		game_state.minigame_state = 'state_1';
		if(HasGameState('MINI_GAME_04_LAMP_STATE', "1") == false) then
			AddGameState('MINI_GAME_04_LAMP_STATE', "1")
		end
	end
	
	if(obj_name == 'area_circle_2' and DragObject ~= nil and game_state.minigame_state == 'state_1') then
		Objects[6].Obj:SetVisible(true);
		Objects[6].Obj:ChangeOpacity(0.0, 1.0, 0.2);
		game_state.minigame_state = 'state_2';
		PlaySound('05_MG_FireMatch');
		if(HasGameState('MINI_GAME_04_LAMP_STATE', "2") == false) then
			AddGameState('MINI_GAME_04_LAMP_STATE', "2")
		end
	end
	
	if(obj_name == 'area_circle_3' and DragObject ~= nil and game_state.minigame_state == 'state_2') then
		Objects[1].Obj:ChangeOpacity(1.0, 0.0, 0.2);
		Objects[4].Obj:SetVisible(true);
		Objects[4].Obj:ChangeOpacity(0.0, 1.0, 0.2);
		Objects[5].Obj:SetVisible(true);
		PlaySound('05_MG_FireLamp');
		SetCursor('CursorDefault');
		DragObject = nil;
		game_state.minigame_state = 'finish';
		ActiveLamp();
		if(HasGameState('MINI_GAME_04_LAMP_STATE', "3") == false) then
			AddGameState('MINI_GAME_04_LAMP_STATE', "3")
		end
	end
	
    DefaultOnObjectMouseDownProcessed(is_wrong_click);
end

function OnObjectMouseUp(object)
    DefaultOnObjectMouseUp(object)
end

function SkipMiniGame()
	Objects[1].Obj:SetVisible(false);
    SetCursor('CursorDefault');
	DragObject = nil;
	game_state.minigame_state = 'finish';
	if(HasGameState('MINI_GAME_04_LAMP_STATE', "3") == false) then
			AddGameState('MINI_GAME_04_LAMP_STATE', "3")
	end		
	
    ActiveLamp();
    
	Objects[4].Obj:SetVisible(true)
	Objects[5].Obj:SetVisible(true)
	Objects[6].Obj:SetVisible(false)
	Objects[7].Obj:SetVisible(false)
	 
	--[[if not HasGameState('ToolFound', '05_lamp') then
		AddGameState('ToolFound', '05_lamp')
	end
	if (HasGameState('ClosedScene', game_state.scene_path) == false) then
		AddGameState('ClosedScene', game_state.scene_path);
	end
	if (HasGameState('MiniGameCompleted', '04_lamp') == false) then
		AddGameState('MiniGameCompleted', '04_lamp');
	end
	
	TimerDoString('StartLevel(game_state.parent_scene)', 0.4, MiniGameScreen:GetTimer())]]
	StartQuestMode()
end

----------------------------------------------------------------------------------------------------------

function StartDrag(Obj)
	DragObject = Obj;
	DragObject.Obj:SetZ(DragObject.Obj:GetZ() + 200);
	SetCursor('CursorHand');
	MiniGameScreen:IgnoreOnTestPoint(DragObject.Obj:GetName());
end

function UpdateDrag()
	if(DragObject ~= nil) then
		DragObject.Obj:SetXY(GetMousePos().x, GetMousePos().y);
	end
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)
    
    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/04_lamp/background.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('background',anim,512,384,97,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/04_lamp/lampa.png',0,0,275,551,1,0)
	anim:SetHotSpot(137,275)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('lampa',anim,343,240,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/04_lamp/lampa_shadow.png',0,0,231,214,1,0)
	anim:SetHotSpot(115,107)
	anim:SetBlendMode(2)
	anim:SetColor('0x80FFFFFF')
	screen:CreateObject('lampa_shadow',anim,235,393,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/04_lamp/korobok.png',0,0,180,192,1,0)
	anim:SetHotSpot(90,96)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('korobok',anim,684,420,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/04_lamp/korobok_shadow.png',0,0,152,186,1,0)
	anim:SetHotSpot(76,93)
	anim:SetBlendMode(2)
	anim:SetColor('0x80FFFFFF')
	screen:CreateObject('korobok_shadow',anim,628,436,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/04_lamp/fire3.png',0,0,64,128,31,10)
	anim:SetHotSpot(32,64)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('fire_match',anim,528,472,121,0,0.4,0.6)
	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',anim,491,400,210,0,100,70)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_circle',anim,479,511,200,340,1.1,0.2)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_circle_2',anim,678,409,200,0,1.5,1.4)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_circle_3',anim,340,228,200,-11,1.9,4.3)
	local anim = CreateAnimation('data/Texture/Minigames/04_lamp/spichka.png',0,0,104,52,1,0)
	anim:SetHotSpot(52,26)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('spichka',anim,477,514,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/04_lamp/spichka_shadow.png',0,0,113,57,1,0)
	anim:SetHotSpot(56,28)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('spichka_shadow',anim,473,516,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/04_lamp/ten.png',0,0,343,240,3,25)
	anim:SetHotSpot(171,360)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ten',anim,366,671,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/04_lamp/lamp.png',0,0,280,364,34,25)
	anim:SetHotSpot(140,182)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('lamp',anim,346,243,111,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/04_lamp/cvet.png',0,0,908,768,3,25)
	anim:SetHotSpot(908,768)
	anim:SetBlendMode(0)
	anim:SetColor('0x60FFFFFF')
	screen:CreateObject('cvet',anim,899,763,120,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
