UI.AddMultiDropdown("indicators", ["watermark", "keybinds", "spectators"]);
UI.AddColorPicker("color");

const screen_size = Global.GetScreenSize();
var stored = false;
var x_offs = 0;
var y_offs = 0;

UI.AddSliderInt("binds x", 0, screen_size[0]);
UI.AddSliderInt("binds y", 0, screen_size[1]);
UI.AddSliderInt("spectators x", 0, screen_size[0]);
UI.AddSliderInt("spectators y", 0, screen_size[1]);
UI.SetEnabled("Script items", "binds x", true);
UI.SetEnabled("Script items", "binds y", true);
UI.SetEnabled("Script items", "spectators x", true);
UI.SetEnabled("Script items", "spectators y", true);
// there is a fucking stupid bug. Some lines above, it should be TRUE so you can change fucking pos of it ( was FALSE, and obv spec and bind are in the same pos. If youre reading this, pay attention to what youre coding. 
const get_dropdown_value = function(value, index){
    const mask = 1 << index;
    return value & mask ? true : false;
}

const in_bounds = function(vec, x, y, x2, y2) {
    return (vec[0] > x) && (vec[1] > y) && (vec[0] < x2) && (vec[1] < y2)
}

const watermark = function() {
    const user = Cheat.GetUsername();
    const ping = Math.round(Local.Latency( ) * 1000 - 16);
    const now = new Date();
    const hours = now.getHours(), mins = now.getMinutes(), secs = now.getSeconds();
    const time = (hours < 10 ? "0" + hours : hours) + ":" + (mins < 10 ? "0" + mins : mins) + ":" + (secs < 10 ? "0" + secs : secs);
    const font = Render.AddFont("Calibri", 10, 100);
    const color = UI.GetColor("Script items", "color");
    const text = "gamesense | "+ user +" | ";
    if (World.GetServerString() != ""){text += ""+ ping +" ms | " + Globals.Tickrate() + " tick | "; }
    text += time;
    const w = Render.TextSizeCustom(text, font)[0] + 10;
    const x = screen_size[0];
    x = x - w - 10;

    Render.FilledRect(x, 12, w, 18, [0, 0, 0, color[3]]);
    Render.StringCustom(x + 5, 13, 0, text, [255, 255, 255, 255], font);

    Render.GradientRect(x    , 10, w/2, 2, 2, [55, 177, 218/*55, 177, 218*/, 255], [203, 70, 205/*203, 70, 205*/, 255]);
    Render.GradientRect(x+ 98, 10, w/2, 2, 2, [203, 70, 205/*203, 70, 205*/, 255], [204, 227, 53/*204, 227, 53*/, 255]);
    if (World.GetServerString()){
        Render.GradientRect(x    , 10, w/2, 2, 2, [55, 177, 218/*55, 177, 218*/, 255], [203, 70, 205/*203, 70, 205*/, 255]);
        Render.GradientRect(x+ 141, 10, w/2, 2, 2, [203, 70, 205/*203, 70, 205*/, 255], [204, 227, 53/*204, 227, 53*/, 255]);}
}

