var keybinds = [];
keybinds.push({path: ["Rage", "Exploits", "Hide shots"], name: "Hide-shots"});
keybinds.push({path: ["Rage", "GENERAL", "Exploits", "Doubletap"], name: "Double-tap"});
keybinds.push({path: ["Rage", "GENERAL", "Force body aim"], name: "Body-aim"});
keybinds.push({path: ["Anti-Aim", "Fake angles", "Inverter"], name: "Angle Inverter"});
keybinds.push({path: ["Rage", "GENERAL", "Force safe point"], name: "Force Safepoint"});
keybinds.push({path: ["Misc", "Movement", "Auto peek"], name: "Auto Peek"});
keybinds.push({path: ["Anti-Aim", "Extra", "Slow walk"], name: "Slow Walk"});
keybinds.push({path: ["Anti-Aim", "Extra", "Fake duck"], name: "Fake Duck"});

/*
*
*   Concept: https://yougame.biz/attachments/114551
*
*/

const print = function(text, color){
    if(color == undefined)
    Cheat.Print(text.toString() + "\n");
    else
    Cheat.PrintColor(color, text.toString() + "\n");
}

const getval = function(name){
    return UI.GetValue("Script items", name);
}

const setval = function(name, value){
    return UI.SetValue("Script items", name, value);
}

const getcol = function(name){
    return UI.GetColor("Script items", name);
}

const get_active_keybinds = function(){
    var ret = [];
    for(var i in keybinds){
        if(UI.IsHotkeyActive.apply(null, keybinds[i].path))
            ret.push(keybinds[i].name);
    }
    return ret;
}

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

const point_in_rect = function(pos, min, max, debug) {
    if(debug) {
        if(pos[0] > min[0] && pos[1] > min[1] && pos[0] < max[0] && pos[1] < max[1])
             Render.Rect(min[0], min[1], max[0] - min[0], max[1] - min[1], [0, 255, 0, 255])
        else Render.Rect(min[0], min[1], max[0] - min[0], max[1] - min[1], [255, 0, 0, 255])             
    }
    return pos[0] > min[0]
        && pos[1] > min[1]
        && pos[0] < max[0]
        && pos[1] < max[1];
}

const get_dropdown_value = function(value, index)
{
    const mask = 1 << index;
    return value & mask ? true : false;
}

Cheat.Print("\nLoaded ");
Cheat.PrintColor([255,0,0,255], keybinds.length + " ");
print("keybinds!\n");
print("You may add yours by using:");
Cheat.PrintColor([255,255,0,255], 'keybinds.push({path: ["Legit", "Triggerbot", "Enabled"], name: "Trigger bot"}); ');
print("for example.");

const screen_size = Global.GetScreenSize();

