//
// ExpressionResult.cs
//
// Author:
//   Mike Krüger <mkrueger@novell.com>
//
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections.Generic;

namespace MonoDevelop.Projects.Dom
{
	public class ExpressionResult
	{
		public string Expression {
			get;
			set;
		}
		
		public DomRegion Region {
			get;
			set;
		}
		
		List<ExpressionContext> expressionContexts = new List<ExpressionContext> ();
		public IList<ExpressionContext> Contexts {
			get {
				return expressionContexts;
			}
		}
		
		public ExpressionContext ExpressionContext {
			get {
				return expressionContexts.Count > 0 ? expressionContexts[expressionContexts.Count - 1] : null;
			}
			set {
				expressionContexts.Clear ();
				expressionContexts.Add (value);
			}
		}
		
		public bool IsInInheritableTypeContext {
			get {
				for (int i = expressionContexts.Count - 1; i >= 0; i--) {
					ExpressionContext cur = expressionContexts[i];
					if (cur == ExpressionContext.ObjectCreation)
						return true;
					if (cur == ExpressionContext.IdentifierExpected || cur == ExpressionContext.Global)
						continue;
					return cur == ExpressionContext.InheritableType;
				}
				return false;
			}
		}
		
		public ExpressionResult (string expression) : this (expression, DomRegion.Empty, new ExpressionContext[0])
		{
		}
		
		public ExpressionResult (string expression, ExpressionContext expressionContext) : this (expression, DomRegion.Empty, expressionContext)
		{
		}
		public ExpressionResult (string expression, IEnumerable<ExpressionContext> expressionContexts) : this (expression, DomRegion.Empty, expressionContexts)
		{
		}
		
		public ExpressionResult (string expression, DomRegion region, ExpressionContext expressionContext) : this (expression, region, new [] {expressionContext})
		{
		}
		
		public ExpressionResult (string expression, DomRegion region, IEnumerable<ExpressionContext> expressionContexts)
		{
			this.Expression = expression;
			this.Region     = region;
			this.expressionContexts = new List<ExpressionContext> (expressionContexts);
		}
		
		public override string ToString ()
		{
			return String.Format ("[ExpressionResult:Expression={0}, ExpressionContext={1}, Region={2}]",
			                      this.Expression,
			                      this.ExpressionContext,
			                      this.Region);
		}
	}
}
