GameScreen.textures_path = "graphics/minigames/12_signs/"


function GameScreen:RestoreScene()
    for key, value in pairs(self.plates) do
        if(GetState(CURRENT_SCREEN, value[1]) == "1") then
            ItemRemove(self.name, value[1])
        end
    end
    for key, value in pairs(self.places) do
        if(GetState(CURRENT_SCREEN, value[1]) == "1") then
            ItemSetOpacity(value[1], 1.0)
        else
            ItemSetOpacity(value[1], 0.0)
        end
    end
end

function GameScreen:InitScreen()
    self:LoadScreen()

    self:__translator_init();
    self:__plates_init();
    self:RestoreScene();
    
    HUDScreen:SetMiniGameMode()
    
    Inventory:remove_tool("translator_010")
    
    MusicPlay({"sound/ambient/12_ambient.ogg"}, 12)
    HUDScreen:CreateBackButton("012_fresco") 
end  
------------------------------------------------------------------------------------------------------
---------------------------------------------------Plates---------------------------------------------
------------------------------------------------------------------------------------------------------ 
function GameScreen:__plates_init()
    self.plates = {{"we_02_plate", 1}, {"from_plate", 2}, {"most_plate", 3}, {"need_plate", 4}, {"the_earth_plate", 5},
                     {"to_01_plate", 6}, {"came_plate", 7}, {"we_01_plate", 1}, {"take_plate", 8}, {"to_02_plate", 6}, 
                    {"people_plate", 9}, {"what_plate", 10},}
                    
    self.places = {{"most_insert", 3}, {"need_insert", 4}, {"we_02_insert", 1}, {"what_insert", 10}, {"people_insert", 9},
                     {"from_insert", 2}, {"take_insert", 8}, {"to_02_insert", 6}, {"the_earth_insert", 5}, {"to_01_insert", 6}, 
                    {"came_insert", 7}, {"we_01_insert", 1},}    
         
end
------------------------------------------------------------------------------------------------------ 
function GameScreen:__take_plate(item_name)
    
    for key, value in pairs(self.plates) do
        if(value[1] == item_name) then
            SoundPlay("sound/piece_take.ogg", false)
            local plate = {}
            plate.name = value[1];
            plate.type = value[2]
            plate.start_pos = {x = ItemGetX(plate.name), y = ItemGetY(plate.name)}
            ItemSetType(plate.name, OBJ_TYPE.NO_ACTIVE)
            SetVisibleCursor(false)
            ItemAttachChild("drag_trigger_img", plate.name)
            DragObject.active = true
            return plate;
        end
    end
    
    return nil
end
------------------------------------------------------------------------------------------------------ 
function GameScreen:__get_place_type_by_name(place_name)
    for key, value in pairs(self.places) do
        if(value[1] == place_name) then
            return value[2];
        end
    end
    
    return -1;
end
------------------------------------------------------------------------------------------------------ 
function GameScreen:__apply_plate(place_name)
    local type = self:__get_place_type_by_name(place_name)
    
    SetVisibleCursor(true)
    ItemAttachChild(self.name, self.drag_plate.name)
    DragObject.active = false
    if(type == self.drag_plate.type) then
        SoundPlay("sound/piece_insert.ogg", false)
        ItemRemove(self.name, self.drag_plate.name, 0.3)
        ItemChangeOpacity(place_name, 1.0, 0.3)
        SetState(CURRENT_SCREEN, place_name, "1")
        SetState(CURRENT_SCREEN, self.drag_plate.name, "1")
        DelayedExec(1.0, function()
            if(self:__is_all_plate_states()) then
                SetState("screen_012_fresco", "state", "after_mg")
                clApplication:LoadLocation("012_fresco")
            end
        end, 113)
    else
        SoundPlay("sound/interface/wrong.ogg", false)
        ItemMoveTo(self.drag_plate.name, self.drag_plate.start_pos.x, self.drag_plate.start_pos.y, 0.5)
        ItemSetType(self.drag_plate.name, OBJ_TYPE.ACTIVE_ALL)
    end
    
    self.drag_plate = nil
end
------------------------------------------------------------------------------------------------------ 
function GameScreen:__is_all_plate_states()
    for key, value in pairs(self.places) do
        if(GetState(CURRENT_SCREEN, value[1]) ~= "1") then
            return false;
        end
    end
    
    return true
