using System;

namespace InABox.Core
{
    // EntityForm no longer is an IOneToMany, so any subclasses must add this if they want to.

    public interface IEntityForm
    {

    }

    [Caption("Digital Forms")]
    public abstract class EntityForm<TParent, TParentLink> : Entity, IRemotable, IPersistent, ISequenceable,
        IEntityForm,
        IDigitalFormInstance<TParentLink>, ILicense<DigitalFormsLicense>
        where TParent : Entity
        where TParentLink : IEntityLink<TParent>, new()
    {
        [EditorSequence(2)]
        public string Description { get; set; }

        [NullEditor]
        public TParentLink Parent { get; set; }

        [NullEditor]
        [Obsolete("Being Replaced by Form")]
        public QAFormLink QAForm { get; set; }

        [EditorSequence(1)]
        public DigitalFormLink Form { get; set; }

        [NullEditor]
        [Obsolete("Being Replaced by FormData", true)]
        public string QAData { get; set; }

        [NullEditor]
        public string FormData { get; set; }

        [NullEditor]
        public string? BlobData { get; set; }

        [NullEditor]
        [Obsolete("Being Replaced by FormCompleted", true)]
        public DateTime QACompleted { get; set; }

        [EditorSequence(2)]
        [Caption("Completed")]
        [DateTimeEditor]
        public DateTime FormCompleted { get; set; }

        [NullEditor]
        [Obsolete("Being Replaced by FormCompletedBy")]
        public UserLink QACompletedBy { get; set; }

        [EditorSequence(3)]
        [Caption("User")]
        public UserLink FormCompletedBy { get; set; }

        [EditorSequence(4)]
        [CheckBoxEditor]
        public bool Processed { get; set; }

        [NullEditor]
        public Location Location { get; set; }

        public IDigitalFormDataModel CreateDataModel(Entity? parent = null)
        {
            var t = typeof(DigitalFormDataModel<,,>).MakeGenericType(typeof(TParent), typeof(TParentLink), GetType());
            if (parent != null)
                return (Activator.CreateInstance(t, parent, this) as IDigitalFormDataModel)!;
            return (Activator.CreateInstance(t, Parent.ID, ID) as IDigitalFormDataModel)!;
        }

        public Guid ParentID()
        {
            return Parent.ID;
        }

        public DateTime FormStarted { get; set; }
        public TimeSpan FormOpen { get; set; }

        [NullEditor]
        public long Sequence { get; set; }

        protected override void Init()
        {
            base.Init();
            Parent = new TParentLink();
            Form = new DigitalFormLink();
            FormCompletedBy = new UserLink();
            QAForm = new QAFormLink();
            QACompletedBy = new UserLink();
            Location = new Location();
            Description = "";
            FormStarted = DateTime.MinValue;
            FormOpen = TimeSpan.Zero;
            Processed = false;
        }
    }
}