
local RING_POSITION_AMOUNT = 12

function GameScreen:RestoreScreen()
    ------------------------------------------------------------------------------------------------------------------------------ 
    --Первая загрузка екрана
    ------------------------------------------------------------------------------------------------------------------------------ 
    if(GetState(CURRENT_SCREEN, "state") == "") then
        self:StartMG()
    elseif(GetState(CURRENT_SCREEN, "state") == "finish")then
        --clApplication:LoadLocation("032_fireflies")
    end
    
    for found_item in string.gmatch(GetState(CURRENT_SCREEN, "found_obj"), "[_%a%d]+") do
		ItemRemove(self.name, found_item)
		ItemRemove(self.name, found_item.."_shadow")
    end 
end
------------------------------------------------------------------------------------------------------
function GameScreen:InitScreen()
    self:LoadScreen()
   
    
    MusicPlay({"sound/ambient/33_ambient.ogg", "sound/music/spaceship.ogg"} , 33)
    
    self:AddHiddenCrystal("crystal_33_part")
    HUDScreen:SetMiniGameMode() 
    
    self:RestoreScreen()
    
    self:ring_set_pos()
end
------------------------------------------------------------------------------------------------------
function GameScreen:StartMG()
    self:init_ring();
    self:init_buttons();
    self:init_lines()
    --ItemSetPosition("figure_1", 780 - ItemGetWidth("figure_1") / 2 - 30, 285)
    self.active = true;
end
------------------------------------------------------------------------------------------------------
figure_eff = {
	    {key=EFFECTOR_MODIFIER.KEY_MOVE_X, time = 11.0, start_value = -210, end_value = 1810}, --2020
    }
------------------------------------------------------------------------------------------------------
function GameScreen:init_lines()
    local array = {"figure_3", "figure_7", "figure_5", "figure_11", "figure_2", "figure_9",
                    "figure_6", "figure_10", "figure_1", "figure_12", "figure_4", "figure_8"}
                
    for key, value in pairs(array) do
        EffectorPlay(figure_eff, value, true)
        EffectorSetProgress(value, 1 / 12 * (key - 1) + 1 / 20)
    end
end
------------------------------------------------------------------------------------------------------
function GameScreen:init_buttons()
    for i = 1, 12 do
        self:__add_button("button_"..i, i);
    end
end
------------------------------------------------------------------------------------------------------
function GameScreen:__add_button(button_name, id)
    self.buttons = self.buttons or {}
    
    local button = {}
    button.name = button_name;
    ItemSetOpacity(button_name, 0.01)
    button.id = id;
    button.action = function()
        PlaySfx("sound/plate_push.ogg")
        
        self.active = false
        ItemSetOpacity(button_name, 1.0)
        DelayedExec(0.1, function()
            ItemSetOpacity(button_name, 0.01)
            self:figure_kick(button.id)
        end, 113)
        ItemMove("hammer", 0, 50, 0.2)
        DelayedExec(0.4, function()
           ItemMove("hammer", 0, -50, 0.8) 
        end, 113)
        DelayedExec(1.2, function()
            self.active = true
        end, 113)
    end
    table.insert(self.buttons, button)
