
local PAIRS_AMOUNT = 10

function GameScreen:InitScreen()
    self:LoadScreen()      

    self.bottles_table = {}
    for i = 1, PAIRS_AMOUNT do
        self:add_bottles_pair(i, "bottle_"..i, "bottle_"..i.."a")
    end        
    
    if(GetState("023_wine_security", "state") == "mg_completed") then
        clApplication:LoadLocation("024_fish")
    end
    
    HUDScreen:SetMiniGameMode()
    HUDScreen:CreateBackButton("023_wine") 
    MusicPlay({"sound/ambient/23_27_ambient.ogg", "sound/music/past.ogg"} , 23) 
    
    self.active = true
end

function GameScreen:add_bottles_pair(pair_id, bottle_name_1, bottle_name_2)
    self.pairs = self.pairs or {}
     
    local pair = {}
    pair.bottle1 = bottle_name_1;
    pair.bottle2 = bottle_name_2;
    if(GetState(CURRENT_SCREEN, "pair_"..pair_id) == "1") then
        pair.active = false       
        ItemSetOpacity(bottle_name_1, 0.0)
        ItemSetOpacity(bottle_name_2, 0.0)
        ItemSetOpacity(bottle_name_1.."_shadow", 0.0)
        ItemSetOpacity(bottle_name_2.."_shadow", 0.0)
    else    
        pair.active = true;
    end
    ItemSetOpacity(bottle_name_1.."_full", 0.0)
    ItemSetOpacity(bottle_name_2.."_full", 0.0)
    
    self.pairs[pair_id] = pair;
end

function GameScreen:get_bootle_by_name(item_name)
    for i = 1, PAIRS_AMOUNT do
        if(self.pairs[i].active and (item_name == self.pairs[i].bottle1 or item_name == self.pairs[i].bottle2)) then
            return item_name; 
        end
    end
    
    return nil;
end

function GameScreen:OnSkip()
end

function GameScreen:InMinigameCompleted()       
    return true
end

function GameScreen:full_bottle_show(obj_name)
    ItemChangeOpacity(obj_name, 0.0, 0.3)
    ItemChangeOpacity(obj_name.."_shadow", 0.0, 0.3)
    ItemChangeOpacity(obj_name.."_full", 1.0, 0.3)
end

function GameScreen:full_bottles_hide(obj_name)
    ItemChangeOpacity(obj_name, 1.0, 0.3)
    ItemChangeOpacity(obj_name.."_shadow", 1.0, 0.3)
    ItemChangeOpacity(obj_name.."_full", 0.0, 0.3)
end

function GameScreen:is_pair(obj_name1, obj_name2)
    for i = 1, PAIRS_AMOUNT do
        if(obj_name1 == self.pairs[i].bottle1 and obj_name2 == self.pairs[i].bottle2 or
            obj_name1 == self.pairs[i].bottle2 and obj_name2 == self.pairs[i].bottle1
        ) then
            return true; 
        end
    end
    
    return false;
end

local tem_found_eff = {
	    {key=EFFECTOR_MODIFIER.KEY_SCALE_XY, start_time = 0.0, time = 0.3, end_value = 1.1},
	    {key=EFFECTOR_MODIFIER.KEY_SCALE_XY, start_time = 0.3, time = 0.3, end_value = 1.0},
	    {key=EFFECTOR_MODIFIER.KEY_SCALE_XY, start_time = 0.6, time = 0.3, end_value = 1.1},
	    {key=EFFECTOR_MODIFIER.KEY_SCALE_XY, start_time = 0.9, time = 0.3, end_value = 1.0},
	    {key=EFFECTOR_MODIFIER.KEY_SCALE_XY, start_time = 1.2, time = 0.3, end_value = 1.1},
	    {key=EFFECTOR_MODIFIER.KEY_SCALE_XY, start_time = 1.5, time = 0.3, end_value = 1.0},
    }

