GameScreen.inv_fun = {}
GameScreen.inv_fun["jewel_027"] = function()
    Inventory:ClueAndHide()
    DragObject:CreateObj(OBJ_TYPE.ACTIVE, "jewel_09_full", "graphics/minigames/28_diadem/jewel_09_full", -100, -100);
    DragObject.active = false;
    DragObject.active2 = false
    DelayedExec(0.1, function()
        DragObject.active2 = true
    end)
    
    --Inventory:remove_tool("jewel_027")
    --Inventory:UpdateData();
    --Inventory:add_tool("jewel_027", true)

    CurrentScreen.drag_object = {}
    CurrentScreen.drag_object.name = "jewel_09_full";
    CurrentScreen.drag_object.diadem = CurrentScreen:get_diadem_by_name("jewel_09")
    
    CurrentScreen.drag_object.inv = true;
    clApplication.lock_cursor = false;
    SoundPlay("sound/jewel_take.ogg")
end


function GameScreen:InitScreen()
    self:LoadScreen()       
    
    self.places_array = {
        {name = "jewel_01", pos = {1381, 664}, id = 1}, 
        {name = "jewel_02a", pos = {265, 648}, id = 2},
        {name = "jewel_02b", pos = {268, 713}, id = 2},
        {name = "jewel_02c", pos = {399, 647}, id = 2},
        {name = "jewel_02d", pos = {403, 719}, id = 2},
        {name = "jewel_02e", pos = {1169, 650}, id = 2},
        {name = "jewel_02f", pos = {1164, 724}, id = 2},
        {name = "jewel_02g", pos = {1310, 654}, id = 2},
        {name = "jewel_03a", pos = {293, 685}, id = 3},
        {name = "jewel_03b", pos = {414, 689}, id = 3},
        {name = "jewel_03c", pos = {906, 680}, id = 3},
        {name = "jewel_03d", pos = {1186, 693}, id = 3},
        {name = "jewel_04a", pos = {324, 623}, id = 4},
        {name = "jewel_04b", pos = {706, 623}, id = 4},
        {name = "jewel_04c", pos = {735, 593}, id = 4},
        {name = "jewel_04d", pos = {866, 594}, id = 4},
        {name = "jewel_04e", pos = {896, 624}, id = 4},
        {name = "jewel_04f", pos = {1231, 626}, id = 4},
        {name = "jewel_04g", pos = {1278, 626}, id = 4},
        {name = "jewel_05", pos = {1245, 525}, id = 5},
        {name = "jewel_06a", pos = {325, 651}, id = 6},
        {name = "jewel_06b", pos = {1225, 653}, id = 6},
        {name = "jewel_07a", pos = {473, 671}, id = 7},
        {name = "jewel_07b", pos = {1030, 674}, id = 7},
        {name = "jewel_08a", pos = {619, 705}, id = 8},
        {name = "jewel_08b", pos = {924, 637}, id = 8},
        {name = "jewel_08c", pos = {916, 706}, id = 8},
        {name = "jewel_09", pos = {728, 620}, id = 9},
        {name = "jewel_10", pos = {747, 428}, id = 10},
    }    
        
    self.diadem_array = {
        {name = "jewel_01", pos = {1381, 664}, full = "jewel_01_full", id = 1}, 
        {name = "jewel_02a", pos = {265, 648}, full = "jewel_02_full", id = 2},
        {name = "jewel_02b", pos = {268, 713}, full = "jewel_02_full", id = 2},
        {name = "jewel_02c", pos = {399, 647}, full = "jewel_02_full", id = 2},
        {name = "jewel_02d", pos = {403, 719}, full = "jewel_02_full", id = 2},
        {name = "jewel_02e", pos = {1169, 650}, full = "jewel_02_full", id = 2},
        {name = "jewel_02f", pos = {1164, 724}, full = "jewel_02_full", id = 2},
        {name = "jewel_02g", pos = {1310, 654}, full = "jewel_02_full", id = 2},
        {name = "jewel_03a", pos = {293, 685}, full = "jewel_03_full", id = 3},
        {name = "jewel_03b", pos = {414, 689}, full = "jewel_03_full", id = 3},
        {name = "jewel_03c", pos = {906, 680}, full = "jewel_03_full", id = 3},
        {name = "jewel_03d", pos = {1186, 693}, full = "jewel_03_full", id = 3},
        {name = "jewel_04a", pos = {324, 623}, full = "jewel_04_full", id = 4},
        {name = "jewel_04b", pos = {706, 623}, full = "jewel_04_full", id = 4},
        {name = "jewel_04c", pos = {735, 593}, full = "jewel_04_full", id = 4},
        {name = "jewel_04d", pos = {866, 594}, full = "jewel_04_full", id = 4},
        {name = "jewel_04e", pos = {896, 624}, full = "jewel_04_full", id = 4},
        {name = "jewel_04f", pos = {1231, 626}, full = "jewel_04_full", id = 4},
        {name = "jewel_04g", pos = {1278, 626}, full = "jewel_04_full", id = 4},
        {name = "jewel_05", pos = {1245, 525}, full = "jewel_05_full", id = 5},
        {name = "jewel_06a", pos = {325, 651}, full = "jewel_06_full", id = 6},
        {name = "jewel_06b", pos = {1225, 653}, full = "jewel_06_full", id = 6},
        {name = "jewel_07a", pos = {473, 671}, full = "jewel_07_full", id = 7},
        {name = "jewel_07b", pos = {1030, 674}, full = "jewel_07_full", id = 7},
        {name = "jewel_08a", pos = {619, 705}, full = "jewel_08_full", id = 8},
        {name = "jewel_08b", pos = {924, 637}, full = "jewel_08_full", id = 8},
        {name = "jewel_08c", pos = {916, 706}, full = "jewel_08_full", id = 8},
        {name = "jewel_09", pos = {728, 620}, full = "jewel_09_full", id = 9},
        {name = "jewel_10", pos = {747, 428}, full = "jewel_10_full", id = 10},
    }
    self.active = true
    
    --Inventory:add_tool("jewel_027")--TODO
    
    MusicPlay({"sound/music/past.ogg", 30} , 28) 
    
    for key, value in pairs(self.diadem_array) do
        if(GetState(CURRENT_SCREEN, "diadem_"..value.name) == "1") then
            ItemRemove(self.name, value.name)
        end
    end
    for key, value in pairs(self.places_array) do
        if(GetState(CURRENT_SCREEN, "place_"..value.name) == "1") then
            self:CreateObj(OBJ_TYPE.NO_ACTIVE,  value.name.."_affix", "graphics/minigames/28_diadem/"..value.name.."_affix", value.pos[1], value.pos[2]);
            value.active = false;
        end
    end
    
    self:CreateCounter()
    HUDScreen:SetMiniGameMode()
    
    HUDScreen:CreateBackButton("028_cloakroom")
