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

namespace InABox.Core
{
    public class DFLayoutEmbeddedImageProperties : DFLayoutFieldProperties<Guid, DFLayoutEmbeddedMediaValue>
    {
        
        [EditorSequence(-995)]
        [NullEditor]
        public override Guid Default { get; set; }
        
        [CheckBoxEditor]
        [EditorSequence(1)]
        public bool DisableLibrary { get; set; }

        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 Guid GetValue(DFLayoutEmbeddedMediaValue value)
        {
            return value.ID;
        }

        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)
        {
            if(value.Data != null && value.Data.Length > 0)
            {
                yield return new KeyValuePair<string, object?>(Code, value.Data);
            }
            else
            {
                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("DisableLibrary", false);
        }

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