function GameScreen:disactivate_pair(obj_name1, obj_name2)
    for i = 1, PAIRS_AMOUNT do
        if(self.pairs[i].bottle1 == obj_name1 and self.pairs[i].bottle2 == obj_name2 or
            self.pairs[i].bottle1 == obj_name2 and self.pairs[i].bottle2 == obj_name1) then
            SoundPlay("sound/interface/found.ogg")
            EffectorPlay(item_found_eff, obj_name1.."_full", false)
            EffectorPlay(item_found_eff, obj_name2.."_full", false)
            DelayedExec(1.5, function()
                ItemRemove(self.name, obj_name1.."_full")
                ItemRemove(self.name, obj_name2.."_full")
            end, 113)
            self.pairs[i].active = false;
            SetState(CURRENT_SCREEN, "pair_"..i, "1")
            return;
        end
    end
    
    return nil;
end

function GameScreen:IsMinigameCompleted()
    for i = 1, PAIRS_AMOUNT do
        if(self.pairs[i].active) then
            return false
        end
    end
    
    return true
end

function GameScreen:OnSkip()
    for i = 1, PAIRS_AMOUNT do
        if(self.pairs[i].active) then
            ItemSetOpacity(self.pairs[i].bottle1, 0.0)
            ItemSetOpacity(self.pairs[i].bottle1.."_full", 0.0)
            ItemSetOpacity(self.pairs[i].bottle1.."_shadow", 0.0)
            ItemSetOpacity(self.pairs[i].bottle2, 0.0)
            ItemSetOpacity(self.pairs[i].bottle2.."_full", 0.0)
            ItemSetOpacity(self.pairs[i].bottle2.."_shadow", 0.0)
            
            self.pairs[i].active = false;
            SetState(CURRENT_SCREEN, "pair_"..i, "1")
        end
    end
    
    self:OnFinish()
end

function GameScreen:OnFinish()
    SetState("023_wine_security", "state", "mg_completed")
    HUDScreen:SetInvntoryOnlyMode()
    HUDScreen:RemoveBackButton();
    DelayedExec(1.0, function()
        clApplication:LoadLocation("024_fish")
    end, 113)
end

function GameScreen:OnClick(item_name)
    if(self.active == false) then return end

    local obj = self:get_bootle_by_name(item_name);
    if(obj) then
        self:full_bottle_show(obj)
        self:SoundPlay("sound/bottle_take.ogg")
        if(self.active_bottle) then
            if(self:is_pair(self.active_bottle, obj)) then
                self.active = false;
                DelayedExec(0.8, function()
                    self:disactivate_pair(self.active_bottle, obj)
                    self.active_bottle = nil;
                    self.active = true;   
                    if(self:IsMinigameCompleted()) then
                        self:OnFinish();
                    end
                end, 113)
            else
                self.active = false;
                DelayedExec(0.8, function()
                    self:SoundPlay("sound/bottle_put.ogg")
                    self:full_bottles_hide(self.active_bottle);
                    self:full_bottles_hide(obj);
                    self.active_bottle = nil;
                    self.active = true;                    
                end, 113)
            end
        else
            self.active_bottle = obj
        end
    end
end

