//--------------------------------------
//               PowerUI
//
//        For documentation or 
//    if you have any issues, visit
//        powerUI.kulestar.com
//
//    Copyright  2013 Kulestar Ltd
//          www.kulestar.com
//--------------------------------------

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


namespace Css.Units{
	
	/// <summary>
	/// Used by for example [attrib="val"] in a selector.
	/// </summary>
	
	public class SquareBracketUnit:CssUnit{
		
		/// <summary>Contained CSS value.</summary>
		public ValueSet Value;
		
		public override string ToString(){
			return "["+Value.ToString()+"]";
		}
		
		public AttributeMatch GetMatch(){
			AttributeMatch attrib=new AttributeMatch();
			attrib.Load(Value);
			return attrib;
		}
		
		public override Value ReadStartValue(CssLexer lexer){
			
			// Skip the [:
			lexer.Read();
			
			// Note that we do not read off the close bracket.
			// This is so the lexer knows it's done reading the value
			// and can terminate accordingly.
			SquareBracketUnit block=new SquareBracketUnit();
			
			// Create the value:
			ValueSet cValue=new ValueSet();
			
			List<Css.Value> values=new List<Css.Value>();
			
			// Keep reading single values until a ] is reached.
			while(lexer.Peek()!='\0'){
				
				// Skip junk:
				lexer.SkipJunk();
				
				// Read the value:
				Css.Value value=lexer.ReadSingleValue();
				
				if(value.Type==ValueType.Text){
					
					if(value.Text=="]"){
						break;
					}
					
				}
				
				values.Add(value);
				
			}
			
			cValue.Count=values.Count;
			
			// Copy over:
			for(int i=0;i<values.Count;i++){
				cValue[i]=values[i];
			}
			
			block.Value=cValue;
			
			
			return block;
		}
		
		protected override Value Clone(){
			SquareBracketUnit result=new SquareBracketUnit();
			result.Value=Value;
			return result;
		}
		
		public override string[] PreText{
			get{
				return new string[]{"["};
			}
		}
		
	}
	
}



