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

using System;
using PowerUI.Http;
using System.Collections;
using System.Collections.Generic;


namespace PowerUI{
	
	public partial class HtmlDocument{
	
		public string cookie{
			get{
				
				if(location==null){
					return "";
				}
				
				// Get the jar for this location:
				CookieJar jar=location.CookieJar;
				
				if(jar==null){
					return "";
				}
				
				// Got a cookie jar!
				
				// Build a cookie string for non-httponly and non-secure cookies:
				string str="";
				
				foreach(KeyValuePair<string,Cookie> kvp in jar.CookieSet){
					
					Cookie c=kvp.Value;
					
					if(!c.Secure && !c.HttpOnly){
						
						if(str!=""){
							str+="; ";
						}
						
						// Add it:
						str+=c.ToString();
						
					}
					
				}
				
				return str;
				
			}
			set{
				
				if(location==null){
					throw new Exception("Can't set cookies here!");
				}
				
				// Cookie parsing adapted from node.js cookie module, so it should be pretty robust.
				// With thanks to UnityCookies.cs by Sam McGrath
				
				if(value==null){
					return;
				}
				
				string[] pairs = value.Split(new string[]{"; "},StringSplitOptions.None);
				
				for(int i=0;i<pairs.Length;i++){
					
					string pair=pairs[i].Trim();
					
					// Load the cookie:
					Cookie cookie=new Cookie(pair);
					
					// Safe to set?
					cookie.SafeSet(location);
					
				}
				
			}
		}
		
	}

}