namespace InABox.Core
{
    public enum DesignBooleanFieldType
    {
        Checkbox,
        ComboBox,
        Buttons
    }

    public class DFLayoutBooleanFieldProperties : DFLayoutFieldProperties<bool>
    {
        [EnumLookupEditor(typeof(DesignBooleanFieldType))]
        [EditorSequence(1)]
        public DesignBooleanFieldType Type { get; set; }

        [TextBoxEditor]
        [EditorSequence(2)]
        public string TrueValue { get; set; }

        [TextBoxEditor]
        [EditorSequence(3)]
        public string FalseValue { get; set; }

        protected override void LoadProperties()
        {
            base.LoadProperties();
            Type = GetProperty("Type", DesignBooleanFieldType.Checkbox);
            TrueValue = GetProperty("TrueValue", "True");
            FalseValue = GetProperty("FalseValue", "False");
        }

        protected override void SaveProperties()
        {
            base.SaveProperties();
            SetProperty("Type", Type);
            SetProperty("TrueValue", TrueValue);
            SetProperty("FalseValue", FalseValue);
        }

        public override string FormatValue(object? value)
        {
            return Equals(value, true) ? TrueValue : FalseValue;
        }

        public override object? ParseValue(object? value)
        {
            if (value is bool)
                return value;
            if (bool.TryParse(value as string, out var result))
                return result;
            if (value is string str)
            {
                if (str == TrueValue)
                    return true;
                if (str == FalseValue)
                    return false;
                return null;
            }
            return null;
        }
    }
}