﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VDS.RDF.Nodes;
using VDS.RDF.Query.Expressions.Functions.XPath.String;

namespace VDS.RDF.Query.Expressions.Functions.Sparql.String
{
    /// <summary>
    /// Represents the SPARQL LCASE Function
    /// </summary>
    public class LCaseFunction
        : BaseUnaryStringFunction
    {
        /// <summary>
        /// Creates a new LCASE function
        /// </summary>
        /// <param name="expr">Argument Expression</param>
        public LCaseFunction(ISparqlExpression expr)
            : base(expr) { }

        /// <summary>
        /// Calculates
        /// </summary>
        /// <param name="stringLit"></param>
        /// <returns></returns>
        protected override IValuedNode ValueInternal(ILiteralNode stringLit)
        {
            if (stringLit.DataType != null)
            {
                return new StringNode(null, stringLit.Value.ToLower(), stringLit.DataType);
            }
            else
            {
                return new StringNode(null, stringLit.Value.ToLower(), stringLit.Language);
            }
        }

        /// <summary>
        /// Gets the Functor of the Expression
        /// </summary>
        public override string Functor
        {
            get
            {
                return SparqlSpecsHelper.SparqlKeywordLCase;
            }
        }

        /// <summary>
        /// Gets the String representation of the Expression
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return SparqlSpecsHelper.SparqlKeywordLCase + "(" + this._expr.ToString() + ")";
        }

        /// <summary>
        /// Transforms the Expression using the given Transformer
        /// </summary>
        /// <param name="transformer">Expression Transformer</param>
        /// <returns></returns>
        public override ISparqlExpression Transform(IExpressionTransformer transformer)
        {
            return new LCaseFunction(transformer.Transform(this._expr));
        }
    }
}
