/*
    Note by paradox:
        This was made by Shabeel for automation on D3C

*/

--[[
    Add math for calculating how long it will take to pick up all the kg of garbage i think its like 1 kg every .2 seconds or smthing
    loop so once one garbage is empty move to next
    consistently check the garbage to see if its over 100kg before moving to the next trash thing
    finish keys for picking up the garbage try make universal not limited to smokes
    
]]

local net = D3C.require("net")
local entity = D3C.require("entity")
local player = D3C.require("player")
local automation = D3C.require("automation")
local collisions = D3C.require("collision") -- ray tracing idk if i use it
local nvar = D3C.require("nvar")
local engine = D3C.require("engine")
local input = D3C.require("input")
local localplayer = player.getLocal()
local distance = 10000000000
local closestpos = Vector(0,0,0)

local function GetPos(ent)
    return ent.position
end

local function getByClass(classname)
    return entity.getByClass(classname)
end

local function presskey(key, time1, time2)
    timer.Simple(time1, function()
        input.press(key)
    end)
    timer.Simple(time2, function()
        input.release(key)
    end)
end

for k, v in pairs(getByClass("ztm_manhole")) do
    local manholepos = GetPos(v)
    local currentDistance = GetPos(localplayer):Distance(manholepos)

    if currentDistance < distance and nvar.int(v, 0) > 10 then -- scrap pile checks if it has over 10k
        closestpos = manholepos
        distance = currentDistance
    end
end

local function calculateAngle(fromPos, toPos)
    local direction = (toPos - fromPos):GetNormalized()
    return direction:Angle()
end

if nvar.int(localplayer.weapon, 2) > 100 then -- check if trash weapon has over 100 kg worth of trash, i also do not know if thats the right int
    -- crazy logic for picking up trash bag
    presskey(MOUSE_MIDDLE,0, 0) -- drops
    presskey(KEY_LCONTROL,1, 0) -- crouch
    engine.command("use weapon_pistol") -- AHHHH holster thing
    presskey(MOUSE_LEFT,2, 1) -- pickup
end


local closestAngle = calculateAngle(GetPos(localplayer), closestpos) -- calculate the angle for later

automation:clear()
automation:add('CUserCmd.Position', GetPos(localplayer), 0) -- localplayers feet 
automation:add('CUserCmd.Position', closestpos, 0) -- position where the hole is
automation:add('CUserCmd.Angle', closestAngle, 0) -- set angle to where the hole is
automation:add('CUserCmd.Button', 32, 0) -- e
automation:add('CUserCmd.Button', nil, 0) -- release
automation:add('CUserCmd.Button', 2048, 0) -- rightclick?
automation:add('Nothing', nil, 10) -- hold rightclick for x seconds need to do math to see how long it takes to pickup depending on the kg
automation:add('CUserCmd.Angle', nil, 0) -- release angle
automation:add('CUserCmd.Button', nil, 0) -- release rightclick
automation:play()

--[[

    I cant remember why i did this but im too scared to remove it even tho its not used
    local function entity_information()
        return collisions.raycast(GetPos(localplayer), closestpos)
    end



    if entity_information().entity then
        local size = nvar.int(entity_information().entity, 0)
        print(size)
    end
]]