GameScreen.textures_path = "graphics/screens/25_utility/location/"

function GameScreen:RestoreScreen()
    if(GetState(CURRENT_SCREEN, "state") == "") then
        SetState(CURRENT_SCREEN, "state", "ho")
        self:StartHO()
    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
        clApplication:LoadLocation("026_dessert")
    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/25_utility/location/"..self.mg_finish_parts[key].."_full", self.finish_coord[key][1], self.finish_coord[key][2]);
            end
        end
    end 
end

function GameScreen:InitScreen()
    self:LoadScreen()
    
    self.mg_parts = {"plate_01",  "plate_02", "plate_03", "plate_04", "plate_05",
                        "plate_06",  "plate_07", "plate_08", "plate_09", "plate_10",}
                        
    self.mg_finish_parts = {"pile_01",  "pile_02", "pile_03", "pile_04", "pile_05",
                        "pile_06",  "pile_07", "pile_08", "pile_09", "pile_10",}
    
    self.finish_coord = {{448, 713}, {594, 739}, {967, 755}, {795, 812}, {565, 860}, 
                         {295, 893}, {997, 863}, {196, 1040}, {567, 1057}, {1232, 1056}, }
                            
    ------------------------------------------------------------------------------------------------------------------------------ 
    --OTHER       
    self:SetItemSound("tray", "sound/metal_tray.ogg");
    self:SetItemSound("jug", "sound/lamp_aroma.ogg");
    self:SetItemSound("metal_right", "sound/bedpan.ogg");
    self:SetItemSound("pot_black_small", "sound/metal_pot_25.ogg");
    self:SetItemSound("big_dish", "sound/green_pot.ogg");
    self:SetItemSound("pot_yellow", "sound/ceramic_pot_01.ogg");
    self:SetItemSound("bottle", "sound/bottle_08.ogg");
    self:SetItemSound("basin_big", "sound/metal_basin.ogg");
    self:SetItemSound("pot_upright", "sound/ceramic_pot.ogg");               
    
    MusicPlay({"sound/ambient/15_25_29_ambient.ogg", "sound/music/past.ogg"} , 25) 
    
    self:AddHiddenCrystal("crystal_25_part")
    
    self:RestoreScreen()
end
        
function GameScreen:StartHO()    
    self.item_list = {
        {obj = {"corkscrew", {x = 0, y = 215}}},
        {obj = {"strickle", {x = 1414, y = 823}}},
        {obj = {"coffee_pot", {x = 350, y = 669}}, },
        {obj = {"knife_05", {x = 510, y = 442}}, },
        {obj = {"knife_04", {x = 664, y = 934}}, },
        {obj = {"knife_03", {x = 678, y = 624}}, },
        {obj = {"knife_02", {x = 380, y = 449}}, },
        {obj = {"knife_01", {x = 1081, y = 927}}, },
        {obj = {"teapot", {x = 1122, y = 643}}, }, 
        {obj = {"drinking_horn", {x = 496, y = 590}}, },
        {obj = {"coffee_grinder", {x = 952, y = 637}}, },
        {obj = {"ladle", {x = 475, y = 808}}, },
        {obj = {"pepper_shaker_03", {x = 1226, y = 395}}},
        {obj = {"pepper_shaker_02", {x = 212, y = 266}}},
        {obj = {"pepper_shaker_01", {x = 940, y = 867}}, },
        {obj = {"fork_03", {x = 1260, y = 318}}, },
        {obj = {"fork_02", {x = 20, y = 355}}, },
        {obj = {"fork_01", {x = 899, y = 444}}, },
        {obj = {"spoon_03", {x = 1455, y = 547}}, },
        {obj = {"spoon_02", {x = 404, y = 1017}}, },
        {obj = {"spoon_01", {x = 1191, y = 1061}}, }, 
        {obj = {"glass_04", {x = 1124, y = 122}}, },
        {obj = {"glass_03", {x = 1411, y = 331}}, },
        {obj = {"glass_02", {x = 206, y = 401}}, },
        {obj = {"glass_01", {x = 968, y = 224}}, },
     }
    
    local on_finish = function()
        SetState(CURRENT_SCREEN, "state", "mg")
        self.mini_game = nil;
