-- Minigame 19_repeat

function OnSceneLoad(screen)
    LoadData(screen)
    
    local pf = play_field
    local level = pf:GetLevelScreen()
    g_screen = screen
    g_timer = screen:GetTimer()
    
    BUTTONS_AMOUNT = 6
    STAGES = 3
    CurStage = 1
    ButtonsNum = 4
    ButtonsToFind = {}
    Buttons = {}
    Complete = false
    
    -- variables for delays
    rotate_time = 2.4
    start_rotate_time = 0.6
    highlight_time = 0.7
    highlight_delay = 0.3
    
    screen:IgnoreOnTestPoint('Background')
    screen:GetObject("Buttons_red"):Hide()
    screen:GetObject("Buttons_red_blink"):Hide()
    screen:IgnoreOnTestPoint('Buttons_red')
    screen:IgnoreOnTestPoint('Buttons_red_blink')
    
    AttachInPlace(g_screen:GetObject("Circle"), g_screen:GetObject("Middle"))
    for i = 1, BUTTONS_AMOUNT/2 do
        g_screen:GetObject("Key_"..i):SetOpacity(0.0)
    end
    
    CreateButtons()
    StopButtons(true)
    TimerDo(g_timer, 3*start_rotate_time, StartStage, 1)
end

function CreateButtons()
    for i = 1, BUTTONS_AMOUNT do
        local up = CreateAnimation('data/Texture/Minigames/19_repeat/Button_'..i..'.png',0,0,133,131,1,0)
    	up:SetHotSpot(66,66);
        local over = CreateAnimation('data/Texture/Minigames/19_repeat/Button_'..i..'.png',0,0,133,131,1,0)
        over:SetHotSpot(66,66);
        local down = CreateAnimation('data/Texture/Minigames/19_repeat/Button_'..i..'.png',0,262,133,131,1,0)
        down:SetHotSpot(66,66);
    	Buttons[i] = CreateButton(up, over, down, 'GameButton_'..i, g_screen)
	    Buttons[i]:AttachTo(g_screen)
    	Buttons[i]:SetX(g_screen:GetObject("B_"..i):GetX())
	    Buttons[i]:SetY(g_screen:GetObject("B_"..i):GetY())
    	Buttons[i]:SetZ(105);
    	Buttons[i].highlited = g_screen:GetObject("B_"..i)
    	Buttons[i].highlited:SetZ(50.0)
    	Buttons[i].Name = 'GameButton_'..i
    end
end

function AttachInPlace(object, parentObject)
	local scrX, scrY, scrZ = object:GetScrX(), object:GetScrY(), object:GetScrPosZ()
	local z = object:GetScrPosZ() - parentObject:GetScrPosZ()
	object:SetZ(z)
	object:AttachTo(parentObject)
	object:SetScrXY(scrX, scrY)
end

function StopButtons(stop)
    if Complete == true then stop = true end 
    
    for i = 1, BUTTONS_AMOUNT do
        if stop == true then
            Buttons[i]:Stop()
        else
            Buttons[i]:Go()
        end
    end
end

function PressButtons(pressed)
    for i = 1, BUTTONS_AMOUNT do
        if pressed == true then
            Buttons[i]:SetPressed(true)
        else
            Buttons[i]:SetPressed(false)
        end
    end
end

function StartStage(level)
    CurStage = level
    if CurStage > STAGES then
        StopButtons(true)   
        Complete = true
        TimerDo(g_timer, start_rotate_time, PressButtons, false)
        FinishMinigame()
        return
    end
    -- Randomize
    ButtonsNum = CurStage + 3
    for i = 1, BUTTONS_AMOUNT do
        ButtonsToFind[i] = Buttons[i]
    end
    local rand_1, rand_2, obj
    for i = 1, 20 do
        rand_1 = math.random(1, BUTTONS_AMOUNT/2)
        rand_2 = math.random(BUTTONS_AMOUNT/2, BUTTONS_AMOUNT)
        obj = ButtonsToFind[rand_1]
        ButtonsToFind[rand_1] = ButtonsToFind[rand_2]
        ButtonsToFind[rand_2] = obj
    end
    for i = BUTTONS_AMOUNT, ButtonsNum+1, -1 do
        table.remove(ButtonsToFind, 1)
    end
    --
    HighlightAll()
