require("scripts/framework/Tools")

local MAX_INVENTORY_COUNT = 5

function GameScreen:InitScreen()
    if(self.state ~= "already_load") then --запрещаем перезагрузку от уровня к уровню
        self.state = "already_load"
        self:LoadScreen()

        self.tools = self.tools or {}

        for obj_name in string.gmatch(GetState("INVENTORY", "tools"), "[_%a%d]+") do
    		self:add_tool(obj_name)
        end 
        
        self:UpdateData()  
    else
        ItemSetDepth(self.name, LAYERS.FRONT_MOST)
    end
end

function GameScreen:OnClick(item_name)
    if(clApplication.game_pause) then return end
    --if(DragObject.active ) then return end 
    
    if(self.drag_clue) then return end
    
    if(item_name == "skip" or item_name == "hint" or item_name == "menu_button") then return end
    
    if(self.drag_obj) then
        DelayedExec(0.1,function()
            if(not self.drag_obj) then return end
            if(not self.lock_drag) then
                ItemSetOpacity(self.drag_obj.icon, 1.0)
                DragObject.active = false;
                ItemAttachChild(self.name, self.drag_obj.icon)
                self:ReturnObject();
                clApplication.lock_cursor = false;
            else
                self.lock_drag = false;
            end
        end)
        return true;
    elseif(not DragObject.active) then
        for i = 1, MAX_INVENTORY_COUNT do
            if(item_name == "slot_"..i) then
                DragObject.active = true;
                SetVisibleCursor(false)
                clApplication.lock_cursor = true;
                self.drag_obj = {}
                self.drag_obj.icon = "obj_"..i;
                DragObject:AttachToDragObj(self.drag_obj.icon)
                self.drag_obj.slot = i;
                self.drag_obj.tools = self.tools[i];
                --ItemSetDepth("obj_"..i, LAYERS.FRONT_MOST);
                if(CurrentScreen.inv_fun and  CurrentScreen.inv_fun[self.tools[i].name]) then
                    CurrentScreen.inv_fun[self.tools[i].name]();
                end
                return true;
            end
        end
    end
    
    return false;
end

function GameScreen:ClueAndHide()
    self.drag_clue = true
    ItemSetOpacity(self.drag_obj.icon, 0.0)
end

function GameScreen:IsFound(tool_name)
    for inv_item in string.gmatch(GetState("INVENTORY", "tools_found_all"), "[_%a%d]+") do
        if(inv_item == tool_name) then
            return true
        end
    end
    --[[for key, value in pairs(self.tools) do
        if(value.name == tool_name) then
            return true;
        end
    end  --]]
    
    return false;
end

function GameScreen:GetActiveObj()
    if(self.drag_obj and self.drag_obj.tools) then
        return self.drag_obj.tools.name;
    end
    
    return nil;
end

function GameScreen:ActiveToolApply()
    if(self.drag_obj) then
        DragObject.active = false;
        clApplication.lock_cursor = false;
        self.drag_clue = false
        SetVisibleCursor(true)
        self:remove_tool(self.drag_obj.tools.name)
        self.drag_obj = nil
    end    
end

function GameScreen:ActiveToolDrop()
    if(self.drag_obj) then
        DragObject.active = false;
        clApplication.lock_cursor = false;
        self.drag_clue = false
        SetVisibleCursor(true)
        self.drag_obj = nil
    end    
end

function GameScreen:ToolFound(obj_name, pos_x, pos_y, time)
    self:add_tool(obj_name)
    local key = self:get_tool_slot_by_name(obj_name)
    time = time or 0.3
            
    --ItemSetPosition("obj_"..key, pos_x, pos_y)
    ItemSetPosition("obj_"..key, pos_x - ItemGetWidth("obj_"..key) / 2, pos_y - ItemGetHeight("obj_"..key) / 2)
    ItemMoveTo("obj_"..key, 82 - ItemGetWidth("obj_"..key) / 2, 6 + 146 * key - 70 - ItemGetHeight("obj_"..key) / 2, time)
end

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

function GameScreen:TakeObject(obj_name)
end

