//.################################################
//# Copyright © 2006-2010 Evolving Software Ltd   #
//# Author: Vijay Patel                           #
//################################################'

using System;
using System.Linq.Expressions;

using TestApplicationDomainModel.BasicTypes;
using Evolving.TrueView.Domain.Configuration;
using Evolving.TrueView.Domain.Attributes;
using TestApplicationDomainModel.Objects;

namespace TestApplication
{

    public class PocoObjectConfiguration : ClassConfiguration<PocoObject>
    {
        private const string CategoryName = "A custom category";

        public PocoObjectConfiguration()
        {
            // Use the Attribute classes to configure the Class and/or it's Members:

            this.ConfigureClass(new ObjectInstanceAttribute()
            {
                Name = "POCO object",
                IsPersistable = true,
            });

            this.ConfigureProperty(NameOf(() => ClassType.NormalBoolean), new BooleanAttribute()
            {
                IsVisible = true,
                CanWrite = false,
            });

            this.ConfigureProperty(NameOf(() => ClassType.FormattedText), new TextAttribute()
            {
                IsVisible = true,
                IsRichText = true,
            });

            this.ConfigureProperty(NameOf(() => ClassType.NormalDate), new DateTimeAttribute()
            {
                Category = CategoryName
            });

            this.ConfigureProperty(NameOf(() => ClassType.EnumValue), new EnumAttribute()
            {
                Category = CategoryName,
            });

            this.ConfigureProperty(NameOf(() => ClassType.EnumSwitches), new EnumAttribute()
            {
                Category = CategoryName,
            });

            this.ConfigureProperty(NameOf(() => ClassType.NormalText), new TextAttribute()            
            {
                Category = CategoryName,
            });

            this.ConfigureProperty(NameOf(() => ClassType.NullableInt), new NumberAttribute()            
            {
                Category = CategoryName,
            });

            this.ConfigureProperty(NameOf(() => ClassType.Point), new PropertyAttribute()            
            {
                Category = CategoryName,
            });

            this.ConfigureProperty(NameOf(() => ClassType.ChildObjects), new ListPropertyAttribute()            
            {
                Category = CategoryName,
                CanCreate = true,
                CanAdd = true,
                IsVisible = true,
                IsShownInline = true
            });

        }

        /// <summary>
        /// Returns the member's name for the given lambda expression
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression"></param>
        /// <returns></returns>
        private string NameOf<T>(Expression<Func<T>> expression)
        {
            return ((MemberExpression)expression.Body).Member.Name;
        }

    }

}
