using System;

namespace InABox.Core
{
    public class DFLayoutTimeStampFieldProperties : DFLayoutFieldProperties<DateTime, DateTime?>
    {
        
        [EditorSequence(-995)]
        [TimestampEditor]
        public override DateTime Default { get; set; }
        
        public override string FormatValue(DateTime? value)
        {
            return string.Format("{0:mm HH dd MMM yy}", value);
        }

        public override void SerializeValue(DFSaveStorageEntry entry, DateTime? value)
        {
            if (value != null)
            {
                entry.SetValue(value);
            }
        }

        public override DateTime? DeserializeValue(DFLoadStorageEntry entry)
        {
            var value = entry.GetValue();
            if (value is DateTime date)
                return date;
            if (DateTime.TryParse(value as string, out var result))
                return result;
            return null;
        }

        public override DateTime GetValue(DateTime? value)
        {
            return value ?? Default;
        }
    }
}