end
------------------------------------------------------------------------------------------------------
------------------------------------------------Translator--------------------------------------------
------------------------------------------------------------------------------------------------------
function GameScreen:__translator_init()
    self.translator = {}
    self.translator.worlds = {"came", "what", "most", "we", "take", "to", "people", "from", "need", "the_earth" }
    self.translator.state = "off"
    self.translator.active = true;
    self.translator.current_world = 1;
    self.translator.worlds_amount = 10;
    
    for key, value in pairs(self.translator.worlds) do
        ItemSetOpacity(value, 0.0)
    end
    ItemSetOpacity("display", 0.0)
end
------------------------------------------------------------------------------------------------------
function GameScreen:__translator_on_off()
    if(self.translator.active == false) then return end
    ItemChangeOpacity("on_off", 0.0, 0.2)
    ItemChangeOpacity("on_off", 1.0, 0.2, 0.2)
    
    SoundPlay("sound/fuse_insert.ogg", false)
    
    if(self.translator.state == "off") then
        self.translator.state = "on"
        ItemChangeOpacity("display", 1.0, 1.0)
        ItemChangeOpacity(self.translator.worlds[self.translator.current_world], 1.0, 0.5, 0.5)
        self.translator.active = false
        DelayedExec(1.0, function()
            self.translator.active = true
        end, 113)
    else
        self.translator.state = "off"
        ItemChangeOpacity("display", 0.0, 1.0)
        ItemChangeOpacity(self.translator.worlds[self.translator.current_world], 0.0, 0.5)
        self.translator.active = false
        DelayedExec(1.0, function()
            self.translator.active = true
        end, 113)
    end
