function OnSceneLoad(screen)
    LoadData(screen)
    
    OBJECTS_AMOUNT = 7
    Objects = {}
    DragObject = nil
    CentrX, CentrY = 517, 388
    sound_x, sound_y = false, false
    
    g_screen = screen
    g_timer = screen:GetTimer()
    
    for i = 1, OBJECTS_AMOUNT do
        Objects[i] = {}
        Objects[i].Entity = screen:GetObject('Circle_'..i)
        Objects[i].Index = i
        Objects[i].Angle = math.rad( math.random(0, 360) )
        Objects[i].Entity:SetAngle(Objects[i].Angle)
    end
    
    screen:IgnoreOnTestPoint('Key')
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)
    --[[
    if DragObject then
        DragObject = nil
    end
    ]]

    DefaultOnObjectMouseLeave(object)
end

function OnObjectMouseDown(object)
    obj_name = object:GetName()
    local is_wrong_click = true

    local obj = GetObjectByName(obj_name)
    if obj then
        --TimerDoString('PlaySound("28_circle_rotate")', 0.1, g_timer)
        sound_x, sound_y = GetMousePos().x, GetMousePos().y
        DragObject = obj
    end
    
    return is_wrong_click
end

function OnObjectMouseUp(object)
    if DragObject then
        DragObject = nil
        IsComplete()
    end
end

function OnMouseMove(previous_pos, current_pos)
    if DragObject then
        if math.abs(current_pos.x - CentrX) < 5 and math.abs(current_pos.y - CentrY) < 5 then
            return
        end
        -- play sound
        if sound_x ~= false and sound_y ~= false then
            local mx, my = GetMousePos().x, GetMousePos().y
            if mx ~= sound_x or my ~= sound_y then
                TimerDoString('PlaySound("28_circle_rotate")', 0.06, g_timer)
                sound_x, sound_y = false, false
            end
        end
        -- current angle
        local c_angle = - math.atan( (current_pos.x - CentrX) / (current_pos.y - CentrY) )
        if current_pos.y >= CentrY then
            c_angle = c_angle + M_PI
        end
        -- previous angle
        local p_angle = - math.atan( (previous_pos.x - CentrX) / (previous_pos.y - CentrY) )
        if previous_pos.y >= CentrY then
            p_angle = p_angle + M_PI
        end
        local angle = c_angle - p_angle
        
        DragObject.Angle = DragObject.Angle + angle
                
        local k = DragObject.Angle / M_TWO_PI
        if k >= 1 then
            DragObject.Angle = DragObject.Angle - math.floor(k)*M_TWO_PI
        end
        DragObject.Entity:SetAngle(DragObject.Angle)
    end
end

function IsComplete()
    local delta = math.rad(5)
    for i = 1, OBJECTS_AMOUNT do
        if Objects[i].Angle > M_PI then
            if math.abs(Objects[i].Angle - M_TWO_PI) > delta then
                return false
            end
        else
            if math.abs(Objects[i].Angle) > delta then
                return false
            end
        end
    end
    
    FinalAnimation()
    return true
end

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

function FinalAnimation()
    PlaySound("28_circle_rotate")

    for i = 1, OBJECTS_AMOUNT do
        local obj = g_screen:GetObject('Circle_'..i)
        g_screen:IgnoreOnTestPoint(obj:GetName())
        
        local af = CreateAnimationEffector()
        af:AttachTo(obj)
        af:SetMode(_ANIM_FWD + _ANIM_NOLOOP)
        af:SetKey(_TRACK_A, 0, obj:GetAngle())
        if obj:GetAngle() > M_PI then
            af:SetKey(_TRACK_A, 0.4, M_TWO_PI)
        else
            af:SetKey(_TRACK_A, 0.4, 0.0)
        end
        af:SetKey(_TRACK_O, 0.7, 1.0)
        af:SetKey(_TRACK_O, 1.4, 0.0)
        af:Play()
    end
    
    do
        local af = CreateAnimationEffector()
        af:AttachTo(g_screen:GetObject('Circle_8'))
        af:SetMode(_ANIM_FWD + _ANIM_NOLOOP)
        af:SetKey(_TRACK_O, 0.7, 1.0)
        af:SetKey(_TRACK_O, 1.4, 0.0)
        af:Play()
    end
    
    do
        local af = CreateAnimationEffector()
        af:AttachTo(g_screen:GetObject('Shadow'))
        af:SetMode(_ANIM_FWD + _ANIM_NOLOOP)
        af:SetKey(_TRACK_O, 0.7, 1.0)
        af:SetKey(_TRACK_O, 1.4, 0.0)
        af:Play()
    end
    
    MiniGameCompleted(1.2)
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 OnSkipMiniGame()
	if game_state.minigame_complete then
        return
    end
    
    FinalAnimation()
end

function OnButtonPress(title)
    DefaultMinigameOnButtonPress(title)
end

function OnSetTool(tool)
end

function OnMinigameCompleted()
end

function OnLostFocus()
	DefaultMinigameOnLostFocus()
end

function OnGainFocus()
	DefaultMinigameOnGainFocus()
end

function ShowMiniGameHelp()
    CreateDialogMiniGameHelp('MINI_GAME_HELP_DRAGON')
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/28_Dragon/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/28_Dragon/Circle_1.png',0,0,662,662,1,0)
	anim:SetHotSpot(331,331)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Circle_1',anim,517,388,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_Dragon/Circle_2.png',0,0,568,568,1,0)
	anim:SetHotSpot(284,284)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Circle_2',anim,517,388,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_Dragon/Circle_3.png',0,0,478,478,1,0)
	anim:SetHotSpot(239,239)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Circle_3',anim,517,388,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_Dragon/Circle_4.png',0,0,396,396,1,0)
	anim:SetHotSpot(198,198)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Circle_4',anim,517,388,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_Dragon/Circle_5.png',0,0,314,314,1,0)
	anim:SetHotSpot(157,157)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Circle_5',anim,517,388,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_Dragon/Circle_6.png',0,0,258,258,1,0)
	anim:SetHotSpot(129,129)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Circle_6',anim,517,388,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_Dragon/Circle_7.png',0,0,194,194,1,0)
	anim:SetHotSpot(97,97)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Circle_7',anim,517,388,106,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_Dragon/Circle_8.png',0,0,122,122,1,0)
	anim:SetHotSpot(61,61)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFC0C0C0')
	screen:CreateObject('Circle_8',anim,517,388,107,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_Dragon/Shadow.png',0,0,674,674,1,0)
	anim:SetHotSpot(337,337)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Shadow',anim,517,389,96,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/28_Dragon/key.png',0,0,314,284,1,0)
	anim:SetHotSpot(157,142)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Key',anim,521,392,101,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
