
function rotation(item_name)
    local _time = 2.0 + math.random(500) / 100;
    local koef = 1
    if(math.random(100) < 50) then koef = -1 end
    
    local bactaria_anim = {
            {key=EFFECTOR_MODIFIER.KEY_ROTATE, start_time = 0.0, time = _time * 3, start_value = 0, end_value = 6.28 * koef},
    }
    
    EffectorPlay(bactaria_anim, item_name, true)
end

function GameScreen:InitScreen()
    self:LoadScreen()
    HUDScreen:SetMiniGameMode()
    
    self:init_life_zone();
    
    for i = 1, 4 do
        self:bacteria_add_to_array("red")
        self:bacteria_add_to_array("blue")
        self:bacteria_add_to_array("yellow")
        self:bacteria_add_to_array("green")
    end
    
    for i = 1, 6 do
        self:bacteria_add_to_array("round")
        self:bacteria_add_to_array("orange")
        self:bacteria_add_to_array("crimson")
        self:bacteria_add_to_array("creme")
        self:bacteria_add_to_array("brown")
        self:bacteria_add_to_array("black")
    end
    
    for key, value in pairs(self.names_array) do
        self:add_bacteria(value, key)
    end
    
    HUDScreen:ShowHintText({path = "graphics/minigames/08_microscope/hint_08b", x = 322, y = 1128, loop = true})
    
    self:CreateCounter()
    MusicPlay({"sound/ambient/08_ambient.ogg", "sound/music/basement.ogg"}, 8) 
end

