-- Items to be used
-- 12_corkscrew 12_wine_bottle 12_goblet

function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/Minigames/11_wine.lua', 'quest', 'data/Scenes/11_Restaurant_Main.lua')
    
    MiniGameScreen = screen;
    
    OBJECTS_AMOUNT = 9
    
    game_state.complited = false;
    game_state.skip = false;
    
    Objects = {};
	
	for i = 1, OBJECTS_AMOUNT do
		Objects[i] = {};
	end
	
    Objects[1].Obj = screen:GetObject('corkscrew_anim');                --анимация штопора
    Objects[2].Obj = screen:GetObject('bottle');                        --Бутылка
    
    Objects[3].Obj = screen:GetObject('glass');                         --бокал пустой
    Objects[4].Obj = screen:GetObject('wine_glass');                    --бокал полный
    Objects[5].Obj = screen:GetObject('corkscrew');                     --штопор

    
	Objects[6].Obj = screen:GetObject('corkscrew_tool');                --штопор для дропа
    Objects[7].Obj = screen:GetObject('bottle_tool');                   --бутылка для дропа

	Objects[8].Obj = screen:GetObject('batl_01_gorlyshko');             --пробка
	Objects[9].Obj = screen:GetObject('shtopor_prob');                  --штопор с пробкой
    
    for i = 1,OBJECTS_AMOUNT do
		Objects[i].StartPos = Objects[i].Obj:GetPosition();
		Objects[i].Obj:SetVisible(false);
    end
    
    Objects[1].Obj:GetAnimation():Stop();
    Objects[7].Obj:SetAngle(M_PI / 4);
    
    RegisterCorkscrewToolApply();
    RegisterWineBottleToolApply();
    RegisterGlassToolApply();
    
    ----------------------------------------------------------------------------------------------------------
    Objects[5].NeedPos = Objects[2].Obj:GetPosition();
    Objects[5].applied = function()

		game_status = "none" 
		
		local time = GetAnimationLength(Objects[1].Obj:GetAnimation());
		
		TimerDoString('game_status = "need_pour"', 0.5 + time + 0.4, play_field:GetTimer());
		Objects[5].Obj:SetVisible(false);
		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[1].Obj:GetAnimation():SetMode(HGEANIM_FWD + HGEANIM_NOLOOP);
		TimerDoString('Objects[8].Obj:ChangeOpacity(1.0, 0.0, 0.5)', 0.5, play_field:GetTimer());
		TimerDoString('Objects[1].Obj:GetAnimation():Play()', 0.5, play_field:GetTimer());
		TimerDoString('Objects[1].Obj:GetAnimation():Play()', 0.5, play_field:GetTimer());
		PlayCyclicSound("11_MG_OpenBottle", 0.4);
		TimerDoString('StopCyclicSound("11_MG_OpenBottle")', 0.5 + time + 0.4, play_field:GetTimer());
		TimerDoString('Objects[1].Obj:SetVisible(false)', 0.5 + time + 0.4, play_field:GetTimer());
		TimerDoString('Objects[9].Obj:SetVisible(true)', 0.5 + time + 0.4, play_field:GetTimer());
		SaveGame(1);
	end
	----------------------------------------------------------------------------------------------------------
	Objects[2].NeedPos = Objects[3].Obj:GetPosition();
    Objects[2].applied = function()
        -- Final (pour glass)
        game_status = "none" 
		PlaySound('11_MG_PourToGlass');
		
		TimerDoString('game_status = "complited"', 0.3, play_field:GetTimer());
		Objects[3].Obj:SetVisible(false);
		Objects[4].Obj:SetVisible(true);
		Objects[4].Obj:ChangeOpacity(0.0, 1.0, 0.5);
		Objects[4].Obj:ChangeScale(0.5, 1.0, 0.2);
		
		StartQuestMode()
		SaveGame(2);
	end
	
	Objects[2].start_drag = function()
		Objects[2].Obj:SetScaleX(0.5);
		Objects[2].Obj:SetScaleY(0.5);
		Objects[2].Obj:SetAngle(M_PI_OVER_4);
		DragObject = Objects[2];
	end
	
	Objects[2].stop_drag = function()
		if(math.abs(DragObject.NeedPos.x - DragObject.Obj:GetX()) < 150 and math.abs(DragObject.NeedPos.y - DragObject.Obj:GetY()) < 150) then
			Objects[2].applied();
		end
		
		Objects[2].Obj:SetScaleX(1.0);
		Objects[2].Obj:SetScaleY(1.0);
		Objects[2].Obj:SetAngle(0);
		Objects[2].Obj:SetXY(Objects[2].StartPos.x, Objects[2].StartPos.y);
	end
	
	Objects[5].stop_drag = function()
		if(math.abs(DragObject.NeedPos.x - DragObject.Obj:GetX()) < 100 and math.abs(DragObject.NeedPos.y - DragObject.Obj:GetY()) < 350) then
			Objects[5].applied();
		else
			Objects[5].Obj:Move(Objects[5].StartPos.x - Objects[5].Obj:GetX(), Objects[5].StartPos.y - Objects[5].Obj:GetY(), 0,2);
		end
	end
	----------------------------------------------------------------------------------------------------------
	LoadGame()	
	
	RegisterToolFindInfo('12_wine_glass', Objects[4].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', '11_wine') == false) then
														AddGameState('MiniGameCompleted', '11_wine');
													end
													StartLevel(game_state.parent_scene)
												end,
                                                
                                                function(tool_name)
													game_state.complited = true
													StartLevel(game_state.parent_scene)
                                                end);
    	
	if (game_status == "need_pour" or game_status == "need_opened") then 
	    ShowMonolog('C_11_MG_WINE_START', 'C_11_MG_WINE_START');
	elseif (game_state.level_mode ~= "mini_game") then
	    ShowMonolog('C_11_MG_WINE_HINT', 'C_11_MG_WINE_HINT');
	end
