--24
local coords = {
    {{103, 42}, {81, 1294}},
    {{162, 39}, {200, 1288}},
    {{233, 42}, {148, 1288}},
    {{319, 45}, {275, 1285}},
    {{352, 45}, {416, 1288}},
    {{417, 42}, {360, 1288}},
    {{445, 42}, {509, 1288}},
    {{518, 45}, {423, 1289}},
    {{589, 42}, {575, 1291}},
    {{680, 45}, {646, 1288}},
    {{760, 45}, {729, 1288}},
    {{864, 42}, {865, 1288}},
    {{935, 42}, {936, 1288}},
    {{1031, 42}, {1035, 1285}},
    {{1105, 42}, {1012, 1289}},
    {{1167, 45}, {1136, 1285}},
    {{1170, 45}, {1253, 1285}},
    {{1244, 45}, {1306, 1288}},
    {{1300, 45}, {1353, 1288}},
    {{1331, 45}, {1195, 1294}},
    {{1415, 39}, {1397, 1288}},
    {{1563, 45}, {1403, 1285}},
    {{1523, 45}, {1480, 1285}},
    {{1594, 45}, {1551, 1288}},
}

--x, y, width, height
local field_part = {
    {50, 50, 400, 450},
    {450, 50, 600, 450},
    {1050, 50, 500, 450},    
    {50, 500, 400, 650},
    {1050, 500, 450, 650},
}

local neighbors = {}
neighbors[1] = {2, 4}
neighbors[2] = {1, 3}
neighbors[3] = {2, 5}
neighbors[4] = {1}
neighbors[5] = {3}

------------------------------------------------------------------------------------------------------------------------------ 
function GameScreen:get_current_part_id(item_name)
    for i = 1, 5 do
        if(ItemGetX(item_name) >= field_part[i][1] and ItemGetX(item_name) <= field_part[i][1] + field_part[i][3] and
            ItemGetY(item_name) >= field_part[i][2] and ItemGetY(item_name) <= field_part[i][2] + field_part[i][4]) then
                return i;
        end
    end
    
    return 1
end
------------------------------------------------------------------------------------------------------------------------------ 
function GameScreen:get_next_part_id(part_id)

    local next_part_id = math.random(#neighbors[part_id] + 1)
    if(next_part_id == 1) then 
        next_part_id = part_id;
    else
        next_part_id = neighbors[part_id][next_part_id - 1];
    end
    
    return next_part_id;
end
------------------------------------------------------------------------------------------------------------------------------ 
function GameScreen:RestoreScreen()
    ------------------------------------------------------------------------------------------------------------------------------ 
    --Первая загрузка екрана
    ------------------------------------------------------------------------------------------------------------------------------ 
    if(GetState(CURRENT_SCREEN, "state") == "") then
    elseif(GetState(CURRENT_SCREEN, "state") == "finish")then
    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()
   
    HUDScreen:SetMiniGameMode() 
    
    self.green_firefly_table = {}
    
    self:RestoreScreen()
    
    for i = 1, 13 do
        self:add_firefly_red(i);
    end
    
    for i = 1, 20 do
        self:add_firefly_green(i)
    end
    
    self.active = true;
    
    MusicPlay({"sound/ambient/32_ambient.ogg", "sound/music/spaceship.ogg"} , 32)
    
    self:AddHiddenCrystal("crystal_32_part")
    
    DelayedExec(1.0, function()
        local end_tip = function()
            self:CreateCounter()
        end
        HUDScreen:ShowHintText({path = "graphics/screens/32_fireflies/location/Hint_32", x = 314, y = 1110, end_fun = end_tip, loop = true})
    end, 113)

end


------------------------------------------------------------------------------------------------------
function GameScreen:add_firefly_green(_index)
    self.green_firefly_table =  self.green_firefly_table or {}
    
    
    local part_id = math.random(5)
    local pos_x = field_part[part_id][1] + math.random(field_part[part_id][3])
    local pos_y = field_part[part_id][2] + math.random(field_part[part_id][4])
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "firefly_green_".._index, "graphics/screens/32_fireflies/location/firefly_green", pos_x, pos_y);
    if(math.random(100) < 50) then
        ItemSetDepthByOther("firefly_green_".._index, "reservoir")
    end
    
    self:firefly_green_anim("firefly_green_".._index, _index)
    
    table.insert(self.green_firefly_table, {name = "firefly_green_".._index, index = _index})
