function OnSceneLoad(screen)
    LoadData(screen)

    local pf = play_field
    local level = pf:GetLevelScreen()
    custom_cursors = {}
    locked_objects = {}
        
    global_screen = screen
    screen:IgnoreOnTestPoint('background_2')
    
    OBJECTS_AMOUNT = 10
    Objects = {}
    
    for i = 1, 5 do
        Objects[i] = {}
        Objects[i].Entity = screen:GetObject('card_'..i..'_1')
        Objects[i].Group = i
        Objects[i].Index = i * 2 - 1    -- place index
    end
    for i = 6, OBJECTS_AMOUNT do
        Objects[i] = {}
        Objects[i].Entity = screen:GetObject('card_'..(i - 5)..'_2')
        Objects[i].Group = i - 5
        Objects[i].Index = (i - 5) * 2
    end    
    for i = 1, OBJECTS_AMOUNT do
        Objects[i].Anim = Objects[i].Entity:GetAnimation()
        Objects[i].Anim:Stop()
        Objects[i].Anim:SetMode(HGEANIM_NOLOOP)
        Objects[i].Back = true
        Objects[i].SelfIndex = i
    end
    lock_cards = false
    
    ShuffleCards()
    
    face_index = 0 -- индекс карты лицом вверх. 0 - ни одной
end

function IsUsed(tbl, idx)
    for i = 1, table.getn(tbl) do
        if idx == tbl[i] then
            return true
        end
    end
    return false
end

function ShuffleCards()
    math.randomseed(os.clock())
    local used = {}
    for i = 1, OBJECTS_AMOUNT do
        if not IsUsed(used, i) then
            table.insert(used, i)
            
            local idx
            repeat
                idx = math.random(i + 1, OBJECTS_AMOUNT)
            until not IsUsed(used, idx)
            table.insert(used, idx)
            SwapCards(i, idx, 0.3)
        end
    end
end

function SwapCards(index1, index2, time)
    time = time or 0.5
    x1, y1 = Objects[index1].Entity:GetX(), Objects[index1].Entity:GetY()
    x2, y2 = Objects[index2].Entity:GetX(), Objects[index2].Entity:GetY()
    
    Objects[index1].Entity:SetZ(106)
    Objects[index2].Entity:SetZ(106)
    
    Objects[index1].Entity:Move(x2 - x1 , y2 - y1, time)
    Objects[index2].Entity:Move(x1 - x2, y1 - y2, time)
    
    TimerDoString("Objects["..index1.."].Entity:SetZ(105)", time, global_screen:GetTimer())
    TimerDoString("Objects["..index2.."].Entity:SetZ(105)", time, global_screen:GetTimer())
    
    Objects[index1].Index, Objects[index2].Index = Objects[index2].Index, Objects[index1].Index
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 LockCards(lock)
    lock_cards = lock
end

function GetObjectByArea(area)
    if not string.find(area:GetName(), 'area_') then
        return nil
    end
    local area_index = 0 + string.sub(area:GetName(), string.find(area:GetName(), '[%d]+$'))
    for i = 1, OBJECTS_AMOUNT do
        if Objects[i].Index == area_index then
            return Objects[i]
        end
    end
    return nil
end

function DeleteCards(index1, index2)
    Objects[index1].Entity:ChangeOpacity(1, 0, 0.5)
    Objects[index2].Entity:ChangeOpacity(1, 0, 0.5)
    TimerDoString('LockCards(false)', 0.5, global_screen:GetTimer())
end

function SetBack(card)
    card.Back = true
end

function RevertCards(index1, index2)
    Objects[index1].Anim:SetMode(HGEANIM_REV)
    Objects[index1].Anim:Play()
    TimerDoString('SetBack(Objects['..index1..'])', 1, global_screen:GetTimer())
    
    Objects[index2].Anim:SetMode(HGEANIM_REV)
    Objects[index2].Anim:Play()
    TimerDoString('SetBack(Objects['..index2..'])', 1, global_screen:GetTimer())
    
    TimerDoString('LockCards(false)', 1, global_screen:GetTimer())
end

function IsCompleted()
    for i = 1, OBJECTS_AMOUNT do
        if Objects[i].Back then
            return false
        end
    end
    return true
end

function OnSceneFree()
    FreeData()
end

function OnScreenShown()
end

function ShowMiniGameHelp()
    CreateDialogMiniGameHelp('MINI_GAME_HELP_CARDS')
end

function OnUpdate(dt)
end

function OnLose()
end

function OnExit()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnButtonMouseEnter(title)
end

