function OnSceneLoad(screen)
    LoadData(screen)
    MiniGameScreen = screen   
    g_timer = screen:GetTimer()
    
    SetContinueMode(2)  -- миниигру нужно заканчивать вручную
    
    Objects = {}
    Objects.count = 16
    FIELD_SIZE = 4
	PIXELS = 87
	START_X = 388
	START_Y = 309
	MOVE_TIME = 0.25	
    
    game_state.completed = false
    game_state.skip_minigame = false
    game_state.lock_mouse = false
  
    for i = 1, Objects.count do
        Objects[i] = MiniGameScreen:GetObject('Button_'..i)       
    	Objects[i].Index = i
    	Objects[i].RightIndex = i
    	
    end
    Objects[16]:SetOpacity(0.0)
  
    Randomize()
  
	for i = 1, Objects.count do
	    SetPosByIndex(i)
	end
	    
	MiniGameScreen:IgnoreOnTestPoint('coin_take_new')
end

function Randomize()
    local rand = math.random(1, 3)
    if rand == 1 then
        Objects[1].Index = 2; Objects[2].Index = 10; Objects[3].Index = 8; Objects[4].Index = 12; Objects[5].Index = 13; Objects[6].Index = 15;
        Objects[7].Index = 1; Objects[8].Index = 7; Objects[9].Index = 14; Objects[10].Index = 9; Objects[11].Index = 11; Objects[12].Index = 4;
        Objects[13].Index = 5; Objects[14].Index = 3; Objects[15].Index = 6; Objects[16].Index = 16
    elseif rand == 2 then
        Objects[1].Index = 6; Objects[2].Index = 11; Objects[3].Index = 14; Objects[4].Index = 7; Objects[5].Index = 8; Objects[6].Index = 2;
        Objects[7].Index = 4; Objects[8].Index = 15; Objects[9].Index = 13; Objects[10].Index = 10; Objects[11].Index = 1; Objects[12].Index = 3;
        Objects[13].Index = 9; Objects[14].Index = 5; Objects[15].Index = 12; Objects[16].Index = 16
    else
        Objects[1].Index = 11; Objects[2].Index = 4; Objects[3].Index = 7; Objects[4].Index = 13; Objects[5].Index = 2; Objects[6].Index = 9;
        Objects[7].Index = 14; Objects[8].Index = 12; Objects[9].Index = 10; Objects[10].Index = 15; Objects[11].Index = 5; Objects[12].Index = 1;
        Objects[13].Index = 6; Objects[14].Index = 8; Objects[15].Index = 3; Objects[16].Index = 16
    end -- index - number button, value - number cell
end

function SetPosByIndex(index)
    local n = (Objects[index].Index-1) % FIELD_SIZE
    local m = math.floor((Objects[index].Index-1) / FIELD_SIZE)  
    local x, y = n*PIXELS, m*PIXELS
	Objects[index]:SetXY(START_X + x, START_Y + y)
end

function GetPosByIndex(index)
    local n = (index-1) % FIELD_SIZE
    local m = math.floor((index-1) / FIELD_SIZE)   
    return n*PIXELS, m*PIXELS
end

function FinishMiniGame()
    -- Quit minigame  
    game_state.completed = true
    for i = 1, Objects.count do
	    MiniGameScreen:IgnoreOnTestPoint('Button'..i)
	    local x_obj_i = Objects[i]:GetX()
	    local y_obj_i = Objects[i]:GetY()
	    Objects[i]:AttachTo(MiniGameScreen:GetObject('Door'))
	    Objects[i]:SetScrXY(x_obj_i, y_obj_i)
    end
	local DoorObj = MiniGameScreen:GetObject('Door')
	DoorObj:Move(700, 0, 2.0) 
	PlaySound("24_mg_open")
--    MiniGameCompleted(1.5)
    TimerDoString('MiniGameScreen:DoNotIgnoreOnTestPoint("coin_take_new")', 2.0, play_field:GetTimer())
end

function IsComplete()
    for i = 1, Objects.count do
        if Objects[i].Index ~= Objects[i].RightIndex then
            return false
        end
    end
    
    return true
end

function GetObjectByName(obj_name)
	for i = 1, Objects.count do
		if obj_name == Objects[i]:GetName() then
			return Objects[i]
		end
	end
	
	return nil
end

function OnSceneFree()
    FreeData()
end

function OnScreenShown()
end

function OnHiddenComics()
end

function OnUpdate(dt)
    DefaultOnUpdate(dt)
end

function OnLose()
end

function OnExit()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnObjectMouseDown(object)
    obj_name = object:GetName()   
