#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 ConditionalExpression : Expression
    {
        private readonly Expression _condition;
        private readonly Expression _trueValue;
        private readonly Expression _falseValue;

        public ConditionalExpression(Expression condition, Expression trueValue, Expression falseValue)
        {
            _condition = condition;
            _trueValue = trueValue;
            _falseValue = falseValue;
        }

        public override ValueExpression Evaluate(ITemplateContext context)
        {
            bool result = context.ToBoolean(_condition.Evaluate(context).Value);

            if (result)
                return _trueValue.Evaluate(context);
            else
                return _falseValue.Evaluate(context);
        }

        public override string ToString()
        {
            return "(" + _condition + " ? " + _trueValue + " : " + _falseValue + ")";

        }
    }
}