end

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

function GameScreen:get_place(item_name, id)   
    for key, value in pairs(self.places_array) do
        if(value.id == id and value.active ~= false) then
            if(math.abs(ItemGetX(item_name) - value.pos[1]) < 40 and math.abs(ItemGetY(item_name) - value.pos[2]) < 40) then
                return value;
            end
        end
    end
    
    return nil;
end

function GameScreen:onObjectClick(item_name)   
    if(not self.active) then return end    
    
    if(self.drag_object and self.drag_object.inv) then
        local place = self:get_place(self.drag_object.name, self.drag_object.diadem.id);
        if(place and place.active ~= false) then
            ItemRemove(DragObject.name, self.drag_object.name, 0.2);
            self:CreateObj(OBJ_TYPE.NO_ACTIVE,  place.name.."_affix", "graphics/minigames/28_diadem/"..place.name.."_affix", place.pos[1], place.pos[2]);
            Inventory:ActiveToolApply()
            Inventory:remove_tool("jewel_027")
            DragObject.active = false;
            Inventory.lock_drag = true;
            place.active = false;
            SetState(CURRENT_SCREEN, "place_"..place.name, "1") 
            SetState(CURRENT_SCREEN, "diadem_"..self.drag_object.diadem.name, "1")
            self.drag_object = nil
            SoundPlay("sound/jewel_affix.ogg")
            
            if(self:InMinigameCompleted())then
                self:OnFinish();
            end
        elseif(DragObject.active2) then
            ItemRemove(DragObject.name, self.drag_object.name);
            Inventory:ActiveToolDrop()
            DragObject.active = false;
            Inventory.lock_drag = true;
            self.drag_object = nil
            SoundPlay("sound/interface/wrong.ogg")
            Inventory:UpdateData();
            --Inventory:ReturnObject()
            clApplication.lock_cursor = false;
        end
    else
        if(self.drag_object == nil) then
            local diadem = self:get_diadem_by_name(item_name)
            if(diadem) then
                SetVisibleCursor(false)
                self.drag_object = {}
                DragObject:CreateObj(OBJ_TYPE.NO_ACTIVE, diadem.name.."_full", "graphics/minigames/28_diadem/"..diadem.full, -100, -100);
                DragObject.active = true;
                self.drag_object.diadem = diadem;
                self.drag_object.name = diadem.name.."_full";
                ItemSetOpacity(diadem.name, 0.0);
                SoundPlay("sound/jewel_take.ogg")
            end
        else
            SetVisibleCursor(true)
            DragObject.active = false;
            local place = self:get_place(self.drag_object.name, self.drag_object.diadem.id);
            if(place and place.active ~= false) then
                ItemRemove(DragObject.name, self.drag_object.name, 0.2);
                ItemRemove(self.name, self.drag_object.diadem.name);
                self:CreateObj(OBJ_TYPE.NO_ACTIVE,  place.name.."_affix", "graphics/minigames/28_diadem/"..place.name.."_affix", place.pos[1], place.pos[2]);
                place.active = false;
                SetState(CURRENT_SCREEN, "place_"..place.name, "1") 
                SetState(CURRENT_SCREEN, "diadem_"..self.drag_object.diadem.name, "1")
                SoundPlay("sound/jewel_affix.ogg")
                if(self:InMinigameCompleted())then
                    self:OnFinish();
                end 
            else
                ItemMoveTo(self.drag_object.name, ItemGetX(self.drag_object.diadem.name), ItemGetY(self.drag_object.diadem.name), 0.3)
                self.active = false;
                local name1 = self.drag_object.name;
                local name2 = self.drag_object.diadem.name;
                DelayedExec(0.3, function()
                    self.active = true;
                    ItemRemove(DragObject.name, name1);
                    ItemSetOpacity(name2, 1.0)
                end, 113)
                SoundPlay("sound/interface/wrong.ogg")
            end
            
            self.drag_object = nil;
        end
    end
    
    self:UpdateCounter()