function OnButtonMouseLeave(title)
end

function OnSkipMiniGame()
    if game_state.minigame_complete then
        return
    end
    
    LockCards(true)
    for i = 1, OBJECTS_AMOUNT do
        if Objects[i].Back then
            Objects[i].Anim:SetMode(HGEANIM_FWD)
            Objects[i].Anim:Play()
        end
        if Objects[i].Back or i == face_index then
            Objects[i].Back = false
            TimerDoString('Objects['..i..'].Entity:ChangeOpacity(1, 0, 0.8)', 2.0, global_screen:GetTimer())
        end
    end
    
    PlaySound('01_mg_card_rotate')
    
    MiniGameCompleted(1.0)
end

function OnObjectMouseDown(object)
    obj_name = object:GetName();
    local pf = play_field;
    local is_wrong_click = true;
    
    obj = GetObjectByArea(object)
    if obj and not lock_cards and not game_state.minigame_complete then
        if obj.Back then
            LockCards(true)
            obj.Anim:SetMode(HGEANIM_FWD)
            obj.Anim:Play()
            obj.Back = false
            PlaySound('01_mg_card_rotate')
            if face_index ~= 0 then
                local si1 = Objects[face_index].SelfIndex
                local si2 = obj.SelfIndex
                if Objects[face_index].Group == obj.Group then
                    TimerDoString('DeleteCards('..si1..', '..si2..')', 1, global_screen:GetTimer())
                else
                    TimerDoString('RevertCards('..si1..', '..si2..')', 1, global_screen:GetTimer())
                    TimerDoString('SwapCards('..si1..', '..si2..')', 1, global_screen:GetTimer())
                    TimerDoString('PlaySound("01_mg_card_wrong")', 1, global_screen:GetTimer())
                end
                face_index = 0
            else
                face_index = obj.SelfIndex
                TimerDoString('LockCards(false)', 1, global_screen:GetTimer())
            end
            if IsCompleted() then
                MiniGameCompleted(0.5)
            end
        end
    end
    ----------------------------------------------------------------------
    
    return is_wrong_click;
end

function OnObjectMouseUp(object)
end

function OnObjectFound(object)
end

function OnToolFound(tool)
end

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

function OnObjectReachPanel(object)
end

function OnPhotographing(phone_screen_center_pos)
	DefaultOnPhotographing(phone_screen_center_pos);
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

-- 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/1_Booth_Cards/background_2.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('background_2',anim,512,384,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/1_Booth_Cards/card_1.dds',0,0,332,400,25,25)
	anim:SetHotSpot(166,200)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('card_1_1',anim,120,320,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/1_Booth_Cards/card_1.dds',0,0,332,400,25,25)
	anim:SetHotSpot(166,200)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('card_1_2',anim,300,320,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/1_Booth_Cards/card_2.dds',0,0,332,400,25,25)
	anim:SetHotSpot(166,200)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('card_2_1',anim,480,320,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/1_Booth_Cards/card_2.dds',0,0,332,400,25,25)
	anim:SetHotSpot(166,200)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('card_2_2',anim,660,320,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/1_Booth_Cards/card_3.dds',0,0,332,400,25,25)
	anim:SetHotSpot(166,200)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('card_3_1',anim,840,320,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/1_Booth_Cards/card_3.dds',0,0,332,400,25,25)
	anim:SetHotSpot(166,200)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('card_3_2',anim,120,600,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/1_Booth_Cards/card_4.dds',0,0,332,400,25,25)
	anim:SetHotSpot(166,200)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('card_4_1',anim,300,600,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/1_Booth_Cards/card_4.dds',0,0,332,400,25,25)
	anim:SetHotSpot(166,200)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('card_4_2',anim,480,600,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/1_Booth_Cards/card_5.dds',0,0,332,400,25,25)
	anim:SetHotSpot(166,200)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('card_5_1',anim,660,600,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/1_Booth_Cards/card_5.dds',0,0,332,400,25,25)
	anim:SetHotSpot(166,200)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('card_5_2',anim,840,600,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_1',anim,155,235,110,0,19,27)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_2',anim,335,235,110,0,19,27)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_3',anim,515,235,110,0,19,27)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_4',anim,695,235,110,0,19,27)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_5',anim,875,235,110,0,19,27)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_6',anim,155,515,110,0,19,27)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_7',anim,335,515,110,0,19,27)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_8',anim,515,515,110,0,19,27)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_9',anim,695,515,110,0,19,27)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_10',anim,875,515,110,0,19,27)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