end
------------------------------------------------------------------------------------------------------ 780
function GameScreen:figure_kick(id)
    PlaySfx("sound/hammer_33.ogg")
    
    for i = 1, 12 do
        local pos = ItemGetX("figure_"..i) + ItemGetWidth("figure_"..i) / 2;
        --[[if((pos > 680 and pos < 740) or (pos > 800 and pos < 840)) then
            ItemSetOpacity("figure_"..i, 0.0)
            self.counter = self.counter or 1;
            self.counter = self.counter + 1
            self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure"..self.counter, "graphics/screens/33_engine/location/figure_"..i, ItemGetX("figure_"..i), ItemGetY("figure_"..i));
            if((pos > 680 and pos < 740)) then
                ItemMoveTo("figure"..self.counter, 800 - math.random(1000), - 500, 2.0)
            else
                ItemMoveTo("figure"..self.counter, 800 + math.random(1000), - 500, 2.0)
            end
            ItemRotate("figure"..self.counter, M_PI * math.random(8) - 4, 2.0)
            local name = "figure"..self.counter;
            DelayedExec(5.5, function()
                ItemRemove(self.name, name)
                ItemSetOpacity("figure_"..i, 1.0)
            end, 113)
        else--]]
        if(pos > 680 and pos < 840) then
            if(i == id and (math.abs(ItemGetAngle("figure_"..i.."_affix")) < 0.01 or math.abs(ItemGetAngle("figure_"..i.."_affix") - M_PI * 2) < 0.01)) then
                ItemSetOpacity("figure_"..i, 0.0)
                self.counter = self.counter or 1;
                self.counter = self.counter + 1
                self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure"..self.counter, "graphics/screens/33_engine/location/figure_"..i, ItemGetX("figure_"..i), ItemGetY("figure_"..i));
                ItemMoveTo("figure"..self.counter, ItemGetX("figure_"..i.."_affix"), ItemGetY("figure_"..i.."_affix"), 0.4)
                local __name = "figure"..self.counter;
                local angle_id = tonumber(GetState(CURRENT_SCREEN, "ring_cur_pos")) or 0
                SetState(CURRENT_SCREEN, "ring_angle_"..angle_id, "1")
                DelayedExec(0.5, function()
                    PlaySfx("sound/figure_insert.ogg")
                    SetState(CURRENT_SCREEN, "figure_"..i, "1")
                    ItemSetOpacity("figure_"..i.."_affix", 1.0)
                    ItemSetOpacity(__name, 0.0)
                    ItemRemove(self.name, __name)
                    
                end, 113)
                DelayedExec(1.0, function()
                    if(self:IsMinigameCompleted()) then
                        self:OnFinish();
                    else
                        self:rotate_ring()
                    end
                end, 113)
                DelayedExec(5.5, function()
                    ItemSetOpacity("figure_"..i, 1.0)
                end, 113)
            else
                PlaySfx("sound/figure_wrong.ogg")
                ItemSetOpacity("figure_"..i, 0.0)
                self.counter = self.counter or 1;
                self.counter = self.counter + 1
                self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure"..self.counter, "graphics/screens/33_engine/location/figure_"..i, ItemGetX("figure_"..i), ItemGetY("figure_"..i));
                 ItemMoveTo("figure"..self.counter, 800 - ItemGetWidth("figure_"..i) / 2, 2500, 2.0)
                ItemRotate("figure"..self.counter, M_PI * math.random(8) - 4, 2.0)
                local name = "figure"..self.counter;
                DelayedExec(5.5, function()
                    ItemRemove(self.name, "figure"..self.counter)
                    ItemSetOpacity("figure_"..i, 1.0)
                end, 113)
            end
        end
    end
end
------------------------------------------------------------------------------------------------------
function GameScreen:ring_set_pos()
    if(GetState(CURRENT_SCREEN, "ring_cur_pos") ~= "")then
        local ring_pos = tonumber(GetState(CURRENT_SCREEN, "ring_cur_pos"));
        ItemSetAngle("circle", -ring_pos * M_PI / 6)
        
        ItemSetAngle("figure_1_affix", M_PI / 6 - M_PI - ring_pos * M_PI / 6)
        ItemSetAngle("figure_2_affix", M_PI - M_PI / 6 - ring_pos * M_PI / 6)
        ItemSetAngle("figure_3_affix", M_PI - M_PI / 3 - ring_pos * M_PI / 6)  
        ItemSetAngle("figure_4_affix", M_PI / 6 - ring_pos * M_PI / 6)
        ItemSetAngle("figure_5_affix", M_PI / 3 - ring_pos * M_PI / 6)
        ItemSetAngle("figure_6_affix", -M_PI / 2 - ring_pos * M_PI / 6)
        ItemSetAngle("figure_6_affix", -M_PI / 2 - ring_pos * M_PI / 6)
        ItemSetAngle("figure_7_affix", 0 - ring_pos * M_PI / 6)
        ItemSetAngle("figure_8_affix", -M_PI / 6 - ring_pos * M_PI / 6)
        ItemSetAngle("figure_9_affix", M_PI / 3 + M_PI - ring_pos * M_PI / 6)
        ItemSetAngle("figure_10_affix", M_PI - ring_pos * M_PI / 6)
        ItemSetAngle("figure_11_affix", M_PI / 2 - ring_pos * M_PI / 6)
        ItemSetAngle("figure_12_affix", -M_PI / 3 - ring_pos * M_PI / 6)
        
        self:__correct_angle("circle")
        for i = 1, 12 do
            self:__correct_angle("figure_"..i.."_affix")
        end

    end
