using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;

namespace InABox.Core
{
    public class DFLayoutMultiImageProperties : DFLayoutFieldProperties<List<Guid>?, DFLayoutEmbeddedMediaValues>
    {

        [EditorSequence(-995)] [NullEditor] public override List<Guid>? Default { get; set; } = new List<Guid>();
        
        [EditorSequence(100)]
        [CheckBoxEditor]
        public bool DisableLibrary { get; set; } = false;

        public override string FormatValue(DFLayoutEmbeddedMediaValues value)
        {
            return value.Present ? "Yes" : "";
        }

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

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

        public override List<Guid> GetValue(DFLayoutEmbeddedMediaValues value)
        {
            return value.Select(x => x.ID).ToList();
        }

        public override IEnumerable<CoreColumn> GetDisplayColumns()
        {
            yield return new CoreColumn { ColumnName = Code, DataType = typeof(List<byte[]>) };
            yield return new CoreColumn { ColumnName = $"{Code}.IDs", DataType = typeof(List<Guid>) };
            yield return new CoreColumn { ColumnName = $"{Code}.Thumbnails", DataType = typeof(List<byte[]>) };
        }

        public override IEnumerable<KeyValuePair<string, object?>> GetDisplayValues(DFLayoutEmbeddedMediaValues value)
        {
            yield return new KeyValuePair<string, object?>(Code, value.Select(x => DigitalFormDocumentFactory.LoadDocument(x.ID)).ToList());
            yield return new KeyValuePair<string, object?>($"{Code}.IDs", value.Select(x => x.ID).ToList());
            yield return new KeyValuePair<string, object?>($"{Code}.Thumbnails", value.Select(x => x.Thumbnail).ToList());
        }

        protected override void LoadProperties()
        {
            base.LoadProperties();
            DisableLibrary = GetProperty("DisableLibrary", false);
        }

        protected override void SaveProperties()
        {
            base.SaveProperties();
            SetProperty("DisableLibrary", DisableLibrary);
        }
    }
}