end

------------------------------------------------------------------------------------------------------
function GameScreen:OnSkip()
    if(self.active == false) then return end
    
    Inventory:remove_tool("jewel_027")
    
    for key, value in pairs(self.diadem_array) do
        if(GetState(CURRENT_SCREEN, "diadem_"..value.name) ~= "1") then
            ItemRemove(self.name, value.name)
        end
    end
    for key, value in pairs(self.places_array) do
        if(GetState(CURRENT_SCREEN, "place_"..value.name) ~= "1") then
            self:CreateObj(OBJ_TYPE.NO_ACTIVE,  value.name.."_affix", "graphics/minigames/28_diadem/"..value.name.."_affix", value.pos[1], value.pos[2]);
            value.active = false;
        end
    end
    
    self:OnFinish()
end

function GameScreen:OnFinish()
    if(self.active == false) then return end
    
    HUDScreen:SetInvntoryOnlyMode()
    HUDScreen:RemoveBackButton();
    
    self.active = true;
    SetState("screen_028_cloakroom", "diadem", "creating")
    DelayedExec(1.0, function() 
        clApplication:LoadLocation("028_cloakroom")
    end, 113)
end

function GameScreen:InMinigameCompleted()    
    if(self.active == false) then return end
    
    for key, value in pairs(self.diadem_array) do
        if(GetState(CURRENT_SCREEN, "diadem_"..value.name) ~= "1") then
            return false
        end
    end
    
    return true