end
--[[
------------------------------------------------------------------------------------------------------
function GameScreen:__translator_other_symbol(flag)
    if(self.translator.active == false) then return end
    if(self.translator.state == "off") then return end
    
    if(flag == 1) then
        ItemChangeOpacity("down", 0.0, 0.2)
        ItemChangeOpacity("down", 1.0, 0.2, 0.2)
    else
        ItemChangeOpacity("up", 0.0, 0.2)
        ItemChangeOpacity("up", 1.0, 0.2, 0.2)
    end
    
    SoundPlay("sound/fuse_insert.ogg", false)
    ItemChangeOpacity(self.translator.worlds[self.translator.current_world], 0.0, 0.5)
    
    local rand = 0;
    
    repeat
        rand = math.random(#self.translator.worlds[self.translator.current_world])
    until self.translator.current_world ~= rand
    
    self.translator.current_world = rand

    DelayedExec(0.5, function()
        ItemChangeOpacity(self.translator.worlds[self.translator.current_world], 1.0, 0.5)
    end, 113)
    self.translator.active = false
    DelayedExec(1.0, function()
        self.translator.active = true
    end, 113)
end--]]

------------------------------------------------------------------------------------------------------
function GameScreen:__translator_down()
    if(self.translator.active == false) then return end
    if(self.translator.state == "off") then return end
    
    ItemChangeOpacity("down", 0.0, 0.2)
    ItemChangeOpacity("down", 1.0, 0.2, 0.2)
    SoundPlay("sound/fuse_insert.ogg", false)
    
    ItemChangeOpacity(self.translator.worlds[self.translator.current_world], 0.0, 0.5)
    self.translator.current_world = self.translator.current_world - 1
    if(self.translator.current_world < 1) then self.translator.current_world = self.translator.worlds_amount end
    DelayedExec(0.5, function()
        ItemChangeOpacity(self.translator.worlds[self.translator.current_world], 1.0, 0.5)
    end, 113)
    self.translator.active = false
    DelayedExec(1.0, function()
        self.translator.active = true
    end, 113)
end
------------------------------------------------------------------------------------------------------
function GameScreen:__translator_up()
    if(self.translator.active == false) then return end
    if(self.translator.state == "off") then return end
    
    ItemChangeOpacity("up", 0.0, 0.2)
        ItemChangeOpacity("up", 1.0, 0.2, 0.2)
    SoundPlay("sound/fuse_insert.ogg", false)
    
    ItemChangeOpacity(self.translator.worlds[self.translator.current_world], 0.0, 0.5)
    self.translator.current_world = self.translator.current_world + 1
    if(self.translator.current_world > self.translator.worlds_amount) then self.translator.current_world = 1 end
    DelayedExec(0.5, function()
        ItemChangeOpacity(self.translator.worlds[self.translator.current_world], 1.0, 0.5)
    end, 113)
    self.translator.active = false
    DelayedExec(1.0, function()
        self.translator.active = true
    end, 113)
end
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
function GameScreen:onObjectClick(item_name)
    if(not DragObject.active) then
        if(item_name == "on_off") then
            self:__translator_on_off();
        end
        if(item_name == "down") then
            self:__translator_down(1);
        end
        if(item_name == "up") then
            self:__translator_up(2);
        end
    end
    
    if(self.drag_plate == nil) then
        self.drag_plate = self:__take_plate(item_name);
    else
        self:__apply_plate(item_name);
    end
end

------------------------------------------------------------------------------------------------------
function GameScreen:OnSkip()
    for key, value in pairs(self.plates) do
        if(GetState(CURRENT_SCREEN, value[1]) ~= "1") then
            ItemRemove(self.name, value[1], 0.4)
        end
    end
    for key, value in pairs(self.places) do
        if(GetState(CURRENT_SCREEN, value[1]) ~= "1") then
            ItemChangeOpacity(value[1], 1.0, 0.5)
        end
    end
       
    SetState("screen_012_fresco", "state", "after_mg")
    DelayedExec(1.0, function() 
        clApplication:LoadLocation("012_fresco")
    end, 113)
end
------------------------------------------------------------------------------------------------------
function GameScreen:Update()
    if(self.drag_plate) then
        ItemSetPosition(self.drag_plate.name, messenger_table.mouse_X - ItemGetWidth(self.drag_plate.name) / 2, messenger_table.mouse_Y - ItemGetHeight(self.drag_plate.name) / 2);
    end 
end
------------------------------------------------------------------------------------------------------
-------------------------------------------Mouse functions--------------------------------------------
------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------
function GameScreen:LoadScreen()
	--[[---Background---]]--
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "12_fresco", "graphics/minigames/12_signs/12_signs", 0, 0);
	
	
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "most_insert", "graphics/minigames/12_signs/most_insert", 1314, 814);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "need_insert", "graphics/minigames/12_signs/need_insert", 1068, 813);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "we_02_insert", "graphics/minigames/12_signs/we_02_insert", 824, 813);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "what_insert", "graphics/minigames/12_signs/what_insert", 1314, 601);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "people_insert", "graphics/minigames/12_signs/people_insert", 1069, 601);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "from_insert", "graphics/minigames/12_signs/from_insert", 824, 600);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "take_insert", "graphics/minigames/12_signs/take_insert", 1315, 391);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "to_02_insert", "graphics/minigames/12_signs/to_02_insert", 1070, 389);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "the_earth_insert", "graphics/minigames/12_signs/the_earth_insert", 826, 391);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "to_01_insert", "graphics/minigames/12_signs/to_01_insert", 1315, 185);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "came_insert", "graphics/minigames/12_signs/came_insert", 1068, 182);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "we_01_insert", "graphics/minigames/12_signs/we_01_insert", 825, 184);
	
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "we_02_plate", "graphics/minigames/12_signs/we_02_plate", 535, 1048);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "from_plate", "graphics/minigames/12_signs/from_plate", 358, 1051);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "most_plate", "graphics/minigames/12_signs/most_plate", 188, 1044);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "need_plate", "graphics/minigames/12_signs/need_plate", 10, 1046);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "the_earth_plate", "graphics/minigames/12_signs/the_earth_plate", 543, 913);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "to_01_plate", "graphics/minigames/12_signs/to_01_plate", 369, 916);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "came_plate", "graphics/minigames/12_signs/came_plate", 192, 909);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "we_01_plate", "graphics/minigames/12_signs/we_01_plate", 23, 911);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "take_plate", "graphics/minigames/12_signs/take_plate", 547, 773);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "to_02_plate", "graphics/minigames/12_signs/to_02_plate", 371, 775);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "people_plate", "graphics/minigames/12_signs/people_plate", 192, 780);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "what_plate", "graphics/minigames/12_signs/what_plate", 19, 781);
	
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "display", "graphics/minigames/12_signs/display", 348, 416);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "on_off", "graphics/minigames/12_signs/on_off", 320, 662);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "up", "graphics/minigames/12_signs/up", 437, 576);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "down", "graphics/minigames/12_signs/down", 309, 575);
	
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "we", "graphics/minigames/12_signs/we", 408, 425);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "came", "graphics/minigames/12_signs/came", 408, 423);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "to", "graphics/minigames/12_signs/to", 393, 432);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "the_earth", "graphics/minigames/12_signs/the_earth", 356, 431);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "take", "graphics/minigames/12_signs/take", 389, 425);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "from", "graphics/minigames/12_signs/from", 408, 431);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "people", "graphics/minigames/12_signs/people", 387, 434);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "what", "graphics/minigames/12_signs/what", 398, 426);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "need", "graphics/minigames/12_signs/need", 394, 431);
	self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "most", "graphics/minigames/12_signs/most", 408, 424);

	clScreenManager:add_screen("hud") 
end