function GameScreen:ReturnObject()
    SetVisibleCursor(true)
    SoundPlay("sound/interface/wrong.ogg")
    ItemMoveTo(self.drag_obj.icon, 82 - ItemGetWidth("obj_"..self.drag_obj.slot) / 2, 6 + 146 * self.drag_obj.slot - 70 - ItemGetHeight("obj_"..self.drag_obj.slot) / 2, 0.3);
    self.drag_obj = nil
end

function GameScreen:Update()
    if(clApplication.game_pause) then return end
    
    if(self.drag_obj) then
        ItemSetPosition(self.drag_obj.icon, messenger_table.mouse_X - ItemGetWidth(self.drag_obj.icon) / 2, messenger_table.mouse_Y - ItemGetHeight(self.drag_obj.icon) / 2);
    end
end

function GameScreen:add_tool(tool_name, no_update)
    local tool = get_tool_by_name(tool_name);
    if(tool and not self:is_exist(tool)) then--исключаем повторения
        table.insert(self.tools, tool);
    end
    self:save_tool(tool_name);
     
    if(no_update ~= true) then 
        self:UpdateData();
    end
end

function GameScreen:is_exist(tool)
    for key, value in pairs(self.tools) do
        if(tool.name == value.name) then
            return true;
        end
    end
    
    return false;
end

function GameScreen:save_tool(tool_name)
    for inv_item in string.gmatch(GetState("INVENTORY", "tools"), "[_%a%d]+") do
        if(inv_item == tool_name) then
            return;
        end
    end 
    SetState("INVENTORY", "tools", GetState("INVENTORY", "tools").." "..tool_name)
    
    for inv_item in string.gmatch(GetState("INVENTORY", "tools_found_all"), "[_%a%d]+") do
        if(inv_item == tool_name) then
            return;
        end
    end 
    SetState("INVENTORY", "tools_found_all", GetState("INVENTORY", "tools_found_all").." "..tool_name)
end

function GameScreen:remove_tool(tool_name)
    for key, value in pairs(self.tools) do
        if(value.name == tool_name) then
            table.remove(self.tools, key)
            local str_tools = ""
            for inv_item in string.gmatch(GetState("INVENTORY", "tools"), "[_%a%d]+") do
                if(inv_item ~= tool_name) then
                    str_tools = str_tools.." "..inv_item;
                end
            end 
            SetState("INVENTORY", "tools", str_tools)
            break;
        end
    end  

    self:UpdateData();  
end

function GameScreen:ClearData()
    for i = 1, MAX_INVENTORY_COUNT do
        ItemRemove(self.name, "slot_"..i);
        ItemRemove(self.name, "obj_"..i);
    end
    self.tools = nil;
end

function GameScreen:UpdateData()
    for i = 1, MAX_INVENTORY_COUNT do
        ItemRemove(self.name, "slot_"..i);
        ItemRemove(self.name, "obj_"..i);
    end
    
    if(self.tools and #self.tools > 0) then
        for key, value in pairs(self.tools) do
            if(key <= MAX_INVENTORY_COUNT) then
                self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slot_"..key, "graphics/interface/hud/inventory_01", 13, 15 + 146 * (key - 1));
                self:CreateObj(OBJ_TYPE.NO_ACTIVE, "obj_"..key, value.icon, 0, 0);
                ItemSetPosition("obj_"..key, 82 - ItemGetWidth("obj_"..key) / 2, 6 + 146 * key - 70 - ItemGetHeight("obj_"..key) / 2)
            end
        end
    end
end
-------------------------------------------------------------------------------------
function GameScreen:LoadScreen()
end
-------------------------------------------------------------------------------------
function GameScreen:ShowToolHint(tool_name)
    local slot = self:get_tool_slot_by_name(tool_name);
    if(slot) then
        self:CreateObj(OBJ_TYPE.ACTIVE_ALL, "slot_hint", "graphics/interface/hud/inventory_hint_01", 0, 0 + 146 * (slot - 1));
        EffectorPlay(item_hint_eff, "slot_hint", false)
        DelayedExec(3.0, function()
            self:RemoveItem("slot_hint", 0.3)
        end)  
    end
end

function GameScreen:RemoveScreen()
    cpp_application_remove_window("inventory");
end