const keybinds = function() {
var keybinds = [];
if (UI.IsMenuOpen()) {                                                       keybinds.push("Menu is Open") };
if (UI.GetValue("Anti-Aim", "Rage Anti-Aim", "Auto direction")) {            keybinds.push("Freestand") };
if (UI.IsHotkeyActive("Rage", "Exploits", "Double tap")) {                   keybinds.push("Double tap") };
if (UI.IsHotkeyActive("Rage", "Exploits", "Hide shots")) {                   keybinds.push("Hide shots") };
if (UI.IsHotkeyActive("Anti-Aim", "Extra", "Fake duck")) {                   keybinds.push("Duck peek assist") };
if (UI.IsHotkeyActive("Rage", "General", "General", "Force body aim")) {     keybinds.push("Body aim") };
if (UI.IsHotkeyActive("Rage", "General", "General", "Force safe point")) {   keybinds.push("Safe point") };
if (UI.IsHotkeyActive("Anti-Aim", "Extra", "Slow walk")) {   keybinds.push("Slow Walk") };
if (UI.IsHotkeyActive("Anti-Aim", "Fake angeles", "Inverter")) {   keybinds.push("Inverter") };
if (UI.IsHotkeyActive("Visual", "World", "View", "Thirdperson")) {   keybinds.push("Thirdperson") };
if (UI.IsHotkeyActive("Visual", "Self", "Freecam", "Freecam")) {   keybinds.push("Freecam") };
if (UI.IsHotkeyActive("Misc", "General", "Movement", "Edge jump")) {   keybinds.push("Jump at edge") };
if (UI.IsHotkeyActive("Misc", "General", "Movement", "Auto peek")) {   keybinds.push("Auto Peek") };
if (UI.IsHotkeyActive("Script items", "Legit AA Key")) {                      keybinds.push("Legit AA") };


const x = UI.GetValue("Script items", "binds x"),
y = UI.GetValue("Script items", "binds y");
const font = Render.AddFont("Calibri", 10, 100);
const color = UI.GetColor("Script items", "color");

Render.FilledRect(x, y, 150, 18, [0, 0, 0, color[3]]);
Render.StringCustom(x + 75, y + 1, 1, "binds", [255, 255, 255, 255], font);
for (i = 0; i < keybinds.length; i++){
    Render.StringCustom(x + 1, y + 18 + 15 * i, 0, keybinds[i], [255, 255, 255, 255], font);
    Render.StringCustom(x + 128, y + 18 + 15 * i, 0, "[on]", [255, 255, 255, 255], font);
}

Render.GradientRect( x, y - 2, 75, 2, 2, [55, 177, 218/*55, 177, 218*/, 255], [203, 70, 205/*203, 70, 205*/, 255]);
Render.GradientRect( x + 75, y - 2, 75, 2, 2, [203, 70, 205/*203, 70, 205*/, 255], [204, 227, 53/*204, 227, 53*/, 255]);


if(UI.IsMenuOpen() && Input.IsKeyPressed(0x1)){
    const mouse_pos = Global.GetCursorPosition();
    if (in_bounds(mouse_pos, x, y, x + 150, y + 40)) {
        if(!stored){
            x_offs = mouse_pos[0] - x;
            y_offs = mouse_pos[1] - y;
            stored = true;
        }
        UI.SetValue("Script items", "binds x", mouse_pos[0] - x_offs);
        UI.SetValue("Script items", "binds y", mouse_pos[1] - y_offs);
    }
} else if(stored) stored = false;
}

const get_spectators = function() {
    var spectators = [];
    const players = Entity.GetPlayers();
    for (i = 0; i < players.length; i++) {
        const cur = players[i];
        if (Entity.GetProp(cur, "CBasePlayer", "m_hObserverTarget") != "m_hObserverTarget") {
            const obs = Entity.GetProp(cur, "CBasePlayer", "m_hObserverTarget")
            if (obs === Entity.GetLocalPlayer()) {
                const name = Entity.GetName(cur);
                spectators.push(name);
            }
        }
    } return spectators; }

const spectators = function() {
    const x = UI.GetValue("Script items", "spectators x"),
    y = UI.GetValue("Script items", "spectators y");
    const font = Render.AddFont("Calibri", 10, 100);
    const color = UI.GetColor("Script items", "color");
    const names = get_spectators();

    Render.FilledRect(x, y, 150, 18, [0, 0, 0, color[3]]);
    Render.StringCustom(x + 75, y + 1, 1, "spectators", [255, 255, 255, 255], font);
    //Render.FilledRect(x, y - 2, 150, 2, [color[0], color[1], color[2], 255]);
    Render.GradientRect( x, y - 2, 75, 2, 2, [55, 177, 218/*55, 177, 218*/, 255], [203, 70, 205/*203, 70, 205*/, 255]);
    Render.GradientRect( x + 75, y - 2, 75, 2, 2, [203, 70, 205/*203, 70, 205*/, 255], [204, 227, 53/*204, 227, 53*/, 255]);

    for (i = 0; i < names.length; i++){
        Render.StringCustom(x + 1, y + 18 + 15 * i, 0, names[i], [255, 255, 255, 255], font);
    }

    if(UI.IsMenuOpen() && Input.IsKeyPressed(0x1)){
        const mouse_pos = Global.GetCursorPosition();
        if (in_bounds(mouse_pos, x, y, x + 150, y + 40)) {
            if(!stored){
                x_offs = mouse_pos[0] - x;
                y_offs = mouse_pos[1] - y;
                stored = true;
            }
            UI.SetValue("Script items", "spectators x", mouse_pos[0] - x_offs);
            UI.SetValue("Script items", "spectators y", mouse_pos[1] - y_offs);
        }
    } else if(stored) stored = false;
}

const draw = function(){
    if(get_dropdown_value(UI.GetValue("indicators"), 0))
    watermark();

    if(get_dropdown_value(UI.GetValue("indicators"), 1))
    keybinds();

    if(get_dropdown_value(UI.GetValue("indicators"), 2))
    spectators();
}

Cheat.RegisterCallback("Draw", "draw");