using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
using RobotNet.ScriptManager.Services;

namespace RobotNet.ScriptManager.Hubs;

[Authorize]
public class VariablesHub(ScriptGlobalsManager globalsManager) : Hub
{
    public string GetString(string name)
    {
        if (globalsManager.GetVariableType(name) == typeof(string) && globalsManager.Globals.TryGetValue(name, out var value) && value is string strValue)
        {
            return strValue;
        }

        return string.Empty;
    }

    public int GetInt(string name)
    {
        if (globalsManager.GetVariableType(name) == typeof(int) && globalsManager.Globals.TryGetValue(name, out var value) && value is int intValue)
        {
            return intValue;
        }
        return 0;
    }

    public bool GetBool(string name)
    {
        if (globalsManager.GetVariableType(name) == typeof(bool) && globalsManager.Globals.TryGetValue(name, out var value) && value is bool boolValue)
        {
            return boolValue;
        }
        return false;
    }

    public double GetDouble(string name)
    {
        if (globalsManager.GetVariableType(name) == typeof(double) && globalsManager.Globals.TryGetValue(name, out var value) && value is double doubleValue)
        {
            return doubleValue;
        }
        return 0.0;
    }

    public void SetString(string name, string value)
    {
        if (globalsManager.GetVariableType(name) == typeof(double) && globalsManager.Globals.ContainsKey(name))
        {
            globalsManager.Globals[name] = value;
        }
    }

    public void SetInt(string name, int value)
    {
        if (globalsManager.GetVariableType(name) == typeof(int) && globalsManager.Globals.ContainsKey(name))
        {
            globalsManager.Globals[name] = value;
        }
    }

    public void SetBool(string name, bool value)
    {
        if (globalsManager.GetVariableType(name) == typeof(bool) && globalsManager.Globals.ContainsKey(name))
        {
            globalsManager.Globals[name] = value;
        }
    }

    public void SetDouble(string name, double value)
    {
        if (globalsManager.GetVariableType(name) == typeof(double) && globalsManager.Globals.ContainsKey(name))
        {
            globalsManager.Globals[name] = value;
        }
    }
}