function GameScreen:Update()
end
------------------------------------------------------------------------------------------------------
-------------------------------------------Mouse functions--------------------------------------------
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
function GameScreen:LoadScreen()
	--[[---Background---]]--

    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "23_bottles", "graphics/minigames/23_bottles/23_bottles", 0, 0);
    
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_10a_shadow", "graphics/minigames/23_bottles/bottle_10a_shadow", 942, 946);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "bottle_10a", "graphics/minigames/23_bottles/bottle_10a_part", 951, 949);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_10_shadow", "graphics/minigames/23_bottles/bottle_10_shadow", 278, 969);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "bottle_10", "graphics/minigames/23_bottles/bottle_10_part", 286, 969);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_9a_shadow", "graphics/minigames/23_bottles/bottle_09a_shadow", 1160, 785);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "bottle_9a", "graphics/minigames/23_bottles/bottle_09a_part", 1166, 796);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_9_shadow", "graphics/minigames/23_bottles/bottle_09_shadow", 714, 794);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "bottle_9", "graphics/minigames/23_bottles/bottle_09_part", 725, 795);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_8a_shadow", "graphics/minigames/23_bottles/bottle_08a_shadow", 266, 784);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "bottle_8a", "graphics/minigames/23_bottles/bottle_08a_part", 286, 793);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_8_shadow", "graphics/minigames/23_bottles/bottle_08_shadow", 491, 624);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "bottle_8", "graphics/minigames/23_bottles/bottle_08_part", 508, 641);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "bottle_7a", "graphics/minigames/23_bottles/bottle_07a_part", 1336, 919);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "bottle_7", "graphics/minigames/23_bottles/bottle_07_part", 146, 566);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_6a_shadow", "graphics/minigames/23_bottles/bottle_06a_shadow", 131, 910);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "bottle_6a", "graphics/minigames/23_bottles/bottle_06a_part", 136, 894);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_6_shadow", "graphics/minigames/23_bottles/bottle_06_shadow", 1174, 445);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "bottle_6", "graphics/minigames/23_bottles/bottle_06_part", 1176, 492);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "bottle_5a", "graphics/minigames/23_bottles/bottle_05a_part", 513, 942);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_5_shadow", "graphics/minigames/23_bottles/bottle_05_shadow", 723, 436);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "bottle_5", "graphics/minigames/23_bottles/bottle_05_part", 741, 489);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_4a_shadow", "graphics/minigames/23_bottles/bottle_04a_shadow", 940, 625);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "bottle_4a", "graphics/minigames/23_bottles/bottle_04a_part", 951, 644);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_4_shadow", "graphics/minigames/23_bottles/bottle_04_shadow", 239, 422);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "bottle_4", "graphics/minigames/23_bottles/bottle_04_part", 268, 483);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "bottle_3a", "graphics/minigames/23_bottles/bottle_03a_part", 1176, 973);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_3_shadow", "graphics/minigames/23_bottles/bottle_03_shadow", 950, 308);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "bottle_3", "graphics/minigames/23_bottles/bottle_03_part", 958, 338);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "bottle_2a", "graphics/minigames/23_bottles/bottle_02a_part", 1331, 607);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_2_shadow", "graphics/minigames/23_bottles/bottle_02_shadow", 487, 283);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "bottle_2", "graphics/minigames/23_bottles/bottle_02_part", 507, 324);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_1a_shadow", "graphics/minigames/23_bottles/bottle_01a_shadow", 589, 953);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "bottle_1a", "graphics/minigames/23_bottles/bottle_01a_part", 730, 968);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_1_shadow", "graphics/minigames/23_bottles/bottle_01_shadow", 732, 123);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "bottle_1", "graphics/minigames/23_bottles/bottle_01_part", 738, 176);
    
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_10_full", "graphics/minigames/23_bottles/bottle_10_full", 282, 933);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_10a_full", "graphics/minigames/23_bottles/bottle_10a_full", 951, 847);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_9a_full", "graphics/minigames/23_bottles/bottle_09a_full", 1165, 656);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_9_full", "graphics/minigames/23_bottles/bottle_09_full", 722, 653);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_8a_full", "graphics/minigames/23_bottles/bottle_08a_full", 292, 626);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_8_full", "graphics/minigames/23_bottles/bottle_08_full", 513, 477);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_7a_full", "graphics/minigames/23_bottles/bottle_07a_full", 1344, 817);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_7_full", "graphics/minigames/23_bottles/bottle_07_full", 149, 452);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_6a_full", "graphics/minigames/23_bottles/bottle_06a_full", 148, 748);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_6_full", "graphics/minigames/23_bottles/bottle_06_full", 1185, 339);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_5a_full", "graphics/minigames/23_bottles/bottle_05a_full", 528, 797);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_5_full", "graphics/minigames/23_bottles/bottle_05_full", 756, 343);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_4a_full", "graphics/minigames/23_bottles/bottle_04a_full", 969, 498);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_4_full", "graphics/minigames/23_bottles/bottle_04_full", 281, 340);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_3a_full", "graphics/minigames/23_bottles/bottle_03a_full", 1191, 934);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_3_full", "graphics/minigames/23_bottles/bottle_03_full", 974, 226);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_2a_full", "graphics/minigames/23_bottles/bottle_02a_full", 1353, 507);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_2_full", "graphics/minigames/23_bottles/bottle_02_full", 529, 207);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_1a_full", "graphics/minigames/23_bottles/bottle_01a_full", 743, 943);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "bottle_1_full", "graphics/minigames/23_bottles/bottle_01_full", 755, 66);

	
    clScreenManager:add_screen("hud") 
end


