GameScreen.textures_path = "graphics/screens/10_museum/location/"

function GameScreen:RestoreScene()
    if(GetState(CURRENT_SCREEN, "state") == "") then
        SetState(CURRENT_SCREEN, "state", "ho")
        local end_tip = function()
        end
        self:StartHO()
        HUDScreen:ShowTips({path = self.textures_path.."text_10", x = 328, y = 1112, end_fun = end_tip})--]]
    elseif(GetState(CURRENT_SCREEN, "state") == "ho") then
        self:StartHO()
    elseif(GetState(CURRENT_SCREEN, "state") == "mg") then
        self:startMG();
    elseif(GetState(CURRENT_SCREEN, "state") == "finish") then
        HUDScreen:SetInvntoryOnlyMode()
        ItemSetOpacity("door_open", 1.0)
        self:CreateLocationPortal("door_open", "011_gate", "hand");
        ItemSetType("door_open", OBJ_TYPE.ACTIVE_ALL)
    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 
    
    for key, value in pairs(self.mg_parts) do
        if(GetState(CURRENT_SCREEN, value) == "1") then 
            ItemRemove(self.name, value)
            ItemRemove(self.name, value.."_shadow")
            if(GetState(CURRENT_SCREEN, "state") == "mg") then
                self:CreateObj(OBJ_TYPE.NO_ACTIVE, value.."_final", "graphics/screens/10_museum/location/"..value.."_full", self.finish_coord[key][1], self.finish_coord[key][2]);
            end
        end
    end
end

function GameScreen:InitScreen()
    self:LoadScreen()

    
    self.mg_parts = {"slab_01",  "slab_02", "slab_03", "slab_04", "slab_05",
                        "slab_06",  "slab_07", "slab_08", "slab_09", "slab_10",
                        "slab_11",  "slab_12", "slab_13", "slab_14", "slab_15"}
    
    self.finish_coord = {{818, 453}, {901, 478}, {857, 450}, {839, 578}, {1118, 537}, {1119, 581},
                         {1202, 595}, {1183, 493}, {1155, 362}, {970, 465}, {865, 419}, {923, 392}, 
                            {1269, 532}, {1298, 469}, {1287, 396}}
    
    self.item_list = {
        {obj = {"scarab", {x = 931, y = 114}}},
        {obj = {"arrow", {x = 621, y = 684}}},
        {obj = {"oil_lamp", {x = 937, y = 74}}},
        {obj = {"sceptre", {x = 651, y = 29}}},
        {obj = {"tankard", {x = 1234, y = 691}}},
        {obj = {"note_10", {x = 19, y = 350}}, wallpaper = {"note_10_full", 477, 211}},
        {obj = {"mask", {x = 1387, y = 707}}},
        {obj = {"translator", {x = 283, y = 935}}, inv = "translator_010"},
        {obj = {"crown", {x = 1024, y = 677}}},
        {obj = {"vase", {x = 1246, y = 1}}},
        {obj = {"hand_mirror", {x = 293, y = 72}}, inv = "hand_mirror_010"},
        {obj = {"figurine", {x = 1209, y = 863}}},
    }
    
    self:SetItemCursor("note_10_full", "eye")
    self:SetItemCursor("note_in_the_table", "eye")
    self:CreateWallpaperPortal("note_in_the_table", "note_10_full", 477, 211)
    
    self:AddHiddenCrystal("crystal_10")
    
    
    self:SetItemSound("revolver", "sound/revolver.ogg");
    self:SetItemSound("clock", "sound/clock_chimes.ogg");
    self:SetItemSound("bottle_04", "sound/bottle_02.ogg");
    self:SetItemSound("bottle_03", "sound/bottle_03.ogg");
    self:SetItemSound("bottle_02", "sound/bottle_07.ogg");
    self:SetItemSound("bottle_01", "sound/bottle_05.ogg");
    self:SetItemSound("walkie", {"sound/walkie_01.ogg", "sound/walkie_03.ogg"});
    self:SetItemSound("bell", "sound/bell.ogg");
    self:SetItemSound("guitar", "sound/guitar.ogg");
    self:SetItemSound("drum", "sound/drum.ogg");
    self:SetItemSound("megaphone", "sound/megaphone.ogg");
    self:SetItemSound("trumpet", "sound/trumpet.ogg");
    self:SetItemSound("bedpan", "sound/bedpan.ogg");
    
    MusicPlay({"sound/music/spaceship.ogg", "sound/ambient/10_ambient.ogg"} , 10)
    
    self:RestoreScene();   