end
------------------------------------------------------------------------------------------------------
function GameScreen:firefly_green_anim(item_name, index)
    local dir = 1;
    if(math.random(100) < 50) then 
        dir = -1
    end
    local _time = 10 + math.random(10);
    
    local cur_id = self:get_current_part_id(item_name);
    local next_id = self:get_next_part_id(cur_id)
    local pos_x = field_part[next_id][1] + math.random(field_part[next_id][3])
    local pos_y = field_part[next_id][2] + math.random(field_part[next_id][4])
    
    local figure_eff2 = {
        {key=EFFECTOR_MODIFIER.KEY_MOVE_X, time = _time, end_value = pos_x, EFFECTOR_EASING.EASING_COS}, 
        {key=EFFECTOR_MODIFIER.KEY_MOVE_Y, time = _time, end_value = pos_y, EFFECTOR_EASING.EASING_COS}, 
        {key=EFFECTOR_MODIFIER.KEY_ROTATE, time = _time, end_value = M_PI * math.random(100) * dir},
    }   
    EffectorPlay(figure_eff2, item_name, false)
    
    DelayedExec(_time, function()
        self:firefly_green_anim(item_name, index)
    end, index + 200)
end
------------------------------------------------------------------------------------------------------
function GameScreen:add_firefly_red(index)
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "firefly_red_"..index, "graphics/screens/32_fireflies/location/firefly_red", -100, -100);
    ItemSetDepthByOther("firefly_red_"..index, "32_fireflies")
    self:firefly_red_anim("firefly_red_"..index, true)
end
------------------------------------------------------------------------------------------------------
function GameScreen:firefly_red_anim(item_name, need_start_time)
    local eff_num = math.random(24)
    local dir = math.random(2)
    local _time = 10 + math.random(10);
    local start_time = math.random(_time)
    
    local figure_eff = {}
    
    if(dir == 1) then
        figure_eff = {
                {key=EFFECTOR_MODIFIER.KEY_MOVE_X, time = _time, start_value = coords[eff_num][1][1] - 78, end_value = coords[eff_num][2][1] - 78}, 
                {key=EFFECTOR_MODIFIER.KEY_MOVE_Y, time = _time, start_value = coords[eff_num][1][2] - 78, end_value = coords[eff_num][2][2] - 78}, 
            }   
    else
        figure_eff = {
                {key=EFFECTOR_MODIFIER.KEY_MOVE_X, time = _time, start_value = coords[eff_num][2][1] - 78, end_value = coords[eff_num][1][1] - 78}, 
                {key=EFFECTOR_MODIFIER.KEY_MOVE_Y, time = _time, start_value = coords[eff_num][2][2] - 78, end_value = coords[eff_num][1][2] - 78}, 
            }   
    end
    
    EffectorPlay(figure_eff, item_name, false)
    
    if(need_start_time) then
        EffectorSetProgress(item_name, start_time / _time)
        _time = _time - start_time;
    end
    
    DelayedExec(_time, function()
        self:firefly_red_anim(item_name)
    end, 113)
end

------------------------------------------------------------------------------------------------------
function GameScreen:_get_green_firefly(item_name)
    for key, value in pairs(self.green_firefly_table) do
        if(value.name == item_name)then
            return value;
        end
    end
    return nil;
end
------------------------------------------------------------------------------------------------------
function GameScreen:IsMinigameCompleted()
    for key, value in pairs(self.green_firefly_table) do
        if(not value.active) then
            return false;
        end
    end
    
    return true;
end
------------------------------------------------------------------------------------------------------
function GameScreen:OnFinish()
    self.active = false
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "reservoir_cover_50", "graphics/screens/32_fireflies/location/reservoir_cover_50", 607, 441);
    
    for key, value in pairs(self.green_firefly_table) do
        EffectorRemove(value.name)
        StopRequests(value.index + 200)
        if(not value.active) then  
            local x_pos = 550 + math.random(340);
            local x_range = 186 - math.abs(705 - x_pos);
            if(x_range < 1) then x_range = 1 end
            local y_pos = 840 + (math.random(x_range) / 2)
            ItemSetPosition(value.name, x_pos, y_pos)
        end
        SoundPlay("sound/laser_over_sphere_32.ogg")
        ItemMoveTo("cover", 570, 0, 1.0)
        ItemMoveTo("cover_50", 570, 0, 1.0)
        local time2 = math.random(100) / 100 + 1.0
        DelayedExec(1.0, function()
            ItemMoveTo(value.name, 725, 450, time2)
        end, 113)
        DelayedExec(time2 + 1.0, function()
            ItemMoveTo(value.name, 725, -150, 0.5)
        end, 113)
     end
    
    DelayedExec(3.5, function()
        clApplication:LoadLocation("033_engine")
    end, 113)
end
------------------------------------------------------------------------------------------------------
function GameScreen:OnSkip()
    self:OnFinish();
