hook.Add( "CalcView", "CalcView.MedicMod", function( ply, pos, ang, fov )
    if ( !IsValid( ply ) or !ply:Alive() or ply:GetViewEntity() != ply ) then return end

    if ply:GetMedicAnimation() != 0 then
        local view = {}
        view.origin = pos - ( ang:Forward()*20 )
        view.angles = ang
        view.fov = fov
        view.drawviewer = true
        return view
    end

    --[[
    if ply:GetNWBool("CarryingRagdoll", false) then
        local view = {}
        local tilted = Angle(ang.p, ang.y, ang.r)
        tilted:RotateAroundAxis(ang:Forward(), 18)
        view.origin = pos
        view.angles = tilted
        view.fov = fov
        view.drawviewer = false
        return view
    end
    ]]
end)

hook.Add("RenderScreenspaceEffects", "RenderScreenspaceEffects.MedicMod", function()
    if LocalPlayer():IsBleeding() then
        MedicMod.BleedingEffect()
    end
    if LocalPlayer():IsPoisoned() then
        MedicMod.PoisonEffect()
    end
end)
 
local bleedingIcon = Material("materials/bleeding.png")
local poisonedIcon = Material("materials/poisoned.png")
local hattackIcon = Material("materials/heart_attack_icon.png")
local morphIcon = Material("materials/morphine_icon.png")
local breakIcon = Material("materials/break_icon.png")
local notifIcon = Material("materials/heart_attack_icon.png")
  
local deathPanel = nil
 
hook.Add("HUDPaint", "HUDPaint.MedicMod", function()
   
    if ConfigurationMedicMod.MedicTeams and table.HasValue(ConfigurationMedicMod.MedicTeams, LocalPlayer():Team()) then
        for k, v in pairs(ents.FindByClass("prop_ragdoll")) do
           
            if not v:IsDeathRagdoll() then continue end        
           
            local pos = ( v:GetPos() + Vector(0,0,10) ):ToScreen()
            local dist = v:GetPos():Distance(LocalPlayer():GetPos())
           
            surface.SetDrawColor( 255, 255, 255, 255 )
            surface.SetMaterial( notifIcon )
            surface.DrawTexturedRect( pos.x - 25, pos.y, 50, 50 )
           
            draw.SimpleTextOutlined( math.floor(math.sqrt(dist/3)).."m", "MedicModFont30", pos.x, pos.y + 50, Color( 255, 255, 255, 255 ), TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP, 1, Color( 0, 0, 0, 255 ) )
   
        end
    end

    --[[
    local carryText = nil
    if LocalPlayer():GetNWBool("CarryingRagdoll", false) then
        carryText = ConfigurationMedicMod.Sentences["You dropped the corpse"][ConfigurationMedicMod.Language]
    else
        local trace = LocalPlayer():GetEyeTraceNoCursor()
        if IsValid(trace.Entity) and trace.Entity:IsDeathRagdoll() and not trace.Entity:GetNWBool("IsRagdollCarried", false) and trace.HitPos:Distance(LocalPlayer():GetPos()) <= 120 then
            carryText = ConfigurationMedicMod.Sentences["Press E to carry corpse"][ConfigurationMedicMod.Language]
        end
    end

    if carryText then
        draw.SimpleTextOutlined(carryText, "MedicModFont30", ScrW() / 2, ScrH() - 80, Color(255, 255, 0, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP, 2, Color(0, 0, 0, 255))
    end
    ]]

    local nbStat = 1
   
    if LocalPlayer():IsBleeding() then
        surface.SetDrawColor( 255, 255, 255, 255 )
        surface.SetMaterial( bleedingIcon )
        surface.DrawTexturedRect( ScrW() - 300, ScrH() - 150 - 50 * nbStat, 50, 50 )
       
        draw.SimpleTextOutlined( ConfigurationMedicMod.Sentences["Bleeding"][ConfigurationMedicMod.Language], "MedicModFont30", ScrW() - 240, ScrH() - 140 - 50 * nbStat, Color( 255, 0, 0, 255 ), TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP, 1, Color( 0, 0, 0, 255 ) )
        nbStat = nbStat + 1
    end
    if LocalPlayer():IsPoisoned() then
        surface.SetDrawColor( 255, 255, 255, 255 )
        surface.SetMaterial( poisonedIcon )
        surface.DrawTexturedRect( ScrW() - 300, ScrH()  - 150 - 50 * nbStat, 50, 50 )
       
        draw.SimpleTextOutlined( ConfigurationMedicMod.Sentences["Poisoned"][ConfigurationMedicMod.Language], "MedicModFont30", ScrW() - 240, ScrH() - 140 - 50 * nbStat, Color( 153, 201, 158, 255 ), TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP, 1, Color( 0, 0, 0, 255 ) )
        nbStat = nbStat + 1
    end
    if LocalPlayer():GetHeartAttack() then
        surface.SetDrawColor( 255, 255, 255, 255 )
        surface.SetMaterial( hattackIcon )
        surface.DrawTexturedRect( ScrW() - 300, ScrH()  - 150 - 50 * nbStat, 50, 50 )
       
        draw.SimpleTextOutlined( ConfigurationMedicMod.Sentences["Heart Attack"][ConfigurationMedicMod.Language], "MedicModFont30", ScrW() - 240, ScrH() - 140 - 50 * nbStat , Color( 255, 255, 255, 255 ), TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP, 1, Color( 0, 0, 0, 255 ) )
        nbStat = nbStat + 1
    end
    if LocalPlayer():IsMorphine() then
        surface.SetDrawColor( 255, 255, 255, 255 )
        surface.SetMaterial( morphIcon )
        surface.DrawTexturedRect( ScrW() - 300, ScrH()  - 150 - 50 * nbStat, 50, 50 )
       
        draw.SimpleTextOutlined( ConfigurationMedicMod.Sentences["Morphine"][ConfigurationMedicMod.Language], "MedicModFont30", ScrW() - 240, ScrH() - 140 - 50 * nbStat, Color( 255, 255, 255, 255 ), TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP, 1, Color( 0, 0, 0, 255 ) )
        nbStat = nbStat + 1
    end
    if LocalPlayer():IsFractured() then
        surface.SetDrawColor( 255, 255, 255, 255 )
        surface.SetMaterial( breakIcon )
        surface.DrawTexturedRect( ScrW() - 300, ScrH()  - 150 - 50 * nbStat, 50, 50 )
       
        draw.SimpleTextOutlined( ConfigurationMedicMod.Sentences["Fracture"][ConfigurationMedicMod.Language], "MedicModFont30", ScrW() - 240, ScrH() - 140 - 50 * nbStat, Color( 255, 255, 255, 255 ), TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP, 1, Color( 0, 0, 0, 255 ) )
        nbStat = nbStat + 1
    end
 
end)