end    
------------------------------------------------------------------------------------------------------
function GameScreen:StartHO()
    local on_finish = function()
        SetState(CURRENT_SCREEN, "state", "mg")
        HUDScreen:SetInvntoryOnlyMode()
        clApplication.curret_win = clScreenManager:add_screen("tool_found_screen") 
        clApplication.curret_win._resource = "graphics/screens/10_museum/location/object_found_10"
        clApplication.curret_win.end_fun = function()
            Inventory:ToolFound("translator_010", 800, 600)
            DelayedExec(0.4, function()
                Inventory:ToolFound("hand_mirror_010", 800, 600)
            end)
            local end_tip_10a = function()
                DelayedExec(1.0, function()
                    local end_tip_10b = function()
                        DelayedExec(1.0, function()
                            HUDScreen:ShowHintText({path = self.textures_path.."hint_10", x = 310, y = 1109, end_fun = end_hint_10})
                            self:startMG();
                        end, 113)
                    end
                    HUDScreen:ShowTips({path = self.textures_path.."text_10b", x = 359, y = 1127, end_fun = end_tip_10b})
                end, 113)
            end
            HUDScreen:ShowTips({path = self.textures_path.."text_10a", x = 314, y = 1130, end_fun = end_tip_10a})  
        end  
    end
    self:SetMiniGame(mgt_hidden_object)   
    self.mini_game:Init(self.item_list, on_finish);
end
------------------------------------------------------------------------------------------------------
function GameScreen:startMG()
    HUDScreen:SetMiniGameMode()
    self:CreateCounter();
    self.state = "mg"
end 
------------------------------------------------------------------------------------------------------
function GameScreen:__get_part_id_by_name(part_name)
    for key, value in pairs(self.mg_parts) do
        if(value == part_name) then 
            return key;
        end
    end
    
    return nil
end
------------------------------------------------------------------------------------------------------
function GameScreen:__is_mg_completed()
    for key, value in pairs(self.mg_parts) do
        if(GetState(CURRENT_SCREEN, value) ~= "1") then 
            return false;
        end
    end
    return true;
