function OnSceneLoad(screen)
    LoadData(screen)
    
    Objects = {}
    OBJECTS_AMOUNT = 14;
    
    g_screen = screen;
    g_timer = screen:GetTimer();
    lock_mouse = false;
    local_button = nil
    
    function Objects:GetOpenedPos()
        local anim = self.Entity:GetAnimation();
        local dh = anim:GetHeight() - 35;
    
        dh = self.index <= OBJECTS_AMOUNT / 2 and -dh or dh;
    
        return Vector(self.closed_pos.x, self.closed_pos.y + dh);
    end
    function Objects:Move()
        local time = 0.5;
        if self.closed then
            self.Entity:Move(self.opened_pos.x - self.closed_pos.x, self.opened_pos.y - self.closed_pos.y, time);
        else
            self.Entity:Move(self.closed_pos.x - self.opened_pos.x, self.closed_pos.y - self.opened_pos.y, time);
        end
        lock_mouse = true;
        TimerDo(g_timer, time, self.ToggleClosed, self);
        self.locked = true;
        TimerDo(g_timer, time, self.Unlock, self);
    end
    function Objects:ToggleClosed()
        self.closed = not self.closed;
    end
    function Objects:Unlock()
        self.locked = false;
    end
    
    screen:IgnoreOnTestPoint('background')
        
    CreateObjects()
    CreateButtonLocal()
    
    
end

function CreateObjects()
    Objects.__index = Objects;
    for i = 1, OBJECTS_AMOUNT do
        Objects[i] = {}
        setmetatable(Objects[i], Objects)
        Objects[i].index = i;
        Objects[i].name = 'tooth_' .. i;
        Objects[i].Entity = g_screen:GetObject(Objects[i].name);
        Objects[i].closed_pos = Vector(Objects[i].Entity:GetX(), Objects[i].Entity:GetY());
        Objects[i].opened_pos = Objects[i]:GetOpenedPos();
        Objects[i].closed = true;
        Objects[i].locked = false;
    end
    CreateChains();
end

function CreateButtonLocal()
    local up = CreateAnimation('data/Texture/Minigames/23_statues_tooth/button.png',0,0,177,66,1,0)
	up:SetHotSpot(88,33);
    local over = CreateAnimation('data/Texture/Minigames/23_statues_tooth/button.png',0,66,177,66,1,0)
    over:SetHotSpot(88,33);
    local down = CreateAnimation('data/Texture/Minigames/23_statues_tooth/button.png',0,132,177,66,1,0)
    down:SetHotSpot(88,33);
	local_button = CreateButton(up, over, down, 'ResestButton', g_screen)
	local_button:AttachTo(g_screen)
	local_button:SetX(937)
	local_button:SetY(738)
	local_button:SetZ(120)
end

function CreateChains()
    Objects[1].chain = {1, 3, 5, 7, -1};  -- -1 means some rondom tooth move
    Objects[3].chain = {1, 3, 5, 7};      -- right combination
    Objects[5].chain = {1, 3, 5, 7, -1, -1};
    Objects[7].chain = {1, 3, 5, 7, -1, -1, -1};
    
    Objects[2].chain = {2, 4, 6, -1, -1, -1};
    Objects[4].chain = {2, 4, 6,};
    Objects[6].chain = {2, 4, 6, -1, -1};
    
    Objects[8].chain =  {8, 10, 12, 14, -1, -1};
    Objects[10].chain = {8, 10, 12, 14, -1, -1, -1};
    Objects[12].chain = {8, 10, 12, 14};
    Objects[14].chain = {8, 10, 12, 14, -1};
    
    Objects[9].chain =  {9, 11, 13};
    Objects[11].chain = {9, 11, 13, -1, -1};
    Objects[13].chain = {9, 11, 13, -1, -1, -1};
end

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

function UnlockMouse()
    lock_mouse = false;
end

function MoveTeeth(obj)
    for _, index in ipairs(obj.chain) do
        if index ~= -1 then
            if Objects[index].closed then
                Objects[index]:Move();
            end
        else
            local i = RandomToothIndexNotInChain(obj.chain);
            if not i then
                break;
            end
            Objects[i]:Move();
        end
    end
	MakeSoundEntity('23_move_tooth'):Play(false)

    TimerDo(g_timer, 0.5, UnlockMouse);
    TimerDo(g_timer, 0.5 + 0.05, IsCompleted);
end

function RandomToothIndexNotInChain(chain)
    local index;
    local count = 0;
    
    repeat
        count = count + 1;
        local enough = true;
        index = math.random(1, OBJECTS_AMOUNT);
        for _, i in ipairs(chain) do
            if i == index then
                enough = false;
                break;
            end
        end
        if Objects[index].locked or Objects[index].closed then
            enough = false;
        end
    until (enough or count == 50);
    
    if count == 50 then
        return nil;
    else
        return index;
    end
