using System.Linq;

namespace InABox.Core
{
    [UserTracking(typeof(User))]
    [Caption("Security Defaults")]
    [Unrecoverable]
    public class SecurityToken : Entity, IPersistent, IRemotable, ILicense<CoreLicense>
    {
        [NullEditor]
        [EntityRelationship(DeleteAction.Cascade)]
        public SecurityGroupLink Group => InitializeField(ref _group, nameof(Group));
        private SecurityGroupLink? _group;

        [ComboLookupEditor(typeof(SecurityRestrictionGenerator))]
        public string Descriptor { get; set; }

        [CheckBoxEditor]
        public bool Enabled { get; set; }

        public override string ToString()
        {
            return Descriptor;
        }

        private class SecurityRestrictionGenerator : LookupGenerator<object>
        {
            public SecurityRestrictionGenerator(object[] items) : base(items)
            {
                foreach (var descriptor in Security.Descriptors.Where(x => x.HasScope(SecurityDescriptorScope.Group)))
                    AddValue(descriptor.Code, descriptor.Description);
            }
        }
    }
}