end

function HighlightAll()
    StopButtons(true)
    PressButtons(false)
    for i = 0, ButtonsNum-1 do
        TimerDo(g_timer, (highlight_time+highlight_delay)*i, Highlight, i+1, true)
        TimerDo(g_timer, (highlight_time+highlight_delay)*i+highlight_time, Highlight, i+1, false)
    end
    TimerDoString("StopButtons(false)", (highlight_time+highlight_delay)*(ButtonsNum-1)+highlight_time, g_timer)
end

function Highlight(index, on_)
    if Complete == true then on_ = false end -- if complete minigame off all highligting
    
    if on_ == true then
        ButtonsToFind[index]:SetZ(50.0)
        ButtonsToFind[index].highlited:SetZ(105.0)
        PlaySound("19_mg_highlight")
    else
        ButtonsToFind[index]:SetZ(105.0)
        ButtonsToFind[index].highlited:SetZ(50.0)
    end
end

function RotateAnim()
    local af = CreateAnimationEffector()
    af:AttachTo(g_screen:GetObject("Middle"))
    af:SetMode(_ANIM_FWD + _ANIM_NOLOOP)
    af:SetKey(_TRACK_A, start_rotate_time, g_screen:GetObject("Middle"):GetAngle())
    af:SetKey(_TRACK_A, rotate_time+start_rotate_time, g_screen:GetObject("Middle"):GetAngle()+M_TWO_PI/3.0)    
    af:Play()
    
    TimerDoString("PlaySound('19_mg_circle_rotate')", start_rotate_time, g_timer)
    
    for i = 1, CurStage do
        local ef = CreateAnimationEffector()
        ef:AttachTo(g_screen:GetObject("Key_"..i))
        ef:SetMode(_ANIM_FWD + _ANIM_NOLOOP)
        ef:SetKey(_TRACK_O, 0.0, g_screen:GetObject("Key_"..i):GetOpacity())
        ef:SetKey(_TRACK_O, start_rotate_time, 0.0)
        ef:SetKey(_TRACK_O, rotate_time+start_rotate_time, 0.0)
        ef:SetKey(_TRACK_O, rotate_time+2*start_rotate_time, 1.0)    
        ef:Play()
    end
end

function FinishMinigame()
    for i = 1, BUTTONS_AMOUNT/2 do
        local ef = CreateAnimationEffector()
        ef:AttachTo(g_screen:GetObject("Key_"..i))
        ef:SetMode(_ANIM_FWD + _ANIM_NOLOOP)
        ef:SetKey(_TRACK_O, 0.0, 1.0)
        ef:SetKey(_TRACK_O, 0.5*start_rotate_time, 0.0)
        ef:SetKey(_TRACK_O, 1.0*start_rotate_time, 1.0)
        ef:SetKey(_TRACK_O, 1.5*start_rotate_time, 0.0)    
        ef:SetKey(_TRACK_O, 2.0*start_rotate_time, 1.0)    
        ef:Play()
    end
    PlaySound("05_electric_light")
    MiniGameCompleted(2.0*start_rotate_time)
end

function OnSceneFree()
    FreeData()
end

function OnScreenShown()
end

function ShowMiniGameHelp()
    CreateDialogMiniGameHelp('MINI_GAME_HELP_REPEAT')
end

function OnMinigameCompleted()
end

function OnUpdate(dt)
    --DefaultOnUpdate(dt)
end

function OnLose()
end

function OnExit()
end

function OnButtonMouseEnter(title)
end