end
------------------------------------------------------------------------------------------------------
function GameScreen:onObjectClick(item_name)
    if(self.state == "mg") then
        if(item_name == "skip") then
            self.drag_item = nil;
            
            for key, value in pairs(self.mg_parts) do
                if(GetState(CURRENT_SCREEN, value) ~= "1") then 
                    ItemRemove(HUDScreen.name, value.."_full", 0.2)
                    ItemRemove(self.name, value)
                    ItemRemove(self.name, value.."_shadow")
                    SetState(CURRENT_SCREEN, value, "1")
                    self:CreateObj(OBJ_TYPE.NO_ACTIVE, value.."_final", "graphics/screens/10_museum/location/"..value.."_full", self.finish_coord[key][1], self.finish_coord[key][2]);
                end
            end
            
            DelayedExec(0.5, function()
                self:RemoveCounter();
            end, 113)
            SoundPlay("sound/10_door_open.ogg")
            self:CreateLocationPortal("door_open", "011_gate", "hand");
            ItemChangeOpacity("door_open", 1.0, 0.5)
            SetState(CURRENT_SCREEN, "state", "finish")
            ItemSetType("door_open", OBJ_TYPE.ACTIVE_ALL)
            ItemSetDepth("door_open", LAYERS.FRONT_MOST)
            ItemSetDepth("door_cover", LAYERS.FRONT_MOST)
            HUDScreen:SetInvntoryOnlyMode()
            
            return;
        end
        
        if(self.drag_item) then
            if(math.abs(self.finish_coord[self.drag_item.id][1] - ItemGetX(self.drag_item.full)) < 30 and math.abs(self.finish_coord[self.drag_item.id][2] - ItemGetY(self.drag_item.full)) < 30) then
                ItemRemove(HUDScreen.name, self.drag_item.full, 0.2)
                ItemRemove(self.name, self.drag_item.part)
                ItemRemove(self.name, self.drag_item.part.."_shadow")
                SetState(CURRENT_SCREEN, self.drag_item.part, "1")
                self:CreateObj(OBJ_TYPE.NO_ACTIVE, self.drag_item.part.."_final", "graphics/screens/10_museum/location/"..self.drag_item.part.."_full", self.finish_coord[self.drag_item.id][1], self.finish_coord[self.drag_item.id][2]);
                self:UpdateCounter()
                SoundPlay("sound/slab_affix.ogg")
                if(self:__is_mg_completed())then
                    HUDScreen:SetInvntoryOnlyMode()
                    DelayedExec(0.5, function()
                        self:RemoveCounter();
                    end, 113)
                    SoundPlay("sound/10_door_open.ogg")
                    self:CreateLocationPortal("door_open", "011_gate", "hand");
                    ItemChangeOpacity("door_open", 1.0, 0.5)
                    SetState(CURRENT_SCREEN, "state", "finish")
                    ItemSetType("door_open", OBJ_TYPE.ACTIVE_ALL)
                    ItemSetDepth("door_open", LAYERS.FRONT_MOST)
                end
            else
                SoundPlay("sound/interface/wrong.ogg")
                ItemMoveTo(self.drag_item.full, self.drag_item.start_pos.x, self.drag_item.start_pos.y, 0.5)   
                local full_name = self.drag_item.full;
                local part_name = self.drag_item.part;
                DelayedExec(0.5, function()
                    ItemRemove(HUDScreen.name, full_name, 0.2)
                    ItemChangeOpacity(part_name, 1.0, 0.2)
                    ItemChangeOpacity(part_name.."_shadow", 0.2)
                end, 113)
            end
            
            self.drag_item = nil
            SetVisibleCursor(true)
        else
            local part_id = self:__get_part_id_by_name(item_name)
            if(part_id) then
                SoundPlay("sound/slab_take.ogg")
                ItemChangeOpacity(self.mg_parts[part_id], 0.0, 0.2)
                ItemChangeOpacity(self.mg_parts[part_id].."_shadow", 0.0, 0.2)
                self.drag_item = {}
                
                HUDScreen:CreateObj(OBJ_TYPE.NO_ACTIVE, self.mg_parts[part_id].."_full", "graphics/screens/10_museum/location/"..self.mg_parts[part_id].."_full", messenger_table.mouse_X, messenger_table.mouse_Y);
                self.drag_item.full = self.mg_parts[part_id].."_full";
                ItemSetOpacity(self.drag_item.full, 0.0)
                ItemChangeOpacity(self.drag_item.full, 1.0, 0.2)
                self.drag_item.part = self.mg_parts[part_id];
                self.drag_item.id = part_id;
                self.drag_item.start_pos = {x = ItemGetX(self.drag_item.part), y = ItemGetY(self.drag_item.part)}
            end
        end
    end
end

------------------------------------------------------------------------------------------------------
function GameScreen:get_part_count()
    local counter = 0
    for key, value in pairs(self.mg_parts) do
        if(GetState(CURRENT_SCREEN, value) ~= "1") then 
            counter = counter + 1;
        end
    end
    return counter;
end
------------------------------------------------------------------------------------------------------
function GameScreen:CreateCounter()
    HUDScreen:CreateObj(OBJ_TYPE.NO_ACTIVE, "counter_back", "graphics/interface/hud/counter", 18, 1104);
    local string = tostring(self:get_part_count())
    HUDScreen:CreateLabel("counter_text", string, 55, 1117, 53, FONT_QUICKSAND)
    ItemSetSize("counter_text", 60, 30)
    ItemSetPosition("counter_text", 55 - ItemGetWidth("counter_text") / 2, 1117)
    --SetLabelAlign("counter_text", TEXT_ALIGN.H_CENTER)
    
    ItemSetColor("counter_text", 1.0, 1.0, 1.0, 1.0)