end
------------------------------------------------------------------------------------------------------
function GameScreen:rotate_ring()
    PlaySfx("sound/circle_turn.ogg")
    
    local rand = 0;
    repeat
        rand = math.random(11)
    until GetState(CURRENT_SCREEN, "ring_angle_"..rand) ~= "1"
    
    local new_angle = rand
    if(GetState(CURRENT_SCREEN, "ring_cur_pos") ~= "") then
        new_angle = rand - tonumber(GetState(CURRENT_SCREEN, "ring_cur_pos"))
        if(new_angle < 0) then
            --new_angle = 12 - new_angle;
        end
    end
    
    SetState(CURRENT_SCREEN, "ring_cur_pos", ""..rand)
    
    local angle = new_angle * M_PI / 6;
    
    local ring_eff = {
	    {key=EFFECTOR_MODIFIER.KEY_ROTATE, time = 2.0, delta_value = -M_PI * 2 - angle},
    } 
    
    DelayedExec(2.01, function()
        self:__correct_angle("circle")
    end, 113)
    
    EffectorPlay(ring_eff, "circle", false)
    for i = 1, 12 do
        EffectorPlay(ring_eff, "figure_"..i.."_affix", false)
        DelayedExec(2.01, function()
            self:__correct_angle("figure_"..i.."_affix")
        end, 113)
    end
end
------------------------------------------------------------------------------------------------------
function GameScreen:__correct_angle(item_name)
    repeat
        ItemSetAngle(item_name, ItemGetAngle(item_name) + M_PI * 2)
    until ItemGetAngle(item_name) >= 0
end   
------------------------------------------------------------------------------------------------------
function GameScreen:__get_button_by_name(button_name)
    for key, value in pairs(self.buttons) do
        if(value.name == button_name) then
            return value;
        end
    end
    
    return nil;
end
------------------------------------------------------------------------------------------------------
function GameScreen:IsMinigameCompleted()
    for i = 1, 11 do
        if(GetState(CURRENT_SCREEN, "ring_angle_"..i) ~= "1") then
            return false;
        end
    end
    
    return true;
end

------------------------------------------------------------------------------------------------------
function GameScreen:OnSkip()
    DelayedExec(1.0, function()
        self:OnFinish();
    end, 113)
end
------------------------------------------------------------------------------------------------------
function GameScreen:init_ring()
    --EffectorPlay(ring_eff, "circle", true)

    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_10_affix", "graphics/screens/33_engine/location/figure_10_affix",762, 343);
    ItemSetAngle("figure_10_affix", M_PI)
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_7_affix", "graphics/screens/33_engine/location/figure_07_affix",738, 346);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_6_affix", "graphics/screens/33_engine/location/figure_06_affix", 739, 342);
    ItemSetAngle("figure_6_affix", -M_PI / 2)
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_11_affix", "graphics/screens/33_engine/location/figure_11_affix",715, 348);
    ItemSetAngle("figure_11_affix", M_PI / 2)
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_12_affix", "graphics/screens/33_engine/location/figure_12_affix",747, 346);
    ItemSetAngle("figure_12_affix", -M_PI / 3)
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_9_affix", "graphics/screens/33_engine/location/figure_09_affix", 741, 346);
    ItemSetAngle("figure_9_affix", M_PI / 3 + M_PI)
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_5_affix", "graphics/screens/33_engine/location/figure_05_affix",730, 350);
    ItemSetAngle("figure_5_affix", M_PI / 3)
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_8_affix", "graphics/screens/33_engine/location/figure_08_affix",739, 346);
    ItemSetAngle("figure_8_affix", -M_PI / 6)
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_4_affix", "graphics/screens/33_engine/location/figure_04_affix",738, 347);
    ItemSetAngle("figure_4_affix", M_PI / 6)
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_2_affix", "graphics/screens/33_engine/location/figure_02_affix",736, 348);
    ItemSetAngle("figure_2_affix", M_PI - M_PI / 6)
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_3_affix", "graphics/screens/33_engine/location/figure_03_affix",728, 350);
    ItemSetAngle("figure_3_affix", M_PI - M_PI / 3)  
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_1_affix", "graphics/screens/33_engine/location/figure_01_affix",745, 344);
    ItemSetAngle("figure_1_affix", M_PI / 6 - M_PI)
    ---------------------
     
    for i = 1, 12 do
         --EffectorPlay(ring_eff, "figure_"..i.."_affix", true)
         if(GetState(CURRENT_SCREEN, "figure_"..i) == "1") then
            ItemSetOpacity("figure_"..i.."_affix", 1.0)
            --ItemRemove(self.name, "figure_"..i)
         else
            ItemSetOpacity("figure_"..i.."_affix", 0.0)
         end
    end
    ItemSetHotSpot("figure_7_affix", 54, 320)
    ItemSetHotSpot("figure_10_affix", 35, 324)
    ItemSetHotSpot("figure_6_affix", 54, 325)
    ItemSetHotSpot("figure_11_affix", 81, 321)
    ItemSetHotSpot("figure_12_affix", 47, 322)
    ItemSetHotSpot("figure_9_affix", 55, 322)
    ItemSetHotSpot("figure_5_affix", 65, 319)
    ItemSetHotSpot("figure_8_affix", 56, 322)
    ItemSetHotSpot("figure_4_affix", 58, 321)
    ItemSetHotSpot("figure_2_affix", 59, 319)
    ItemSetHotSpot("figure_3_affix", 68, 318)
    ItemSetHotSpot("figure_1_affix", 50, 322)
    
    self.active = true;