end

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

function LoadGame()
	local State = 0;
	
	for i = 1,4 do
		if(HasGameState('MINI_GAME_11_STATE', "0"..i)) then
			State = i;
			break;
		end
	end
	
	if(State > 0) then
		if(State == 1) then                          -- уже открыта бутылка
			game_status = "need_pour";
			Objects[8].Obj:SetVisible(false);
			Objects[5].Obj:SetVisible(false);
			Objects[9].Obj:SetVisible(true);
		end
		if(State == 2) then                          -- уже налита
			game_status = "complited";
			Objects[8].Obj:SetVisible(false);
			Objects[5].Obj:SetVisible(false);
			Objects[3].Obj:SetVisible(false);
			Objects[4].Obj:SetVisible(true);
			Objects[9].Obj:SetVisible(true);
			
			StartQuestMode()
		end
	end
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 StartMinigame()
    game_state.level_mode = 'mini_game'
    play_field:GetLevelScreen():GetPanel():SetMode(PM_MINIGAME)
end

function RegisterCorkscrewToolApply()
    RegisterToolApplyInfo('12_corkscrew', 'area_can', nil,  nil, function(tool_name)        
																	Objects[5].Obj:SetVisible(true);
																	Objects[5].Obj:ChangeOpacity(0.0, 1.0, 0.5);
																	Objects[5].Obj:ChangeScale(0.5, 1.0, 0.2);

                                                                    game_state.corkscrew_used = true;
                                                                    if(game_state.bottle == true and game_state.glass == true) then
																	    StartMinigame()
																		MiniGameScreen:GetObject('area_can'):SetVisible(false);
																		game_status = "need_opened";
																	end
                                                                end,
                                                                
                                                                function(tool_name)                                                                 
                                                                    Objects[5].Obj:SetVisible(true);
                                                                                                                                       
                                                                    game_state.corkscrew_used = true;

																	if(game_state.bottle == true and game_state.glass == true) then
																		StartMinigame()
																		MiniGameScreen:GetObject('area_can'):SetVisible(false);
																		game_status = "need_opened";
																	end
                                                                end);
end

function RegisterWineBottleToolApply()
    RegisterToolApplyInfo('12_wine_bottle', 'area_can', nil,  nil, function(tool_name)      
																	Objects[2].Obj:SetVisible(true);
																	Objects[2].Obj:ChangeOpacity(0.0, 1.0, 0.5);
																	Objects[2].Obj:ChangeScale(0.5, 1.0, 0.2);
																	Objects[8].Obj:SetVisible(true);
																	Objects[8].Obj:ChangeOpacity(0.0, 1.0, 0.5);
																	Objects[8].Obj:ChangeScale(0.5, 1.0, 0.2);
																	
																	game_state.bottle = true;
																	if(game_state.corkscrew_used == true and game_state.glass == true) then
																		StartMinigame()
																		MiniGameScreen:GetObject('area_can'):SetVisible(false);
																		game_status = "need_opened";
																	end
                                                                    
                                                                end,
                                                                
                                                                function(tool_name)                                                                 
																	Objects[2].Obj:SetVisible(true);
																	Objects[8].Obj:SetVisible(true);
																	 
                                                                    game_state.bottle = true;
                                                                    if(game_state.corkscrew_used == true and game_state.glass == true) then
																		StartMinigame()
																		MiniGameScreen:GetObject('area_can'):SetVisible(false);
																		game_status = "need_opened";
																	end
                                                                end);