self:CreateObj(OBJ_TYPE.TRIGGER, "plate_10_affix", "graphics/screens/25_utility/location/pile_10_affix",1231, 1068);
    self:CreateObj(OBJ_TYPE.TRIGGER, "plate_07_affix", "graphics/screens/25_utility/location/pile_07_affix",999, 878);
    self:CreateObj(OBJ_TYPE.TRIGGER, "plate_05_affix", "graphics/screens/25_utility/location/pile_05_affix",571, 892);
    self:CreateObj(OBJ_TYPE.TRIGGER, "plate_09_affix", "graphics/screens/25_utility/location/pile_09_affix",573, 1075);
    self:CreateObj(OBJ_TYPE.TRIGGER, "plate_04_affix", "graphics/screens/25_utility/location/pile_04_affix",795, 817);
    self:CreateObj(OBJ_TYPE.TRIGGER, "plate_03_affix", "graphics/screens/25_utility/location/pile_03_affix",964, 765);
    self:CreateObj(OBJ_TYPE.TRIGGER, "plate_02_affix", "graphics/screens/25_utility/location/pile_02_affix",594, 743);
    self:CreateObj(OBJ_TYPE.TRIGGER, "plate_01_affix", "graphics/screens/25_utility/location/pile_01_affix",448, 732);
    self:CreateObj(OBJ_TYPE.TRIGGER, "plate_06_affix", "graphics/screens/25_utility/location/pile_06_affix",296, 917);
    self:CreateObj(OBJ_TYPE.TRIGGER, "plate_08_affix", "graphics/screens/25_utility/location/pile_08_affix",205, 1066);
        local tip_end = function()
            self:startMG();
        end
        DelayedExec(1.0, function()
            HUDScreen:ShowTips({path = self.textures_path.."Text_25", x = 328, y = 1112, end_fun = tip_end})
        end, 113)
    end       
    
                
    self:SetMiniGame(mgt_hidden_object)   
    self.mini_game.list_type = 2;
    self.mini_game:Init(self.item_list, on_finish);
end

------------------------------------------------------------------------------------------------------
function GameScreen:startMG()
    HUDScreen:SetMiniGameMode()
    self:CreateCounter();
    self.state = "mg"
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, 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: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: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:__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/"..self.mg_finish_parts[key].."_full", self.finish_coord[key][1], self.finish_coord[key][2]);
                end
            end
            
            DelayedExec(0.5, function()
                self:RemoveCounter();
            end, 113)
            SetState(CURRENT_SCREEN, "state", "finish")
            HUDScreen:SetDefaultMode()
            DelayedExec(2.0, function()
                clApplication:LoadLocation("026_dessert")
            end, 113)
            return;
        end
        
        if(self.drag_item) then
            if(item_name == self.mg_parts[self.drag_item.id].."_affix") 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/25_utility/location/"..self.mg_finish_parts[self.drag_item.id].."_full", self.finish_coord[self.drag_item.id][1], self.finish_coord[self.drag_item.id][2]);
                self:UpdateCounter()
                SoundPlay("sound/plate_insert.ogg")
                if(self:__is_mg_completed())then
                    DelayedExec(0.5, function()
                        self:RemoveCounter();
                    end, 113)
                    ItemChangeOpacity("door_open", 1.0, 0.5)
                    SetState(CURRENT_SCREEN, "state", "finish")
                    HUDScreen:SetDefaultMode()
                    DelayedExec(2.0, function()
                        clApplication:LoadLocation("026_dessert")
                    end, 113)
                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/plate_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/25_utility/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: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