end
------------------------------------------------------------------------------------------------------
function GameScreen:onObjectClick(item_name)   
    if(not self.active) then return end  

    local button = self:__get_button_by_name(item_name)  
    if(button) then
        button:action()
    end
end
------------------------------------------------------------------------------------------------------
function GameScreen:OnFinish()
    self.active = false;
    SetState(CURRENT_SCREEN, "state", "finish");
    
    for i = 1, 12 do
        if(GetState(CURRENT_SCREEN, "ring_angle_"..i) ~= "1") then
            ItemSetOpacity("figure_"..i.."_affix", 1.0)
        end
    end
    
    DelayedExec(1.0, function()
        clApplication:LoadLocation("Intro_06a")
    end, 113)    
end
------------------------------------------------------------------------------------------------------
-------------------------------------------Mouse functions--------------------------------------------
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
function GameScreen:LoadScreen()
	--[[---Background---]]--
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "33_engine", "graphics/screens/33_engine/location/33_engine",0, 0);
    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "circle", "graphics/screens/33_engine/location/circle",473, 347);

    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_12", "graphics/screens/33_engine/location/figure_12",1279, 176);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_11", "graphics/screens/33_engine/location/figure_11",1161, 176);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_10", "graphics/screens/33_engine/location/figure_10",1081, 174);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_9", "graphics/screens/33_engine/location/figure_9",981, 177);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_8", "graphics/screens/33_engine/location/figure_8",869, 176);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_7", "graphics/screens/33_engine/location/figure_7",778, 176);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_6", "graphics/screens/33_engine/location/figure_6",685, 175);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_5", "graphics/screens/33_engine/location/figure_5",593, 177);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_4", "graphics/screens/33_engine/location/figure_4",515, 177);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_3", "graphics/screens/33_engine/location/figure_3",441, 175);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_2", "graphics/screens/33_engine/location/figure_2",352, 178);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figure_1", "graphics/screens/33_engine/location/figure_1",256, 174);    

    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "cover_left", "graphics/screens/33_engine/location/cover_left",0, 174);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "cover_right", "graphics/screens/33_engine/location/cover_right",1293, 174);
    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "hammer", "graphics/screens/33_engine/location/hammer",717, 0);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "hammer_cover", "graphics/screens/33_engine/location/hammer_cover",666, 0);
    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_12", "graphics/screens/33_engine/location/button_12",1218, 1019);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_11", "graphics/screens/33_engine/location/button_11",1267, 878);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_10", "graphics/screens/33_engine/location/button_10",1365, 735);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_9", "graphics/screens/33_engine/location/button_09",1409, 539);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_8", "graphics/screens/33_engine/location/button_08",1432, 386);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_7", "graphics/screens/33_engine/location/button_07",1476, 237);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_6", "graphics/screens/33_engine/location/button_06",199, 908);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_5", "graphics/screens/33_engine/location/button_05",162, 804);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_4", "graphics/screens/33_engine/location/button_04",108, 645);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_3", "graphics/screens/33_engine/location/button_03",50, 516);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_2", "graphics/screens/33_engine/location/button_02",39, 354);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "button_1", "graphics/screens/33_engine/location/button_01",29, 223);
    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "crystal_33_part", "graphics/screens/33_engine/location/crystal_33_part",1110, 1059);


    clScreenManager:add_screen("hud") 
end