end
------------------------------------------------------------------------------------------------------
function GameScreen:RemoveCounter()
    ItemRemove(HUDScreen.name, "counter_back", 0.2)
    ItemRemove(HUDScreen.name, "counter_text", 0.2)
end
------------------------------------------------------------------------------------------------------
function GameScreen:UpdateCounter()
    local string = tostring(self:get_part_count())
    SetLabelText("counter_text", string)
    ItemSetPosition("counter_text", 55 - ItemGetWidth("counter_text") / 2, 1117)
end
------------------------------------------------------------------------------------------------------
function GameScreen:onMouseMove(item_name)
    if(self.drag_item) then
        if(item_name == "skip" or item_name == "hint" or item_name == "menu_button") then
            SetVisibleCursor(true)
            ItemSetOpacity(self.drag_item.full, 0.0)
        else
            SetVisibleCursor(false)
            ItemSetOpacity(self.drag_item.full, 1.0)
        end
    end
end
------------------------------------------------------------------------------------------------------
function GameScreen:Update()
    if(self.drag_item) then
        ItemSetPosition(self.drag_item.full, messenger_table.mouse_X - ItemGetWidth(self.drag_item.full) / 2, messenger_table.mouse_Y - ItemGetHeight(self.drag_item.full) / 2);
    end 