end

------------------------------------------------------------------------------------------------------
function GameScreen:get_diadem_count()
    local counter = 0
    for key, value in pairs(self.diadem_array) do
        if(GetState(CURRENT_SCREEN, "diadem_"..value.name) ~= "1") 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_diadem_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_diadem_count())
    SetLabelText("counter_text", string)
    ItemSetPosition("counter_text", 55 - ItemGetWidth("counter_text") / 2, 1117)
end
------------------------------------------------------------------------------------------------------
function GameScreen:Update()
    if(self.drag_object) then
        local pos_x = messenger_table.mouse_X - ItemGetWidth(self.drag_object.name) / 2;
        local pos_y = messenger_table.mouse_Y - ItemGetHeight(self.drag_object.name) / 2;
        ItemSetPosition(self.drag_object.name, pos_x, pos_y);
    end
end
------------------------------------------------------------------------------------------------------
-------------------------------------------Mouse functions--------------------------------------------
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
function GameScreen:LoadScreen()
	--[[---Background---]]--
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "28_diadem", "graphics/minigames/28_diadem/28_diadem", 0, 0);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_10", "graphics/minigames/28_diadem/jewel_10_part", 1142, 289);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_08c", "graphics/minigames/28_diadem/jewel_08c_part", 995, 465);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_08b", "graphics/minigames/28_diadem/jewel_08b_part", 782, 80);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_08a", "graphics/minigames/28_diadem/jewel_08a_part", 259, 247);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_07b", "graphics/minigames/28_diadem/jewel_07b_part", 149, 380);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_07a", "graphics/minigames/28_diadem/jewel_07a_part", 1330, 361);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_06b", "graphics/minigames/28_diadem/jewel_06b_part", 1496, 332);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_06a", "graphics/minigames/28_diadem/jewel_06a_part", 1, 206);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_05", "graphics/minigames/28_diadem/jewel_05_part", 441, 534);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_04g", "graphics/minigames/28_diadem/jewel_04g_part", 1498, 819);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_04f", "graphics/minigames/28_diadem/jewel_04f_part", 1494, 672);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_04e", "graphics/minigames/28_diadem/jewel_04e_part", 1347, 606);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_04d", "graphics/minigames/28_diadem/jewel_04d_part", 882, 1146);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_04c", "graphics/minigames/28_diadem/jewel_04c_part", 784, 792);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_04b", "graphics/minigames/28_diadem/jewel_04b_part", 13, 730);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_04a", "graphics/minigames/28_diadem/jewel_04a_part", 91, 420);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_03d", "graphics/minigames/28_diadem/jewel_03d_part", 1205, 940);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_03c", "graphics/minigames/28_diadem/jewel_03c_part", 465, 903);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_03b", "graphics/minigames/28_diadem/jewel_03b_part", 268, 1017);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_03a", "graphics/minigames/28_diadem/jewel_03a_part", 31, 1045);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_02g", "graphics/minigames/28_diadem/jewel_02g_part", 1491, 857);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_02f", "graphics/minigames/28_diadem/jewel_02f_part", 1059, 591);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_02e", "graphics/minigames/28_diadem/jewel_02e_part", 747, 281);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_02d", "graphics/minigames/28_diadem/jewel_02d_part", 602, 131);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_02c", "graphics/minigames/28_diadem/jewel_02c_part", 219, 584);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_02b", "graphics/minigames/28_diadem/jewel_02b_part", 202, 283);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_02a", "graphics/minigames/28_diadem/jewel_02a_part", 11, 432);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "jewel_01", "graphics/minigames/28_diadem/jewel_01_part", 860, 337);
    
    clScreenManager:add_screen("hud") 
end