end

function IsCompleted()
    for i = 1, OBJECTS_AMOUNT do
        if Objects[i].closed then
            return false;
        end
    end
    
    --if local_button then local_button:Stop() end
    FinishMiniGame()
    return true;
end

function FinishMiniGame()
    if local_button then
        local_button:SetEnabled(false)
        local af = CreateAnimationEffector()
        af:AttachTo(local_button)
        af:SetKey(_TRACK_O, 0.0, local_button:GetOpacity())
        af:SetKey(_TRACK_O, game_state.hide_time_minigame, 0.0)
        af:Play()
    end
    MiniGameCompleted()
end

function OnSceneFree()
    FreeData()
end

function OnScreenShown()
end

function ShowMiniGameHelp()
    CreateDialogMiniGameHelp('MINI_GAME_HELP_TOOTH')
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 pf = play_field
--  local is_wrong_click = DefaultOnObjectMouseDown(object);
    
    local obj = GetObjectByName(obj_name);
    if not game_state.minigame_complete and obj and obj.closed and not lock_mouse then
        MoveTeeth(obj);
        is_wrong_click = false
    end
    
    return is_wrong_click;
end

function OnObjectMouseUp(object)
end

function OnObjectRightMouseDown(object)
end

function OnObjectRightMouseUp(object)
end

function OnObjectFound(object)
    DefaultOnObjectFound();
end

function OnToolFound(tool)
end

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

function OnObjectReachPanel(object)
end

function OnPhotographing()
	DefaultOnPhotographing()
end

function OnSkipMiniGame()
    if game_state.minigame_complete then
        return
    end
    
    for i = 1, OBJECTS_AMOUNT do
        if Objects[i].closed then
            Objects[i]:Move();
        end
    end
    
    --if local_button then local_button:Stop() end
    FinishMiniGame()
end

function OnButtonPress(title)
    if title == 'ResestButton' and not game_state.minigame_complete and not lock_mouse then
        for i = 1, OBJECTS_AMOUNT do
            if not Objects[i].closed then
                Objects[i]:Move();
            end
        end
        TimerDo(g_timer, 0.5, UnlockMouse);
    end
    
    DefaultMinigameOnButtonPress(title)
end

function OnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
end

function OnMinigameCompleted()
end

function OnLostFocus()
    DefaultMinigameOnLostFocus()
end

function OnGainFocus()
    DefaultMinigameOnGainFocus()
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/23_statues_tooth/background.png',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('background',anim,514,384,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/23_statues_tooth/mouth.png',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('mouth',anim,514,385,96,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/23_statues_tooth/tooth_1.png',0,0,77,148,1,0)
	anim:SetHotSpot(38,74)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('tooth_1',anim,314,508,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/23_statues_tooth/tooth_2.png',0,0,75,140,1,0)
	anim:SetHotSpot(37,70)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('tooth_2',anim,382,499,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/23_statues_tooth/tooth_3.png',0,0,87,134,1,0)
	anim:SetHotSpot(43,67)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('tooth_3',anim,446,498,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/23_statues_tooth/tooth_4.png',0,0,86,132,1,0)
	anim:SetHotSpot(43,66)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('tooth_4',anim,519,494,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/23_statues_tooth/tooth_5.png',0,0,80,136,1,0)
	anim:SetHotSpot(40,68)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('tooth_5',anim,588,498,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/23_statues_tooth/tooth_6.png',0,0,80,142,1,0)
	anim:SetHotSpot(40,71)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('tooth_6',anim,658,497,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/23_statues_tooth/tooth_7.png',0,0,72,138,1,0)
	anim:SetHotSpot(36,69)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('tooth_7',anim,728,503,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/23_statues_tooth/tooth_8.png',0,0,74,87,1,0)
	anim:SetHotSpot(37,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('tooth_8',anim,322,612,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/23_statues_tooth/tooth_9.png',0,0,75,92,1,0)
	anim:SetHotSpot(37,46)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('tooth_9',anim,380,605,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/23_statues_tooth/tooth_10.png',0,0,78,93,1,0)
	anim:SetHotSpot(39,46)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('tooth_10',anim,448,602,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/23_statues_tooth/tooth_11.png',0,0,81,99,1,0)
	anim:SetHotSpot(40,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('tooth_11',anim,517,601,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/23_statues_tooth/tooth_12.png',0,0,77,102,1,0)
	anim:SetHotSpot(38,51)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('tooth_12',anim,589,607,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/23_statues_tooth/tooth_13.png',0,0,80,95,1,0)
	anim:SetHotSpot(40,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('tooth_13',anim,658,606,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/23_statues_tooth/tooth_14.png',0,0,75,92,1,0)
	anim:SetHotSpot(37,46)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('tooth_14',anim,718,607,101,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