end
------------------------------------------------------------------------------------------------------
-------------------------------------------Mouse functions--------------------------------------------
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
function GameScreen:LoadScreen()
	--[[---Background---]]--
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "10_museum", "graphics/screens/10_museum/location/10_museum", 0, 0);
    
    self:CreateObj(OBJ_TYPE.TRIGGER, "revolver", "graphics/screens/10_museum/location/revolver", 755, 1094);
    self:CreateObj(OBJ_TYPE.TRIGGER, "clock", "graphics/screens/10_museum/location/clock", 1187, 764);
    self:CreateObj(OBJ_TYPE.TRIGGER, "bottle_04", "graphics/screens/10_museum/location/bottle_04", 790, 737);
    self:CreateObj(OBJ_TYPE.TRIGGER, "bottle_03", "graphics/screens/10_museum/location/bottle_03", 766, 862);
    self:CreateObj(OBJ_TYPE.TRIGGER, "bottle_02", "graphics/screens/10_museum/location/bottle_02", 463, 886);
    self:CreateObj(OBJ_TYPE.TRIGGER, "bottle_01", "graphics/screens/10_museum/location/bottle_01", 611, 1111);
    self:CreateObj(OBJ_TYPE.TRIGGER, "walkie", "graphics/screens/10_museum/location/walkie", 363, 1105);
    self:CreateObj(OBJ_TYPE.TRIGGER, "bell", "graphics/screens/10_museum/location/bell", 886, 113);
    self:CreateObj(OBJ_TYPE.TRIGGER, "guitar", "graphics/screens/10_museum/location/guitar", 781, 236);
    self:CreateObj(OBJ_TYPE.TRIGGER, "drum", "graphics/screens/10_museum/location/drum", 272, 842);
    self:CreateObj(OBJ_TYPE.TRIGGER, "megaphone", "graphics/screens/10_museum/location/megaphone", 443, 208);
    self:CreateObj(OBJ_TYPE.TRIGGER, "trumpet", "graphics/screens/10_museum/location/trumpet", 667, 330);
    self:CreateObj(OBJ_TYPE.TRIGGER, "bedpan", "graphics/screens/10_museum/location/bedpan", 834, 884);
    
    ------------------------------MG
    ---[[
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "slab_15_shadow", "graphics/screens/10_museum/location/slab_15_shadow", 256, 367);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slab_15", "graphics/screens/10_museum/location/slab_15_part", 334, 388);    
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "slab_14_shadow", "graphics/screens/10_museum/location/slab_14_shadow", 380, 66);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slab_14", "graphics/screens/10_museum/location/slab_14_part", 410, 113);    
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "slab_13_shadow", "graphics/screens/10_museum/location/slab_13_shadow", 517, 324);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slab_13", "graphics/screens/10_museum/location/slab_13_part", 547, 354);    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slab_12", "graphics/screens/10_museum/location/slab_12_part", 1473, 885);    
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "slab_11_shadow", "graphics/screens/10_museum/location/slab_11_shadow", 345, 954);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slab_11", "graphics/screens/10_museum/location/slab_11_part", 379, 987);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "slab_10_shadow", "graphics/screens/10_museum/location/slab_10_shadow", 1322, 104);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slab_10", "graphics/screens/10_museum/location/slab_10_part", 1348, 132);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "slab_09_shadow", "graphics/screens/10_museum/location/slab_09_shadow", 632, 49);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slab_09", "graphics/screens/10_museum/location/slab_09_part", 646, 72);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "slab_08_shadow", "graphics/screens/10_museum/location/slab_08_shadow", 135, 398);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slab_08", "graphics/screens/10_museum/location/slab_08_part", 148, 341);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "slab_07_shadow", "graphics/screens/10_museum/location/slab_07_shadow", 712, 1014);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slab_07", "graphics/screens/10_museum/location/slab_07_part", 865, 1008);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "slab_06_shadow", "graphics/screens/10_museum/location/slab_06_shadow", 1238, 986);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slab_06", "graphics/screens/10_museum/location/slab_06_part", 1267, 1006);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slab_05", "graphics/screens/10_museum/location/slab_05_part", 1294, 740);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "slab_04_shadow", "graphics/screens/10_museum/location/slab_04_shadow", 940, 243);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slab_04", "graphics/screens/10_museum/location/slab_04_part", 958, 261);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "slab_03_shadow", "graphics/screens/10_museum/location/slab_03_shadow", 610, 410);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slab_03", "graphics/screens/10_museum/location/slab_03_part", 643, 436);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "slab_02_shadow", "graphics/screens/10_museum/location/slab_02_shadow", 346, 558);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slab_02", "graphics/screens/10_museum/location/slab_02_part", 367, 575);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "slab_01_shadow", "graphics/screens/10_museum/location/slab_01_shadow", 763, 711);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slab_01", "graphics/screens/10_museum/location/slab_01_part", 759, 723);
    ----------------------------------------]]
    
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "door_open", "graphics/screens/10_museum/location/door_open", 693, 197);
    self:CreateObj(OBJ_TYPE.NO_ACTIVE, "door_cover", "graphics/screens/10_museum/location/door_cover", 791, 187);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "scarab", "graphics/screens/10_museum/location/scarab_part", 942, 138);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "arrow", "graphics/screens/10_museum/location/arrow_part", 670, 742);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "oil_lamp", "graphics/screens/10_museum/location/oil_lamp_part", 999, 131);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "sceptre", "graphics/screens/10_museum/location/sceptre_part", 634, 91);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "note_in_the_table", "graphics/screens/10_museum/location/note_10_part", 32, 364);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "note_10", "graphics/screens/10_museum/location/note_10_part", 32, 364);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "mask", "graphics/screens/10_museum/location/mask_part", 1388, 722);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "translator", "graphics/screens/10_museum/location/translator_part", 300, 984);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "crown", "graphics/screens/10_museum/location/crown_part", 1067, 718);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "vase", "graphics/screens/10_museum/location/vase_part", 1252, 14);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "hand_mirror", "graphics/screens/10_museum/location/hand_mirror_part", 311, 97);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "figurine", "graphics/screens/10_museum/location/figurine_part", 1239, 917);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "tankard", "graphics/screens/10_museum/location/tankard_part", 1254, 732);
     
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "crystal_10", "graphics/screens/10_museum/location/crystal_10_part", 1034, 890);
    
    ItemSetOpacity("door_open", 0.0)
    clScreenManager:add_screen("hud") 
end