using System;
using System.Collections.Generic;
using System.Text;

namespace InABox.Core
{

    public class DFLayoutVideoFieldProperties : DFLayoutFieldProperties<byte[], DFLayoutEmbeddedMediaValue>
    {
        public enum VideoQuality
        {
            Default,
            Low,
            Medium,
            High
        }

        [EditorSequence(-995)] 
        [NullEditor]
        public override byte[] Default { get; set; } = new byte[] { };
        
        [CheckBoxEditor]
        [EditorSequence(100)]
        public bool DisableLibrary { get; set; }

        [Comment("Maximum video length (sec)")]
        [IntegerEditor]
        [EditorSequence(101)]
        public int MaximumVideoLength { get; set; } = 0;

        [EnumLookupEditor(typeof(VideoQuality))]
        [EditorSequence(102)]
        public VideoQuality Quality { get; set; } = VideoQuality.Default;

        public override string FormatValue(DFLayoutEmbeddedMediaValue value)
        {
            return value.ID != Guid.Empty ? "Yes" : "";
        }

        public override DFLayoutEmbeddedMediaValue DeserializeValue(DFLoadStorageEntry entry)
        {
            var value = new DFLayoutEmbeddedMediaValue();
            value.Deserialize(entry);
            return value;
        }

        public override void SerializeValue(DFSaveStorageEntry entry, DFLayoutEmbeddedMediaValue value)
        {
            value.Serialize(entry);
        }

        public override byte[] GetValue(DFLayoutEmbeddedMediaValue value)
        {
            return value.Data ?? Default;
        }
        public override IEnumerable<CoreColumn> GetDisplayColumns()
        {
            yield return new CoreColumn { ColumnName = Code, DataType = typeof(byte[]) };
            yield return new CoreColumn { ColumnName = $"{Code}.ID", DataType = typeof(Guid) };
            yield return new CoreColumn { ColumnName = $"{Code}.Thumbnail", DataType = typeof(byte[]) };
        }

        public override IEnumerable<KeyValuePair<string, object?>> GetDisplayValues(DFLayoutEmbeddedMediaValue value)
        {
            yield return new KeyValuePair<string, object?>(Code, DigitalFormDocumentFactory.LoadDocument(value.ID));
            yield return new KeyValuePair<string, object?>($"{Code}.ID", value.ID);
            yield return new KeyValuePair<string, object?>($"{Code}.Thumbnail", value.Thumbnail);
        }

        protected override void LoadProperties()
        {
            base.LoadProperties();
            DisableLibrary = GetProperty(nameof(DisableLibrary), false);
            MaximumVideoLength = GetProperty(nameof(MaximumVideoLength), 0);
            Quality = GetProperty(nameof(Quality), VideoQuality.Default);
         }

        protected override void SaveProperties()
        {
            base.SaveProperties();
            SetProperty(nameof(DisableLibrary), DisableLibrary);
            SetProperty(nameof(MaximumVideoLength),MaximumVideoLength);
            SetProperty(nameof(Quality), Quality);
        }
        
    }
}
