AddCSLuaFile()

ENT.Base = "base_gmodentity"
ENT.Type = "anim"
ENT.PrintName = "Loot"
if SERVER then
    function ENT:Initialize()
        if not self.LootType and not self.Weapon then
            self:Remove() 
            return 
        end

        local type_set = 2
        local tab = LootItems[self.LootType]

        if istable(self.Weapon) then
            tab = self.Weapon
            self:SetNWBool('Weapon', true)

            local cl = tab.class
            if cl then
                local t = SuR.Config.TierWeapons
                if table.HasValue(t["Tier1"], cl) then
                    type_set = 1
                elseif table.HasValue(t["Tier3"], cl) or table.HasValue(t["Other"], cl) then
                    type_set = 3
                elseif table.HasValue(t["Tier4"], cl) then
                    type_set = 4
                elseif table.HasValue(t["Tier5"], cl) then
                    type_set = 5
                end
            end
        end

        if tab.tier then
            type_set = tab.tier
        end

        self.FuncUse = tab.usefunc

        self:SetNWFloat("Type", type_set)
        self:SetNWString("LootName", tab.name)
        self:SetModel(tab.model)
        self:PhysicsInit(SOLID_VPHYSICS)
        self:SetMoveType(MOVETYPE_VPHYSICS)
        self:SetUseType(SIMPLE_USE)
        self:SetCollisionGroup(COLLISION_GROUP_DEBRIS_TRIGGER)
        local phys = self:GetPhysicsObject()
        if IsValid(phys) then
            phys:EnableMotion(true)
            phys:Wake()
        end

        timer.Simple(SuR.Config.Loot_Remove_Delay, function()
            if !IsValid(self) or self.NoRemove then return end

            self:Remove()
        end)
    end

    function ENT:Use(ply)
        if not istable(self.Weapon) then
            if !IsValid(ply.LastPickupWeapon) then
                ply.LastPickupWeapon = ply:GetActiveWeapon()
            end
            ply:SetActiveWeapon(nil)
            timer.Create("PlyTakeAnim"..ply:EntIndex(), 1.5, 1, function()
                if !IsValid(ply) or !IsValid(ply.LastPickupWeapon) then return end

                local class = ply.LastPickupWeapon:GetClass()
                ply:SelectWeapon(class)
                ply.LastPickupWeapon = nil
            end)
        end
        if istable(self.Weapon) then
            if self.Weapon.class and ply:HasWeapon(self.Weapon.class) then return end
            self.Weapon.usefunc(self, ply)
            if self.Weapon.class then
                ply:SelectWeapon(self.Weapon.class)
            end
            if self.Weapon.class == "sur_hammer" then
                SuR:ShowTips("hammer", ply)
            end
        else
            self.FuncUse(self, ply)
            SuR:ShowTips("items", ply)
        end

        local dif = self:GetPos().z-ply:GetPos().z
        if dif > 30 then
            ply:SetSVAnimation("sur_itemgrab_normal", true, true)
        elseif dif > 15 then
            ply:SetSVAnimation("sur_itemgrab_crouch", true, true)
        else
            ply:SetSVAnimation("sur_itemgrab_floor", true, true)
        end
        ply:DropButtons()
        ply:EmitSound("items/ammo_pickup.wav", 70)
        ply:PlayVoiceLine("pickup")
        self:Remove()
    end
else
    local arrow = Material('surrounded/arrow.png')
    hook.Add("PostDrawTranslucentRenderables", "LootShow", function()
        for _, self in ipairs(ents.FindByClass("sur_loot")) do
            local angle = EyeAngles()
            angle = Angle(angle.x, angle.y, 0)
            angle:RotateAroundAxis(angle:Up(), -90)
            angle:RotateAroundAxis(angle:Forward(), 90)
            
            cam.Start3D2D(self:GetPos()+Vector(0,0,24+(math.cos(CurTime())*2)), angle, 0.1)
                surface.SetMaterial(arrow)
                surface.SetDrawColor(255,255,255)
                surface.DrawTexturedRect(-32,16,64,64)

                local loot = self:GetNWString("LootName", "")
                local type = self:GetNWFloat("Type")
                local name = SuR.Language[loot]
                if not name then
                    name = loot
                end
                draw.SimpleText(name, "SuR_SmallFont2", 0, 0, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
                if type > 0 and SuR.Config.Tier_Colors[type] then
                    draw.SimpleText(SuR.Language["loot_tier"..type], "SuR_SmallFont1", 0, 20, SuR.Config.Tier_Colors[type], TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
                end
            cam.End3D2D()
        end
    end)
end
