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

using Dom;
using UnityEngine;
using Blaze;
using System.Collections;
using System.Collections.Generic;


namespace Css{
	
	/// <summary>
	/// Represents a region of the screen. Very similar to System.Drawing.Region.
	/// </summary>
	
	public class ScreenRegion{
		
		/// <summary>True if this region contains the given x/y point assuming an infinitely
		/// long ray on the z axis.</summary>
		public virtual bool Contains(float x,float y){
			return false;
		}
		
		public virtual float ScreenMinX{
			get{
				return 0f;
			}
		}
		
		public virtual float ScreenMinY{
			get{
				return 0f;
			}
		}
		
		public virtual float ScreenMaxX{
			get{
				return 0f;
			}
		}
		
		public virtual float ScreenMaxY{
			get{
				return 0f;
			}
		}
		
    }
	
	/// <summary>A region defined by a usually closed path.</summary>
	public class PathScreenRegion : ScreenRegion{
		
		public VectorPath Path;
		
		public override float ScreenMinX{
			get{
				return Path.MinX;
			}
		}
		
		public override float ScreenMinY{
			get{
				return Path.MinY;
			}
		}
		
		public override float ScreenMaxX{
			get{
				return Path.Width + Path.MinX;
			}
		}
		
		public override float ScreenMaxY{
			get{
				return Path.Height + Path.MinY;
			}
		}
		
		public PathScreenRegion(VectorPath path){
			Path=path;
			
			if(path.Width==0f){
				// Bounds are required here:
				path.RecalculateBounds();
			}
			
		}
		
		public override bool Contains(float x,float y){
			return Path.Contains(x,y);
		}
		
	}
	
	/// <summary>A collection of screen regions.</summary>
	public class ScreenRegionGroup : ScreenRegion{
		
		/// <summary>The bounds of the group.</summary>
		private float MinX_=float.MaxValue;
		private float MinY_=float.MaxValue;
		private float MaxX_=float.MinValue;
		private float MaxY_=float.MinValue;
		/// <summary>The regions within the group.</summary>
		private List<ScreenRegion> Regions;
		
		
		public ScreenRegionGroup(){
			Regions=new List<ScreenRegion>();
		}
		
		public override float ScreenMinX{
			get{
				return MinX_;
			}
		}
		
		public override float ScreenMinY{
			get{
				return MinY_;
			}
		}
		
		public override float ScreenMaxX{
			get{
				return MaxX_;
			}
		}
		
		public override float ScreenMaxY{
			get{
				return MaxY_;
			}
		}
		
		
		public ScreenRegionGroup(List<ScreenRegion> set){
			Regions=set;
		}
		
		public override bool Contains(float x,float y){
			
			for(int i=Regions.Count-1;i>=0;i--){
				
				if(Regions[i].Contains(x,y)){
					return true;
				}
				
			}
			
			return false;
			
		}
		
		public void Add(ScreenRegion region){
			
			// Add it:
			Regions.Add(region);
			
			// Update bounds:
			float b=region.ScreenMinX;
			
			if(b<MinX_){
				MinX_=b;
			}
			
			b=region.ScreenMinY;
			
			if(b<MinY_){
				MinY_=b;
			}
			
			// Max:
			b=region.ScreenMaxX;
			
			if(b>MaxX_){
				MaxX_=b;
			}
			
			b=region.ScreenMaxY;
			
			if(b>MaxY_){
				MaxY_=b;
			}
			
			
			
		}
		
		public void CopyInto(List<ScreenRegion> set){
			
			for(int i=0;i<Regions.Count;i++){
				set.Add(Regions[i]);
			}
			
		}
		
	}
	
}