end
------------------------------------------------------------------------------------------------------
function GameScreen:onObjectClick(item_name)   
    if(not self.active) then return end  
    
    if(self.drag_obj == nil) then
        local obj = self:_get_green_firefly(item_name); 
        if(obj) then
            EffectorRemove(obj.name)
            StopRequests(obj.index + 200)
            ItemSetType(obj.name, OBJ_TYPE.NO_ACTIVE)
            ItemAttachChild("drag_trigger_img", obj.name)
            DragObject.active = true
            self.drag_obj = obj;
        end
    else
        local obj = self:_get_green_firefly(item_name);
         if(item_name == "reservoir" or (obj and obj.active == true)) then
            ItemSetType(self.drag_obj.name, OBJ_TYPE.ACTIVE_ALL)
            ItemAttachChild(self.name, self.drag_obj.name)
            DragObject.active = false
            SetVisibleCursor(true)
            self.drag_obj.active = true;
            
            local x_pos = messenger_table.mouse_X - ItemGetWidth(self.drag_obj.name) / 2;
            if(x_pos < 550) then x_pos = 550; end
            if(x_pos > 870) then x_pos = 870; end
            local y_pos = 840 + math.random((185 - math.abs(705 - x_pos)) / 2)
            local time = 2.0 + math.random(300) / 100
            ItemMoveTo(self.drag_obj.name, x_pos, y_pos, time)
            local __index = self.drag_obj.index;
            local __name = self.drag_obj.name;
            
            DelayedExec(time + math.random(600) / 100 + 3.0, function()
                local time2 = math.random(900) / 100 + 5.0;
                ItemMoveTo(__name, 630 + math.random(190), 450, time2)
                DelayedExec(time2, function()
                    local obj = self:_get_green_firefly(__name); 
                    obj.active = false;
                    self:UpdateCounter()
                    self:firefly_green_anim(__name, __index)
                end, __index + 200) 
            end, __index + 200)
            
            self:UpdateCounter()
            
            if(self:IsMinigameCompleted()) then
                self:OnFinish()
            end
            self.drag_obj = nil
         else
            ItemSetType(self.drag_obj.name, OBJ_TYPE.ACTIVE_ALL)
            ItemAttachChild(self.name, self.drag_obj.name)
            DragObject.active = false
            self:firefly_green_anim(self.drag_obj.name, self.drag_obj.index)
            self.drag_obj = nil
            SetVisibleCursor(true)
         end
    end   
    
    
end
------------------------------------------------------------------------------------------------------
function GameScreen:Update()
    if(self.drag_obj) then
        ItemSetPosition(self.drag_obj.name, messenger_table.mouse_X - ItemGetWidth(self.drag_obj.name) / 2, messenger_table.mouse_Y - ItemGetHeight(self.drag_obj.name) / 2);
    end
end
------------------------------------------------------------------------------------------------------
function GameScreen:get_flise_count()
    local counter = 0
    for key, value in pairs(self.green_firefly_table) do
        if(not value.active) then
            counter = counter + 1
        end
    end
    
    return counter;
end
------------------------------------------------------------------------------------------------------
function GameScreen:CreateCounter()
    HUDScreen:CreateObj(OBJ_TYPE.NO_ACTIVE, "ho_panel", "graphics/interface/hud/counter", 18, 1104);
    local string = tostring(self:get_flise_count())
    HUDScreen:CreateLabel("counter_text", string, 35, 1117, 53, FONT_QUICKSAND)
    ItemSetPosition("counter_text", 55 - ItemGetWidth("counter_text") / 2, 1117)
    --SetLabelAlign("counter_text", TEXT_ALIGN.H_CENTER)
    ItemSetSize("counter_text", 65, 30)
    ItemSetColor("counter_text", 1.0, 1.0, 1.0, 1.0)
end
------------------------------------------------------------------------------------------------------
function GameScreen:UpdateCounter()
    local string = tostring(self:get_flise_count())
    SetLabelText("counter_text", string)
    ItemSetPosition("counter_text", 55 - ItemGetWidth("counter_text") / 2, 1117)
end
------------------------------------------------------------------------------------------------------
-------------------------------------------Mouse functions--------------------------------------------
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
function GameScreen:LoadScreen()
	--[[---Background---]]--
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "32_fireflies", "graphics/screens/32_fireflies/location/32_fireflies", 0, 0);
    
    self:CreateObj(OBJ_TYPE.TRIGGER, "reservoir", "graphics/screens/32_fireflies/location/reservoir", 469, 543);
    
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "column", "graphics/screens/32_fireflies/location/column", 444, 839);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "crystal_32_part", "graphics/screens/32_fireflies/location/crystal_32_part", 1004, 858);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "32_background_50", "graphics/screens/32_fireflies/location/32_background_50", 22, 0);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "cover", "graphics/screens/32_fireflies/location/cover", 570, -570);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "reservoir_cover", "graphics/screens/32_fireflies/location/reservoir_cover", 467, 441);
    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "cover_50", "graphics/screens/32_fireflies/location/cover_50", 570, -570);
    --self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "Hint_32", "graphics/screens/32_fireflies/location/Hint_32", 314, 1110);
    
    clScreenManager:add_screen("hud") 
end