------------------------------------------------------------------------------------------------------
function GameScreen:LoadScreen()
	--[[---Background---]]--
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "25_utility", "graphics/screens/25_utility/location/25_utility", 0, 0);
    
    self:CreateObj(OBJ_TYPE.TRIGGER, "tray", "graphics/screens/25_utility/location/tray", 821, 1000);
    self:CreateObj(OBJ_TYPE.TRIGGER, "jug", "graphics/screens/25_utility/location/jug", 1391, 903);
    self:CreateObj(OBJ_TYPE.TRIGGER, "metal_right", "graphics/screens/25_utility/location/metal_right", 1370, 726);
    self:CreateObj(OBJ_TYPE.TRIGGER, "pot_black_small", "graphics/screens/25_utility/location/pot_black_small", 1308, 155);
    self:CreateObj(OBJ_TYPE.TRIGGER, "big_dish", "graphics/screens/25_utility/location/big_dish", 713, 660);
    self:CreateObj(OBJ_TYPE.TRIGGER, "pot_yellow", "graphics/screens/25_utility/location/pot_yellow", 583, 629);
    self:CreateObj(OBJ_TYPE.TRIGGER, "bottle", "graphics/screens/25_utility/location/bottle", 50, 756);
    self:CreateObj(OBJ_TYPE.TRIGGER, "basin_big", "graphics/screens/25_utility/location/basin_big", 557, 286);
    self:CreateObj(OBJ_TYPE.TRIGGER, "pot_upright", "graphics/screens/25_utility/location/pot_upright", 0, 224);
  
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "plate_10", "graphics/screens/25_utility/location/plate_10_part", 121, 861);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "plate_09", "graphics/screens/25_utility/location/plate_09_part", 680, 385);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "plate_08", "graphics/screens/25_utility/location/plate_08_part", 1243, 943);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "plate_07", "graphics/screens/25_utility/location/plate_07_part", 533, 485);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "plate_06", "graphics/screens/25_utility/location/plate_06_part", 1118, 391);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "plate_05", "graphics/screens/25_utility/location/plate_05_part", 277, 798);  
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "plate_04", "graphics/screens/25_utility/location/plate_04_part", 377, 360);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "plate_03", "graphics/screens/25_utility/location/plate_03_part", 25, 67);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "plate_02", "graphics/screens/25_utility/location/plate_02_part", 1237, 554);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "plate_01", "graphics/screens/25_utility/location/plate_01_part", 118, 740);
    
  
    
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "strickle", "graphics/screens/25_utility/location/strickle_part", 1461, 845);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "corkscrew", "graphics/screens/25_utility/location/corkscrew_part", 17, 204);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "coffee_pot", "graphics/screens/25_utility/location/coffee_pot_part", 348, 746);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "knife_05", "graphics/screens/25_utility/location/knife_05_part", 582, 454);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "knife_04", "graphics/screens/25_utility/location/knife_04_part", 778, 948);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "knife_03", "graphics/screens/25_utility/location/knife_03_part", 733, 644);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "knife_02", "graphics/screens/25_utility/location/knife_02_part", 403, 475);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "knife_01_shadow", "graphics/screens/25_utility/location/knife_01_shadow", 1240, 1032);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "knife_01", "graphics/screens/25_utility/location/knife_01_part", 1239, 866);  
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "teapot", "graphics/screens/25_utility/location/teapot_part", 1183, 698);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "coffee_grinder", "graphics/screens/25_utility/location/coffee_grinder_part", 984, 679);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "drinking_horn", "graphics/screens/25_utility/location/drinking_horn_part", 535, 676); 
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "ladle", "graphics/screens/25_utility/location/ladle_part", 485, 823);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "pepper_shaker_03", "graphics/screens/25_utility/location/pepper_shaker_03_part", 1251, 458);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "pepper_shaker_02", "graphics/screens/25_utility/location/pepper_shaker_02_part", 231, 349);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "pepper_shaker_01", "graphics/screens/25_utility/location/pepper_shaker_01_part", 954, 884);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fork_03", "graphics/screens/25_utility/location/fork_03_part", 1296, 353);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fork_02", "graphics/screens/25_utility/location/fork_02_part", 27, 364);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "fork_01", "graphics/screens/25_utility/location/fork_01_part", 973, 480);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "spoon_03", "graphics/screens/25_utility/location/spoon_03_part", 1497, 590);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "spoon_02_shadow", "graphics/screens/25_utility/location/spoon_02_shadow", 350, 1096);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "spoon_02", "graphics/screens/25_utility/location/spoon_02_part", 364, 1083);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "spoon_01_shadow", "graphics/screens/25_utility/location/spoon_01_shadow", 1135, 1094);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "spoon_01", "graphics/screens/25_utility/location/spoon_01_part", 1177, 1156);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "glass_04", "graphics/screens/25_utility/location/glass_04_part", 1152, 165);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "glass_03", "graphics/screens/25_utility/location/glass_03_part", 1426, 347);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "glass_02", "graphics/screens/25_utility/location/glass_02_part", 226, 447);
    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "glass_01", "graphics/screens/25_utility/location/glass_01_part", 995, 282);
    
    

    self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "crystal_25_part", "graphics/screens/25_utility/location/crystal_25_part", 303, 770);
    
    clScreenManager:add_screen("hud")  
end