function GameScreen:bacteria_add_to_array(name)
    self.names_array = self.names_array or {}
    table.insert(self.names_array,  math.random(#self.names_array + 1), name)
end

function GameScreen:OnSkip()
    self:OnFinish2();    
end

function GameScreen:init_life_zone()
    self.zone = {}
    self.zone.pos = {x = ItemGetX("bacteria_zone") + ItemGetWidth("bacteria_zone") / 2, y = ItemGetY("bacteria_zone") + ItemGetHeight("bacteria_zone") / 2};
    self.zone.radius = ItemGetWidth("bacteria_zone") / 2;
end

function GameScreen:add_bacteria(item_name, index)
    self.bacteria_table = self.bacteria_table or {}
    if(item_name == "red" or item_name == "blue" or item_name == "yellow" or item_name == "green") then
        self:CreateObj(OBJ_TYPE.NO_ACTIVE, item_name.."_"..index, "graphics/minigames/08_microscope/bacteria_"..item_name, 0, 0);
    else
        self:CreateObj(OBJ_TYPE.NO_ACTIVE, item_name.."_"..index, "graphics/minigames/08_microscope/bacteria_"..item_name, 0, 0);
    end	
    local bacteria = {}
    bacteria.name = item_name.."_"..index;
    bacteria.center = {}
    bacteria.color = item_name;
    bacteria.radius = ItemGetWidth(bacteria.name) / 2;
    if(ItemGetHeight(bacteria.name) / 2 > bacteria.radius) then
        bacteria.radius = ItemGetHeight(bacteria.name) / 2
    end
    bacteria.max_delta = self.zone.radius - bacteria.radius;
 
    local pos_x = math.random(-bacteria.max_delta, bacteria.max_delta);
    local pos_y = math.random(-bacteria.max_delta, bacteria.max_delta);
    local max_dy = math.sqrt(bacteria.max_delta * bacteria.max_delta - pos_x * pos_x)
    local pos_y = math.random(- max_dy, max_dy);
    ItemSetPosition(bacteria.name, pos_x - ItemGetWidth(bacteria.name) / 2 + self.zone.pos.x,  pos_y - ItemGetHeight(bacteria.name) / 2 + self.zone.pos.y);
    
    rotation(bacteria.name)
    
    self:bacteria_update_speed(bacteria)
    self:bacteria_move(bacteria)
    table.insert(self.bacteria_table, bacteria)
end

function GameScreen:bacteria_update_speed(bacteria)
    --[[local dx = ItemGetX(bacteria.name) + ItemGetWidth(bacteria.name) / 2 - self.zone.pos.x;
    local dy = ItemGetY(bacteria.name) + ItemGetHeight(bacteria.name) / 2 - self.zone.pos.y;
    
    local speed_x = 0;
    local speed_y = 0;
    repeat
        speed_x = -0.5;
        if(math.random(100) < 50) then
            speed_x =  0.5;
        end
        speed_y =  0.5;
        if(math.random(100) < 50) then
            speed_y =  - 0.5;
        end
    until  math.sqrt((dx + speed_x) * (dx + speed_x) + (dy + speed_y) * (dy + speed_y)) < bacteria.max_delta
    
    bacteria.speed = {x = speed_x, y = speed_y}--]]
end

function GameScreen:bacteria_move(bacteria)
    local dx = ItemGetX(bacteria.name) + ItemGetWidth(bacteria.name) / 2 - self.zone.pos.x;
    local dy = ItemGetY(bacteria.name) + ItemGetHeight(bacteria.name) / 2 - self.zone.pos.y;
    
    local angle = math.random(3.14 * 2)--math.atan(bacteria.speed.y / bacteria.speed.x)
    
    local pos_x = math.random(-bacteria.max_delta, bacteria.max_delta);
    local pos_y = math.random(-bacteria.max_delta, bacteria.max_delta);
    local max_dy = math.sqrt(bacteria.max_delta * bacteria.max_delta - pos_x * pos_x)
    local pos_y = math.random(- max_dy, max_dy);
    local x = pos_x - ItemGetWidth(bacteria.name) / 2 + self.zone.pos.x;
    local y = pos_y - ItemGetHeight(bacteria.name) / 2 + self.zone.pos.y;


--[[
    local x = bacteria.max_delta * math.sin(angle) - dx
    local y = bacteria.max_delta * math.cos(angle) - dy--]]
    local time = 5.0 + math.random(100) / 10;
    ItemMoveTo(bacteria.name, x, y, time * 3)
    
    DelayedExec(time * 3, function()
        if(not bacteria.remove and not self.finish) then
            self:bacteria_update_speed(bacteria)
            self:bacteria_move(bacteria)
        end
    end, 113)
end

function GameScreen:onObjectClick(item_name)   
    if(item_name == "dropper_part") then
        SetVisibleCursor(false)
        ItemRemove(self.name, "dropper_part")
        ItemRemove(self.name, "dropper_shadow")
        self:dropper_paint("empty")
        return;   
    end
    if(item_name == "liquid_green") then
        self:dropper_paint("green")
        self:update_bacteria_enable("green")
        return;
    end
    if(item_name == "liquid_blue") then
        self:dropper_paint("blue")
        self:update_bacteria_enable("blue")
        return;
    end
    if(item_name == "liquid_red") then
        self:dropper_paint("red")
        self:update_bacteria_enable("red")
        return;
    end
    if(item_name == "liquid_yellow") then
        self:dropper_paint("yellow")
        self:update_bacteria_enable("yellow")
        return;
    end
    
    if(self.drag_item and self.drag_item.color ~= "empty") then
        SoundPlay("sound/drop.ogg")
        local bacteria = self:get_bacteria_by_name(item_name)
        if(bacteria) then
            if(bacteria.color == self.drag_item.color) then
                SoundPlay("sound/interface/found.ogg")
                self:bacteria_remove(bacteria.name)
                self:dropper_paint("empty")
                self:UpdateCounter()
                if(self:IsMinigameCompleted()) then
                    self:OnFinish();
                end
            else
                self:dropper_paint("empty")
            end
        else
            self:dropper_paint("empty")
        end
    end
end

function GameScreen:update_bacteria_enable(color)
    for key, value in pairs(self.bacteria_table) do
        if(value.color == color) then
            ItemSetType(value.name, OBJ_TYPE.ACTIVE_ALL)
        else
            ItemSetType(value.name, OBJ_TYPE.NO_ACTIVE)
        end
    end
end

function GameScreen:bacteria_remove(_name)
    for key, value in pairs(self.bacteria_table) do
        if(value.name == _name) then
            value.remove = true;
            ItemRemove(self.name, value.name, 0.5)
            table.remove(self.bacteria_table, key)
            return;
        end
    end
end

function GameScreen:get_bacteria_by_name(_name)
    for key, value in pairs(self.bacteria_table) do
        if(value.name == _name) then
            return value;
        end
    end
    
    return nil
end

function GameScreen:IsMinigameCompleted()
    for key, value in pairs(self.bacteria_table) do
        if(value.color == "red" or value.color == "yellow" or value.color == "green" or value.color == "blue") then
            return false
        end
    end
    
    return true;
end

function GameScreen:OnFinish()
    if(self.finish) then return end
    self.finish = true;
    
    DragObject.active = false;
    SetVisibleCursor(true)
    
    for key, value in pairs(self.bacteria_table) do
        if(value.color == "red" or value.color == "yellow" or value.color == "green" or value.color == "blue") then
            ItemRemove(self.name, value.name, 0.5)
        end
    end
    if( self.drag_item) then
        ItemRemove(self.name, self.drag_item.name)
    end
    self.drag_item = nil
    
    DelayedExec(2.0, function()
        SetState("screen_008_portal", "state", "microscope_finish")
        DelayedExec(1.0, function() 
            clApplication:LoadLocation("008_portal")
        end, 113)
    end, 113)
end

function GameScreen:OnFinish2()
    if(self.finish) then return end
    self.finish = true;
    
    SetVisibleCursor(true)
    
    for key, value in pairs(self.bacteria_table) do
        if(value.color == "red" or value.color == "yellow" or value.color == "green" or value.color == "blue") then
            ItemRemove(self.name, value.name, 0.5)
        end
    end
    if( self.drag_item) then
        ItemRemove(self.name, self.drag_item.name)
    end
    self.drag_item = nil
    
    SetState("screen_008_portal", "state", "microscope_finish")
    clApplication:LoadLocation("008_portal")
end

function GameScreen:dropper_paint(color)
    if(self.finish) then return end
    
    if(self.drag_item == nil and color ~= "empty") then return end
    
    if( self.drag_item) then
        ItemRemove(DragObject.name, self.drag_item.name)
        DragObject.active = false;
    end
    self.drag_item = {}
    if(color == "empty") then
        DragObject:CreateObj(OBJ_TYPE.NO_ACTIVE, "dropper_drag", "graphics/minigames/08_microscope/dropper_empty", messenger_table.mouse_X - 5, messenger_table.mouse_Y - 85);	 
        self.drag_item.color = "empty"
        DragObject.active = true;
    elseif(color == "green") then
        SoundPlay("sound/dropper.ogg")
        DragObject:CreateObj(OBJ_TYPE.NO_ACTIVE, "dropper_drag", "graphics/minigames/08_microscope/dropper_green", messenger_table.mouse_X - 5, messenger_table.mouse_Y - 85);	 
        self.drag_item.color = "green";
        DragObject.active = true;
    elseif(color == "blue") then
        SoundPlay("sound/dropper.ogg")
        DragObject:CreateObj(OBJ_TYPE.NO_ACTIVE, "dropper_drag", "graphics/minigames/08_microscope/dropper_blue", messenger_table.mouse_X - 5, messenger_table.mouse_Y - 85);	 
        self.drag_item.color = "blue";
        DragObject.active = true;
    elseif(color == "red") then
        SoundPlay("sound/dropper.ogg")
        DragObject:CreateObj(OBJ_TYPE.NO_ACTIVE, "dropper_drag", "graphics/minigames/08_microscope/dropper_red", messenger_table.mouse_X - 5, messenger_table.mouse_Y - 85);	 
        self.drag_item.color = "red";
        DragObject.active = true;
    elseif(color == "yellow") then
        SoundPlay("sound/dropper.ogg")
        DragObject:CreateObj(OBJ_TYPE.NO_ACTIVE, "dropper_drag", "graphics/minigames/08_microscope/dropper_yellow", messenger_table.mouse_X - 5, messenger_table.mouse_Y - 85);	 
        self.drag_item.color = "yellow";
        DragObject.active = true;
    end
    self.drag_item.name = "dropper_drag";   
end

function GameScreen:on_hide_drag_object()
    if(self.drag_item) then
        SetVisibleCursor(true)
        ItemSetOpacity(self.drag_item.name, 0.0)
    end
end
------------------------------------------------------------------------------------------------------
function GameScreen:get_bacteria_count()
    local counter = 0
    for key, value in pairs(self.bacteria_table) do
        if(value.color == "red" or value.color == "yellow" or value.color == "green" or value.color == "blue") 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_bacteria_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", 60, 30)
    ItemSetColor("counter_text", 1.0, 1.0, 1.0, 1.0)
end
------------------------------------------------------------------------------------------------------
function GameScreen:UpdateCounter()
    local string = tostring(self:get_bacteria_count())
    SetLabelText("counter_text", string)
    ItemSetPosition("counter_text", 55 - ItemGetWidth("counter_text") / 2, 1117)
end

------------------------------------------------------------------------------------------------------
-------------------------------------------Mouse functions--------------------------------------------
------------------------------------------------------------------------------------------------------
function GameScreen:Update()
    if(self.drag_item) then
        ItemSetPosition(self.drag_item.name, messenger_table.mouse_X - 5, messenger_table.mouse_Y - 85);
    end 
end
------------------------------------------------------------------------------------------------------
function GameScreen:LoadScreen()
	--[[---Background---]]--
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "08_microscope", "graphics/minigames/08_microscope/08_microscope", 0, 0);	  

    
    self:CreateObj(OBJ_TYPE.TRIGGER, "08_microscope_01", "graphics/minigames/08_microscope/08_microscope_01", 0, 0);	
    self:CreateObj(OBJ_TYPE.TRIGGER, "liquid_green", "graphics/minigames/08_microscope/liquid_green", 510, 33);	
    self:CreateObj(OBJ_TYPE.TRIGGER, "liquid_blue", "graphics/minigames/08_microscope/liquid_blue", 398, 126);	
    self:CreateObj(OBJ_TYPE.TRIGGER, "liquid_red", "graphics/minigames/08_microscope/liquid_red", 275, 229);	
    self:CreateObj(OBJ_TYPE.TRIGGER, "liquid_yellow", "graphics/minigames/08_microscope/liquid_yellow", 126, 356);	
    self:CreateObj(OBJ_TYPE.TRIGGER, "bacteria_zone", "graphics/minigames/08_microscope/bacteria_zone", 636, 88);	
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "dropper_shadow", "graphics/minigames/08_microscope/dropper_shadow", 393, 428);	    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "dropper_part", "graphics/minigames/08_microscope/dropper_part", 492, 437);	  
    
    clScreenManager:add_screen("hud") 
end 