Render.OutlineStringCustom = function(x, y, alignid, text, color, font) {
    Render.StringCustom(x - 1, y - 1, alignid, text, [0, 0, 0, color[3]], font);
    Render.StringCustom(x - 1, y, alignid, text, [0, 0, 0, color[3]], font); 

    Render.StringCustom(x - 1, y + 1, alignid, text, [0, 0, 0, color[3]], font);   
    Render.StringCustom(x, y + 1, alignid, text, [0, 0, 0, color[3]], font);

    Render.StringCustom(x, y - 1, alignid, text, [0, 0, 0, color[3]], font);
    Render.StringCustom(x + 1, y - 1, alignid, text, [0, 0, 0, color[3]], font);

    Render.StringCustom(x + 1, y, alignid, text, [0, 0, 0, color[3]], font);
    Render.StringCustom(x + 1, y + 1, alignid, text, [0, 0, 0, color[3]], font);

    Render.StringCustom(x, y, alignid, text, color, font);
}

var ping_color = function(ping_value) {
    if(ping_value < 40) { return [255, 255, 255] }
    if(ping_value < 100) { return [255, 125, 95] }

    return [255, 60, 80]
}

var ping_info = []
var fps_info = []
var last_time = Global.Curtime()

var scx = Render.GetScreenSize()[0]
var scy = Render.GetScreenSize()[1]

var LC_ALPHA = 1

function draw() {
    var font = Render.AddFont("Verdana", 7, 400)
    var icon = Render.AddFont("Danger", 28, 400)
    var font_b = Render.AddFont("Verdana", 11, 400)

    var alpha = Math.min(Math.floor(Math.sin((Globals.Realtime()%3) * 4) * 125 + 200), 255)
    var color = [255, 200, 95, 255]

    var net_state = 0
    var net_data_text = {
        [0] : "clock syncing",
        [1] : "packet choke",
        [2] : "packet loss",
        [3] : "lost connection"
    }

    if (Global.Curtime() - last_time > 0.5) {
        last_time = Global.Curtime();
        ping_info.unshift(Global.Latency())
        fps_info.unshift(Global.Frametime())
    };

    if (ping_info.length > 1) {
        ping_info.pop()
    };

    if (fps_info.length > 1) {
        fps_info.pop()
    };

    var server_framerate = fps_info
    var server_vars = ping_info

    if(server_vars > 1) { net_state = 2 }

    if(net_state != 0) {
        color = [255, 50, 50, alpha]
    }
    
    var x = scx / 2 + 1
    var y = scy - 155

    var ccor_text = net_data_text[net_state]
    var ccor_width = Render.TextSizeCustom(ccor_text, font)

    var sp_x = x - ccor_width[0] - 25
    var sp_y = y

    var cn = 1

    LC_ALPHA = LC_ALPHA + (Globals.Frametime() * 2)
    LC_ALPHA = LC_ALPHA > 1 && 1 || LC_ALPHA 

    Render.OutlineStringCustom(sp_x, sp_y, 0, ccor_text, [255, 255, 255, alpha], font)
    Render.StringCustom(x - 17, sp_y - 14, 0, "0", [color[0], color[1], color[2], color[3]], icon)
    Render.StringCustom(x - 6, sp_y - 8, 0, "!", [color[0], color[1], color[2], color[3]], font_b)
    Render.OutlineStringCustom(x + 20, sp_y, 0, "+-" + (server_vars / 1).toFixed(1) + "ms", [255, 255, 255, 255], font)

    var bytes_in_text = "in: 16.01k/s    "
    var bi_width = Render.TextSizeCustom(bytes_in_text, font)

    var sent_bytes_in_text = "out: 5.02k/s"
    var sb_width = Render.TextSizeCustom(sent_bytes_in_text, font)

    var tickrate = 1 / Globals.TickInterval()
    var lerp_time = Convar.GetFloat("cl_interp_ratio") * (1000 / tickrate)

    var lerp_clr = [255, 255, 255]

    if (lerp_time / 1000 < 2 / Convar.GetInt("cl_updaterate")) {
        lerp_clr = [255, 125, 95]
    }

    Render.OutlineStringCustom(sp_x, sp_y + 20*cn, 0, bytes_in_text, [255, 255, 255, LC_ALPHA * 255], font) 
    Render.OutlineStringCustom(sp_x + bi_width[0], sp_y + 20*cn, 0, "lerp: " + lerp_time.toFixed(1) + " ms", [lerp_clr[0], lerp_clr[1], lerp_clr[2], LC_ALPHA * 255], font) 
    cn = cn + 1
    Render.OutlineStringCustom(sp_x, sp_y + 20*cn, 0, sent_bytes_in_text, [255, 255, 255, LC_ALPHA * 255], font) 
    cn = cn + 1
    Render.OutlineStringCustom(sp_x, sp_y + 20*cn, 0, "sv: " + (server_framerate / 1).toFixed(2) + " +- " + (server_vars / 1).toFixed(2) + "mc    var: " + (server_vars / 1).toFixed(3) + " mc", [255, 255, 255, LC_ALPHA * 255], font) 
    cn = cn + 1

    var ping = Math.round(Entity.GetProp(Entity.GetLocalPlayer(), "CPlayerResource", "m_iPing")).toString()
    var avg_ping = ping

    var pc = ping_color(avg_ping)

    var latency_interval = (ping) / (Globals.TickInterval())
    var additional_latency = Math.min(latency_interval * 1000, 1) * 100 //-- 100 = green 0 = red

    var tr_text = "tick: " + tickrate + "p/s    "
    var tr_width = Render.TextSizeCustom(tr_text, font)

    var nd_text = "delay: " + ping.toString() + "ms (+- " + Math.abs(avg_ping - ping) + "ms)    "
    var nd_width = Render.TextSizeCustom(nd_text, font)

    var incoming_latency = Math.max(0, (server_vars + ping) * 1000)

    //Render.OutlineStringCustom(sp_x, sp_y + 20*cn, 0, tr_text, [pc[0], pc[1], pc[2], LC_ALPHA * 255], font) 
    Render.OutlineStringCustom(sp_x, sp_y + 20*cn, 0, nd_text, [pc[0], pc[1], pc[2], LC_ALPHA * 255], font) 
    Render.OutlineStringCustom(sp_x + nd_width[0], sp_y + 20*cn, 0, "datagram", [255, 255 / 100 * additional_latency, 255 / 100 * additional_latency, LC_ALPHA * 255], font) 

    var lc_state = 0
    var lc_variants = {
        [0] : "UNSAFE", 
        [1] : "LAG COMP BREAKER",
        [2] : "SHIFTING TICKBASE",
    }

    var lc_text = "lagcomp: "
    var lc_width = Render.TextSizeCustom(lc_text, font)

    var lc_color = [255, 45, 45, 255]

    if(UI.IsHotkeyActive("Rage", "Exploits", "Hide shots")) { lc_state = 1, lc_color = [255, 125, 95, 255] }
    if(UI.IsHotkeyActive("Rage", "Exploits", "Doubletap")) { lc_state = 2, lc_color = [42, 250, 87, 255] }
    if(Exploit.GetCharge() != 1) { lc_state = 0, lc_color = [255, 45, 45, 255] }

    Render.OutlineStringCustom((scx / 2) - 60, sp_y + 28*cn, 0, lc_text, [255, 255, 255, 255], font)
    Render.OutlineStringCustom((scx / 2) - 60 + lc_width[0], sp_y + 28*cn, 0, lc_variants[lc_state], lc_color, font) 
}
Cheat.RegisterCallback("Draw", "draw")