end

function RegisterGlassToolApply()
    RegisterToolApplyInfo('12_goblet', 'area_can', nil,  nil, function(tool_name)      
																	Objects[3].Obj:SetVisible(true);
																	Objects[3].Obj:ChangeOpacity(0.0, 1.0, 0.5);
																	Objects[3].Obj:ChangeScale(0.5, 1.0, 0.2);
																	
																	game_state.glass = true;
																	if(game_state.corkscrew_used == true and game_state.bottle == true) then
																		StartMinigame()
																		MiniGameScreen:GetObject('area_can'):SetVisible(false);
																		game_status = "need_opened";
																	end
                                                                end,
                                                                
                                                                function(tool_name)                                                                 
																	Objects[3].Obj:SetVisible(true);
	
                                                                    game_state.glass = true;
                                                                    if(game_state.corkscrew_used == true and game_state.bottle == true) then
																		StartMinigame()
																		MiniGameScreen:GetObject('area_can'):SetVisible(false);
																		game_status = "need_opened";
																	end
                                                                end);
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)
    DefaultOnObjectMouseLeave(object)
end

function StartDrag(Obj)
	if(Obj.start_drag == nil) then
		DragObject = Obj;
	else
		Obj.start_drag();
	end
	
	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
	    game_state.custom_cursors[DragObject.Obj:GetName()] = nil;
        game_state.ignore_on_highlight[DragObject.Obj:GetName()] = nil;
		if(DragObject.stop_drag == 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
		else
			DragObject.stop_drag();
		end
		
		DragObject.Obj:SetZ(DragObject.Obj:GetZ() - 200);
		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.complited == false and game_state.skip == false) then
		DragObject = nil;
		
		Objects[1].Obj:SetVisible(false);
		Objects[2].Obj:SetVisible(true);
		Objects[3].Obj:SetVisible(false);
		Objects[4].Obj:SetVisible(true);
		Objects[5].Obj:SetVisible(false);
		Objects[6].Obj:SetVisible(false);
		Objects[7].Obj:SetVisible(false);

		Objects[8].Obj:SetVisible(false);
		Objects[9].Obj:SetVisible(true);

		game_state.skip = true
		StartQuestMode()
		
		SaveGame(2)
	end
end

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

	if(game_status == "need_opened" and obj_name == Objects[5].Obj:GetName()) then
		StartDrag(Objects[5]);
	end
	
	if(game_status == "need_pour" and obj_name == Objects[2].Obj:GetName()) then
		StartDrag(Objects[2]);
	end
	
	if obj_name == "area_can" then
	    ShowMonolog("C_11_MG_WINE_HINT", "C_11_MG_WINE_HINT")
	end
	
    DefaultOnObjectMouseDownProcessed(is_wrong_click);
end

function OnObjectMouseUp(object)
    DefaultOnObjectMouseUp(object)
    StopDrag()
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/11_wine/mng_restor.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('mng_restor_solution',anim,513,384,97,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_wine/anim.png',0,0,142,267,16,20)
	anim:SetHotSpot(71,133)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('corkscrew_anim',anim,505,126,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_wine/batl_01.png',0,0,333,603,1,0)
	anim:SetHotSpot(135,370)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bottle',anim,489,532,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_wine/platok.png',0,0,503,407,1,0)
	anim:SetHotSpot(251,203)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('shawl',anim,240,390,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_wine/rumka.png',0,0,292,446,1,0)
	anim:SetHotSpot(80,180)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('glass',anim,718,313,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_wine/rumka_full.png',0,0,272,466,1,0)
	anim:SetHotSpot(80,190)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('wine_glass',anim,718,311,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_wine/shtopor_-.png',0,0,210,168,1,0)
	anim:SetHotSpot(105,84)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('corkscrew',anim,211,550,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_wine/shtopor.png',0,0,189,299,1,0)
	anim:SetHotSpot(94,149)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('corkscrew_tool',anim,77,114,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_wine/Tools/batl_b.png',0,0,230,230,1,0)
	anim:SetHotSpot(115,115)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bottle_tool',anim,179,108,103,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,518,340,200,0,100,76.5)
	local anim = CreateAnimation('data/Texture/Minigames/11_wine/batl_01_gorlyshko.png',0,0,89,98,1,0)
	anim:SetHotSpot(44,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('batl_01_gorlyshko',anim,494,260,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_wine/shtopor_prob.png',0,0,210,168,1,0)
	anim:SetHotSpot(105,84)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('shtopor_prob',anim,214,551,103,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
