using Content.Server.Administration;
using Content.Server.Players;
using Content.Shared.Administration;
using Content.Shared.Roles;
using Robust.Server.Player;
using Robust.Shared.Console;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
using System.Linq;

namespace Content.Server.Roles
{
    [AdminCommand(AdminFlags.Fun)]
    public sealed class AddRoleCommand : IConsoleCommand
    {
        public string Command => "addrole";

        public string Description => "Adds a role to a player's mind.";

        public string Help => "addrole <session ID> <role>";

        public void Execute(IConsoleShell shell, string argStr, string[] args)
        {
            if (args.Length != 2)
            {
                shell.WriteLine("Expected exactly 2 arguments.");
                return;
            }

            var mgr = IoCManager.Resolve<IPlayerManager>();
            if (!mgr.TryGetPlayerDataByUsername(args[0], out var data))
            {
                shell.WriteLine("Can't find that mind");
                return;
            }

            var mind = data.ContentData()?.Mind;
            if (mind == null)
            {
                shell.WriteLine("Can't find that mind");
                return;
            }

            var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
            if (!prototypeManager.TryIndex<JobPrototype>(args[1], out var jobPrototype))
            {
                shell.WriteLine("Can't find that role");
                return;
            }

            if (mind.AllRoles.Any(r => r.Name == jobPrototype.Name))
            {
                shell.WriteLine("Mind already has that role");
                return;
            }

            var role = new Job(mind, jobPrototype!);
            mind.AddRole(role);
        }
    }
}