--    local is_wrong_click = not DefaultOnObjectMouseDown(object)
   
	if not game_state.lock_mouse and not game_state.completed then
        local obj = GetObjectByName(obj_name)
        if obj then
            local n, m
            n = (obj.Index-1) % FIELD_SIZE + 1
            m = math.floor((obj.Index-1) / FIELD_SIZE) + 1
            -- Find empty cell
            local empty_cell, n_e, m_e
            for i = 1, Objects.count do
                if Objects[i].RightIndex == 16 then
                    empty_cell = Objects[i]
                    n_e = (empty_cell.Index-1) % FIELD_SIZE + 1
                    m_e = math.floor((empty_cell.Index-1) / FIELD_SIZE) + 1
                    break
                end
            end
            local r_n, r_m = n - n_e, m - m_e
            if (math.abs(r_n) == 1 and r_m == 0) or (r_n == 0 and math.abs(r_m) == 1) then
                -- swap
                local swap = empty_cell.Index
                empty_cell.Index = obj.Index
                obj.Index = swap
                -- move objects
                local x, y = GetPosByIndex(obj.Index)
                obj:Move((START_X + x) - obj:GetX(), (START_Y + y) - obj:GetY(), MOVE_TIME)              
                PlaySound('24_mg_click_in_counter')
                
                x, y = GetPosByIndex(empty_cell.Index)
                empty_cell:SetXY(START_X + x, START_Y + y)
                -- lock/unlock mouse
                game_state.lock_mouse = true
                TimerDoString('game_state.lock_mouse = false', MOVE_TIME, play_field:GetTimer())                
               
                if IsComplete() then
                   TimerDoString('FinishMiniGame()', MOVE_TIME, play_field:GetTimer())
                end
            end
        end
        
    elseif obj_name == 'coin_take_new' then
            MiniGameScreen:IgnoreOnTestPoint('coin_take_new')
            local ef = CreateAnimationEffector()
            ef:AttachTo(object)
            ef:SetMode(_ANIM_FWD)
            ef:SetKey(_TRACK_O, 0, object:GetOpacity())
            ef:SetKey(_TRACK_O, 0.75, 0)
            ef:Play()
            
            TimerDo(g_timer, 1, OnCompletedGame)
    end
   
    --DefaultOnObjectMouseDownProcessed(is_wrong_click);
end

function OnObjectMouseUp(object)
end

function OnObjectRightMouseDown(object)
end

function OnObjectRightMouseUp(object)
end

function OnSkipMiniGame()
	if game_state.minigame_complete then
        return
    end
    
    if game_state.skip_minigame == false and game_state.completed == false then
	    game_state.completed = true
	    game_state.skip_minigame = true
		for i = 1, Objects.count do
		    local x, y = GetPosByIndex(Objects[i].RightIndex)
            Objects[i]:Move((START_X + x) - Objects[i]:GetX(), (START_Y + y) - Objects[i]:GetY(), MOVE_TIME)
			Objects[i].Index = Objects[i].RightIndex			
			--PlaySound('E_03_TileMove');
		end
		TimerDoString('FinishMiniGame()', MOVE_TIME, play_field:GetTimer())
	end
end

function OnButtonPress(title)
    DefaultMinigameOnButtonPress(title)
end

function OnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
end

function OnLostFocus()
	DefaultMinigameOnLostFocus()
end

function OnGainFocus()
	DefaultMinigameOnGainFocus()
end

function ShowMiniGameHelp()
    CreateDialogMiniGameHelp('MINI_GAME_HELP_METAL_BOX_15')
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/24_metal_box_15/Background.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Background',anim,512,384,95,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/24_metal_box_15/Door.png',0,0,963,502,1,0)
	anim:SetHotSpot(481,251)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Door',anim,535,436,98,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/24_metal_box_15/Button_1.png',0,0,87,87,1,0)
	anim:SetHotSpot(43,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_1',anim,388,309,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/24_metal_box_15/Button_2.png',0,0,87,87,1,0)
	anim:SetHotSpot(43,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_2',anim,475,309,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/24_metal_box_15/Button_3.png',0,0,87,87,1,0)
	anim:SetHotSpot(43,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_3',anim,562,309,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/24_metal_box_15/Button_4.png',0,0,87,87,1,0)
	anim:SetHotSpot(43,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_4',anim,649,309,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/24_metal_box_15/Button_5.png',0,0,87,87,1,0)
	anim:SetHotSpot(43,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_5',anim,388,396,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/24_metal_box_15/Button_6.png',0,0,87,87,1,0)
	anim:SetHotSpot(43,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_6',anim,475,396,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/24_metal_box_15/Button_7.png',0,0,87,87,1,0)
	anim:SetHotSpot(43,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_7',anim,562,396,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/24_metal_box_15/Button_8.png',0,0,87,87,1,0)
	anim:SetHotSpot(43,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_8',anim,649,396,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/24_metal_box_15/Button_9.png',0,0,87,87,1,0)
	anim:SetHotSpot(43,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_9',anim,388,483,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/24_metal_box_15/Button_10.png',0,0,87,87,1,0)
	anim:SetHotSpot(43,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_10',anim,475,483,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/24_metal_box_15/Button_11.png',0,0,87,87,1,0)
	anim:SetHotSpot(43,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_11',anim,562,483,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/24_metal_box_15/Button_12.png',0,0,87,87,1,0)
	anim:SetHotSpot(43,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_12',anim,649,483,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/24_metal_box_15/Button_13.png',0,0,87,87,1,0)
	anim:SetHotSpot(43,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_13',anim,388,570,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/24_metal_box_15/Button_14.png',0,0,87,87,1,0)
	anim:SetHotSpot(43,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_14',anim,475,570,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/24_metal_box_15/Button_15.png',0,0,87,87,1,0)
	anim:SetHotSpot(43,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_15',anim,562,570,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('0xFFFFFFFF')
	screen:CreateObject('Button_16',anim,649,570,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/24_metal_box_15/coin_take.png',0,0,159,144,1,0)
	anim:SetHotSpot(79,72)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('coin_take_new',anim,505,441,97,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
