#region COPYRIGHT
//
//     THIS IS GENERATED BY TEMPLATE
//     
//     AUTHOR  :     ROYE
//     DATE       :     2010
//
//     COPYRIGHT (C) 2010, TIANXIAHOTEL TECHNOLOGIES CO., LTD. ALL RIGHTS RESERVED.
//
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace System.Text.Template
{
    public class AsExpression : Expression
    {
        private readonly Expression _ObjectExpression;
        private readonly Expression _TypeExpression;

        public AsExpression(Expression objectExpression, Expression typeExpression)
        {
            _ObjectExpression = objectExpression;
            _TypeExpression = typeExpression;
        }

        public override ValueExpression Evaluate(ITemplateContext context)
        {
            ClassName className = _TypeExpression.Evaluate(context).Value as ClassName;
            if (className == null)
            {
                throw new Exception("as operator requires type");
            }

            Type checkType = className.Type;
            ValueExpression objectValue = _ObjectExpression.Evaluate(context);
            Type objectType = objectValue.Type;

            if (objectValue.Value == null)
            {
                return Expression.Value(null, checkType);
            }

            objectType = Nullable.GetUnderlyingType(objectType) ?? objectType;
            if (!objectType.IsValueType)
            {
                return Expression.Value(objectValue.Value, checkType);
            }

            if ((Nullable.GetUnderlyingType(checkType) ?? checkType) == objectType)
            {
                return Expression.Value(objectValue.Value, checkType);
            }

            return Expression.Value(null, checkType);
        }
    }
}
