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"});

/*
*
*   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 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];
}

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 render_container = function(x, y, w, h, name, color1, color2, font, to_write, state){
    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){
        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);
    }
}

const screen_size = Global.GetScreenSize();

UI.AddSliderInt("hotkey_x", 0, screen_size[0]);
UI.AddSliderInt("hotkey_y", 0, screen_size[1]);
UI.SetEnabled("Script items", "hotkey_x", false);
UI.SetEnabled("Script items", "hotkey_y", false);

UI.AddLabel("           Hotkeys by @lenin");
UI.AddColorPicker("Gradient color 1");
UI.AddColorPicker("Gradient color 2");
UI.AddCheckbox("Render test container")

var stored = false;
var x_offs = 0;
var y_offs = 0;

const for_test = ["Something there", "And there", ":)"];

const main = function(){
    const font = Render.AddFont("Verdana", 7, 300);
    const active_binds = get_active_keybinds();
    const cursor_pos = Input.GetCursorPosition();
    
    if(UI.IsMenuOpen() && Input.IsKeyPressed(0x1)){
        if(point_in_rect(cursor_pos, [getval("hotkey_x"), getval("hotkey_y")], [getval("hotkey_x") + 150, getval("hotkey_y") + 25])){
            if(!stored){
                x_offs = cursor_pos[0] - getval("hotkey_x");
                y_offs = cursor_pos[1] - getval("hotkey_y");
                stored = true;
            }
            setval("hotkey_x", cursor_pos[0] - x_offs);
            setval("hotkey_y", cursor_pos[1] - y_offs);
        }
    } else if(stored) stored = false;

    render_container(getval("hotkey_x"), getval("hotkey_y"), 150, 25, "Hotkey", getcol("Gradient color 1"), getcol("Gradient color 2"), font, active_binds, "on")

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