function OnButtonMouseLeave(title)
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnButtonPress(title)
    -- Game buttons
    if string.find(title, 'GameButton_') then
        local index
        -- Find button
        for i = 1, BUTTONS_AMOUNT do
            if title == Buttons[i].Name then
                index = i
                break
            end
        end
        PlaySound("19_mg_click")
        -- Is finded button on top of list?
        if ButtonsToFind[1].Name == Buttons[index].Name then
            Buttons[index]:SetPressed(true)
            Buttons[index]:Stop()
            table.remove(ButtonsToFind, 1)
        -- От жалко пацана (wrong button was pressed)
        else
            StopButtons(true)
            local time = 1.6
            local start_time = 0.2
            TimerDoString('g_screen:GetObject("Buttons_red"):Show()', start_time, g_timer)
            TimerDoString('g_screen:GetObject("Buttons_red_blink"):Show()', start_time, g_timer)
            
            local anim = CreateAnimationEffector()
            anim:AttachTo(g_screen:GetObject("Buttons_red_blink"))
            anim:SetMode(_ANIM_FWD + _ANIM_NOLOOP)
    
            anim:SetKey(_TRACK_O, 0 + start_time, 1)
            anim:SetKey(_TRACK_O, 0.25*time + start_time, 0)
            anim:SetKey(_TRACK_O, 0.5*time + start_time, 1)
            anim:SetKey(_TRACK_O, 0.75*time + start_time, 0)
            anim:Play()
            
            TimerDoString('PlaySound("19_mg_wrong_click")', 1.2*start_time, g_timer)
    
            TimerDoString('PressButtons(false)', time + 0.1 + start_time, g_timer)
            TimerDoString('StartStage(CurStage)', time + 1.2 + start_time, g_timer)
            TimerDoString('g_screen:GetObject("Buttons_red"):Hide()', time + 0.1 + start_time, g_timer)
            TimerDoString('g_screen:GetObject("Buttons_red_blink"):Hide()', time + 0.1 + start_time, g_timer)
            
            --DefaultMinigameOnButtonPress(title)
            return
        end
        -- Next stage?
        if table.getn(ButtonsToFind) == 0 then
            StopButtons(true)
            RotateAnim()
            TimerDoString('PressButtons(false)', start_rotate_time, g_timer)
            TimerDoString('StartStage(CurStage + 1)', 3.6, g_timer)
            
            --DefaultMinigameOnButtonPress(title)
            return
        end
    end
    --
    DefaultMinigameOnButtonPress(title)
end

function OnObjectMouseDown(object)
    obj_name = object:GetName()
    local pf = play_field
--  local is_wrong_click = DefaultOnObjectMouseDown(object)
    
    return is_wrong_click
end

function OnObjectMouseUp(object)
end

function OnObjectRightMouseDown(object)
end

function OnObjectRightMouseUp(object)
end

function OnObjectFound(object)
    DefaultOnObjectFound(object)
end

function OnToolFound(tool)
end

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

function OnSkipMiniGame()
    if game_state.minigame_complete then
        return
    end
    
    StartStage(4)
end

function OnPhotographing()
	DefaultOnPhotographing()
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/19_repeat/Background.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Background',anim,514,385,95,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_repeat/Circle.png',0,0,787,758,1,0)
	anim:SetHotSpot(393,379)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Circle',anim,549,387,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_repeat/Key_1.png',0,0,97,280,1,0)
	anim:SetHotSpot(48,140)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Key_1',anim,544,149,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_repeat/Key_2.png',0,0,273,186,1,0)
	anim:SetHotSpot(136,93)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Key_2',anim,755,499,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_repeat/Key_3.png',0,0,257,185,1,0)
	anim:SetHotSpot(128,92)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Key_3',anim,344,497,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_repeat/Middle.png',0,0,217,218,1,0)
	anim:SetHotSpot(110,119)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Middle',anim,549,380,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_repeat/Button_1.png',0,131,133,131,1,0)
	anim:SetHotSpot(66,65)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('B_1',anim,441,180,90,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_repeat/Button_2.png',0,131,133,131,1,0)
	anim:SetHotSpot(66,65)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('B_2',anim,651,177,90,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_repeat/Button_3.png',0,131,133,131,1,0)
	anim:SetHotSpot(66,65)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('B_3',anim,778,385,90,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_repeat/Button_4.png',0,131,133,131,1,0)
	anim:SetHotSpot(66,65)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('B_4',anim,669,573,90,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_repeat/Button_5.png',0,131,133,131,1,0)
	anim:SetHotSpot(66,65)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('B_5',anim,422,572,90,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_repeat/Button_6.png',0,131,133,131,1,0)
	anim:SetHotSpot(66,65)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('B_6',anim,319,389,90,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_repeat/Buttons_red.png',0,0,622,551,1,0)
	anim:SetHotSpot(311,275)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Buttons_red',anim,550,374,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_repeat/Buttons_red.png',0,0,622,551,1,0)
	anim:SetHotSpot(311,275)
	anim:SetBlendMode(0)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Buttons_red_blink',anim,550,374,106,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
