namespace InABox.Core
{
    public class ImportFieldGenerator : LookupGenerator<string>
    {
        static ImportFieldGenerator()
        {
            Fields = new string[] { };
        }

        public ImportFieldGenerator(string[] items) : base(items)
        {
        }

        public static string[] Fields { get; set; }

        protected override void DoGenerateLookups()
        {
            Clear();
            //AddValue("", "");
            foreach (var field in Fields)
                AddValue(field, field);
        }
    }

    public enum ImportLookupType
    {
        None,
        Restrict,
        Create
    }

    public class ImportMapping : BaseObject
    {
        [EditorSequence(1)]
        [TextBoxEditor(Visible = Visible.Default, Editable = Editable.Disabled)]
        public string Property { get; set; }

        [EditorSequence(2)]
        [ComboLookupEditor(typeof(ImportFieldGenerator), Visible = Visible.Default)]
        public string Field { get; set; }

        [EditorSequence(3)]
        [TextBoxEditor(Visible = Visible.Default)]
        public string Constant { get; set; }

        [EditorSequence(4)]
        [EnumLookupEditor(typeof(ImportLookupType), Visible = Visible.Hidden)]
        public ImportLookupType Lookup { get; set; }

        [EditorSequence(5)]
        [CheckBoxEditor(Visible = Visible.Hidden)]
        public bool Key { get; set; }

        protected override void DoPropertyChanged(string name, object? before, object? after)
        {
            base.DoPropertyChanged(name, before, after);
            bool bHasValue;
            if (name.Equals("Field"))
            {
                var constant = after as string;
                bHasValue = !string.IsNullOrWhiteSpace(constant) || !string.IsNullOrWhiteSpace(Field);
                if (!bHasValue && Key)
                    Key = false;
            }
            else if (name.Equals("Constant"))
            {
                var constant = after as string;
                bHasValue = !string.IsNullOrWhiteSpace(constant) || !string.IsNullOrWhiteSpace(Field);
                if (!bHasValue && Key)
                    Key = false;
            }
        }
    }
}