using RobotNet.Script;
using RobotNet.Script.Shares;
using RobotNet.ScriptManager.Services;

namespace RobotNet.ScriptManager.Models;

public abstract class ScriptGlobalsRobotNet(Script.ILogger logger, IRobotManager robotManager, IMapManager mapManager, ScriptConnectionManager connectionManager, IServiceScopeFactory scopeFactory) : IRobotNetGlobals
{
    public Script.ILogger Logger { get; } = logger;

    public IRobotManager RobotManager { get; } = robotManager;

    public IMapManager MapManager { get; } = mapManager;

    public virtual Guid CurrentMissionId => throw new NotImplementedException();

    public IUnixDevice UnixDevice { get; } = Connections.UnixDevice.Instance;

    public IConnectionManager ConnectionManager { get; } = connectionManager;

    public async Task<Guid> CreateMission(string name, params object[] parameters)
    {
        using var scope = scopeFactory.CreateScope();
        var missionCreator = scope.ServiceProvider.GetRequiredService<ScriptMissionCreator>();
        return await missionCreator.CreateMissionAsync(name, []);
    }

    public async Task<Guid> CreateMission(string name) => await CreateMission(name, []);

    public bool CancelMission(Guid missionId, string reason)
    {
        using var scope = scopeFactory.CreateScope();
        var missionManager = scope.ServiceProvider.GetRequiredService<ScriptMissionManager>();
        return missionManager.Cancel(missionId, reason);
    }

    public bool DisableTask(string name)
    {
        using var scope = scopeFactory.CreateScope();
        var taskManager = scope.ServiceProvider.GetRequiredService<ScriptTaskManager>();
        return taskManager.Pause(name);
    }

    public bool EnableTask(string name)
    {
        using var scope = scopeFactory.CreateScope();
        var taskManager = scope.ServiceProvider.GetRequiredService<ScriptTaskManager>();
        return taskManager.Resume(name);
    }

}

public class ScriptGlobalsRobotNetTask(Script.ILogger logger, IRobotManager robotManager, IMapManager mapManager, ScriptConnectionManager connectionManager, IServiceScopeFactory scopeFactory)
    : ScriptGlobalsRobotNet(logger, robotManager, mapManager, connectionManager, scopeFactory)
{
    public override Guid CurrentMissionId => throw new NotImplementedException();
}

public class ScriptGlobalsRobotNetMission(Guid id, Script.ILogger logger, IRobotManager robotManager, IMapManager mapManager, ScriptConnectionManager connectionManager, IServiceScopeFactory scopeFactory)
    : ScriptGlobalsRobotNet(logger, robotManager, mapManager, connectionManager, scopeFactory)
{
    public override Guid CurrentMissionId { get; } = id;
}