const render_container = function(x1, y1, w, h, name, color1, color2, font, to_write, state, custom){

    if(getval(name + "_x") <= 0){
        UI.AddCheckbox(name+" stored");
        UI.AddSliderInt(name+"_x", 1, screen_size[0]);
        UI.AddSliderInt(name+"_y", 1, screen_size[0]);
        UI.AddSliderInt(name+"_x_off", 1, screen_size[0]);
        UI.AddSliderInt(name+"_y_off", 1, screen_size[0]);
        setval(name+"_x", x1);
        setval(name+"_y", y1);
        UI.SetEnabled("Script items", name+" stored", false);
        UI.SetEnabled("Script items", name+"_x", false);
        UI.SetEnabled("Script items", name+"_y", false);
        UI.SetEnabled("Script items", name+"_x_off", false);
        UI.SetEnabled("Script items", name+"_y_off", false);
    }

    const cursor_pos = Input.GetCursorPosition();
    if(UI.IsMenuOpen() && Input.IsKeyPressed(0x1)){
        if(point_in_rect(cursor_pos, [getval(name+"_x"), getval(name+"_y")], [getval(name+"_x") + w, getval(name+"_y") + h])){
            if(getval(name + " stored") == 0){
                setval(name+"_x_off", cursor_pos[0] - getval(name+"_x"));
                setval(name+"_y_off", cursor_pos[1] - getval(name+"_y"));
                setval(name + " stored", 1);
            }
            setval(name+"_x", cursor_pos[0] - getval(name+"_x_off"));
            setval(name+"_y", cursor_pos[1] - getval(name+"_y_off"));
        }
    } else if(getval(name + " stored")) setval(name + " stored", 0);

    x = getval(name+"_x");
    y = getval(name+"_y");
    
    Render.GradientRect(x, y, w, 2, 1, color1, color2);
    Render.GradientRect(x + 1, y + 2, w - 2, h, 0, [0,0,0,125], [0,0,0,0]);
    
    const text_size = Render.TextSizeCustom(name, font);
    Render.FilledCircle(x + 12, y + 12, text_size[1] / 2 + 1, [20,20,20,255]);
    Render.FilledCircle(x + 12 + text_size[0], y + 12, text_size[1] / 2 + 1, [20,20,20,255]);
    Render.FilledRect(x + 12, y + 6, text_size[0], text_size[1] + 1, [20,20,20,255]);

    Render.StringCustom(x + 13, y + 6, 0, name, [255,255,255,255], font);
    Render.StringCustom(x + w - 27, y + 6, 0, "<", [255,255,255,255], font);
    Render.FilledCircle(x + w - 14, y + 12, 6, [20,20,20,100]);
    Render.StringCustom(x + w - 16, y + 5, 0, "x", [255,255,255,255], font);

    const text_size_state = Render.TextSizeCustom(state, font);
    for(var i in to_write){
        if(custom != null){
            const text_size_state = Render.TextSizeCustom(custom[i], font);
            state = custom[i];
        }
        Render.StringCustom(x + 10, y + 25 + (i * 13), 0, to_write[i], [255,255,255,255], font);
        Render.StringCustom(x + w - text_size_state[0] - 15, y + 25 + (i * 13), 0, "[" + state + "]", [255,255,255,255], font);
    }
}

UI.AddLabel("          Indicators by @lenin");
UI.AddMultiDropdown( "Windows", [ "Hotkeys", "Spectator list", "Watermark" ] );
UI.AddCheckbox("Show windows only with information");
UI.AddColorPicker("Gradient color 1");
UI.AddColorPicker("Gradient color 2");
UI.AddCheckbox("Render test container");

const watermark = [Cheat.GetUsername().length > 14 ? Cheat.GetUsername().substr(0,14) + "..." : Cheat.GetUsername(), World.GetServerString() != "" ? World.GetServerString() : "unknown", Math.round(Local.Latency() * 1000 - 16).toString(), Globals.Tickrate().toString()];
const watermark_custom = ["user", "server", "latency", "tickrate"];
const for_test = ["Idet negr", "vidit dom stoit", "konec prikola"];
const main = function(){
    const font = Render.AddFont("Verdana", 7, 300);

    if(get_dropdown_value(getval("Windows"), 0)){
        const active_binds = get_active_keybinds();
        if(!(active_binds.length <= 0 && getval("Show windows only with information")))
        render_container(300, 300, 150, 25, "Hotkey", getcol("Gradient color 1"), getcol("Gradient color 2"), font, active_binds, "on")
    }

    if(get_dropdown_value(getval("Windows"), 1)){
        const spectators = get_spectators();
        if(!(spectators.length <= 0 && getval("Show windows only with information")))
        render_container(300, 450, 200, 25, "Spectators", getcol("Gradient color 1"), getcol("Gradient color 2"), font, spectators, "watching")
    }

    if(get_dropdown_value(getval("Windows"), 2))
        render_container(300, 700, 150, 25, "onetap.com", getcol("Gradient color 1"), getcol("Gradient color 2"), font, watermark_custom, "", watermark)

    if(getval("Render test container"))
        render_container(300, 100, 150, 25, "Test Container", getcol("Gradient color 1"), getcol("Gradient color 2"), font, for_test, "funny")
}
Cheat.RegisterCallback("Draw", "main");