import RectangleUtil from "./../../starling/utils/RectangleUtil";
import Matrix from "openfl/geom/Matrix";
import Matrix3D from "openfl/geom/Matrix3D";
import MatrixUtil from "./../../starling/utils/MatrixUtil";
import Pool from "./../../starling/utils/Pool";
import MathUtil from "./../../starling/utils/MathUtil";
import ArgumentError from "openfl/errors/ArgumentError";
import Vector from "openfl/Vector";
import Vector3D from "openfl/geom/Vector3D";
import Texture from "./../textures/Texture";
import TextureBase from "openfl/display3D/textures/TextureBase";
import Rectangle from "openfl/geom/Rectangle";

declare namespace starling.rendering
{
	/** The RenderState stores a combination of settings that are currently used for rendering.
	 *  This includes modelview and transformation matrices as well as context3D related settings.
	 *
	 *  <p>Starling's Painter instance stores a reference to the current RenderState.
	 *  Via a stack mechanism, you can always save a specific state and restore it later.
	 *  That makes it easy to write rendering code that doesn't have any side effects.</p>
	 *
	 *  <p>Beware that any context-related settings are not applied on the context
	 *  right away, but only after calling <code>painter.prepareToDraw()</code>.
	 *  However, the Painter recognizes changes to those settings and will finish the current
	 *  batch right away if necessary.</p>
	 *
	 *  <strong>Matrix Magic</strong>
	 *
	 *  <p>On rendering, Starling traverses the display tree, constantly moving from one
	 *  coordinate system to the next. Each display object stores its vertex coordinates
	 *  in its local coordinate system; on rendering, they must be moved to a global,
	 *  2D coordinate space (the so-called "clip-space"). To handle these calculations,
	 *  the RenderState contains a set of matrices.</p>
	 *
	 *  <p>By multiplying vertex coordinates with the <code>modelviewMatrix</code>, you'll get the
	 *  coordinates in "screen-space", or in other words: in stage coordinates. (Optionally,
	 *  there's also a 3D version of this matrix. It comes into play when you're working with
	 *  <code>Sprite3D</code> containers.)</p>
	 *
	 *  <p>By feeding the result of the previous transformation into the
	 *  <code>projectionMatrix</code>, you'll end up with so-called "clipping coordinates",
	 *  which are in the range <code>[-1, 1]</code> (just as needed by the graphics pipeline).
	 *  If you've got vertices in the 3D space, this matrix will also execute a perspective
	 *  projection.</p>
	 *
	 *  <p>Finally, there's the <code>mvpMatrix</code>, which is short for
	 *  "modelviewProjectionMatrix". This is simply a combination of <code>modelview-</code> and
	 *  <code>projectionMatrix</code>, combining the effects of both. Pass this matrix
	 *  to the vertex shader and all your vertices will automatically end up at the right
	 *  position.</p>
	 *
	 *  @see Painter
	 *  @see starling.display.Sprite3D
	 */
	export class RenderState
	{
		/** Creates a new render state with the default settings. */
		public constructor();
	
		/** Duplicates all properties of another instance on the current instance. */
		public copyFrom(renderState:RenderState):void;
	
		/** Resets the RenderState to the default settings.
		 *  (Check each property documentation for its default value.) */
		public reset():void;
	
		// matrix methods / properties
	
		/** Prepends the given matrix to the 2D modelview matrix. */
		public transformModelviewMatrix(matrix:Matrix):void;
	
		/** Prepends the given matrix to the 3D modelview matrix.
		 *  The current contents of the 2D modelview matrix is stored in the 3D modelview matrix
		 *  before doing so; the 2D modelview matrix is then reset to the identity matrix.
		 */
		public transformModelviewMatrix3D(matrix:Matrix3D):void;
	
		/** Creates a perspective projection matrix suitable for 2D and 3D rendering.
		 *
		 *  <p>The first 4 parameters define which area of the stage you want to view (the camera
		 *  will 'zoom' to exactly this region). The final 3 parameters determine the perspective
		 *  in which you're looking at the stage.</p>
		 *
		 *  <p>The stage is always on the rectangle that is spawned up between x- and y-axis (with
		 *  the given size). All objects that are exactly on that rectangle (z equals zero) will be
		 *  rendered in their true size, without any distortion.</p>
		 *
		 *  <p>If you pass only the first 4 parameters, the camera will be set up above the center
		 *  of the stage, with a field of view of 1.0 rad.</p>
		 */
		public setProjectionMatrix(x:number, y:number, width:number, height:number,
											stageWidth?:number, stageHeight?:number,
											cameraPos?:Vector3D):void;
	
		/** This method needs to be called whenever <code>projectionMatrix3D</code> was changed
		 *  other than via <code>setProjectionMatrix</code>.
		 */
		public setProjectionMatrixChanged():void;
	
		/** Changes the modelview matrices (2D and, if available, 3D) to identity matrices.
		 *  An object transformed an identity matrix performs no transformation.
		 */
		public setModelviewMatricesToIdentity():void;
	
		/** Returns the current 2D modelview matrix.
		 *  CAUTION: Use with care! Each call returns the same instance.
		 *  @default identity matrix */
		public modelviewMatrix:Matrix;
		protected get_modelviewMatrix():Matrix;
		protected set_modelviewMatrix(value:Matrix):Matrix;
	
		/** Returns the current 3D modelview matrix, if there have been 3D transformations.
		 *  CAUTION: Use with care! Each call returns the same instance.
		 *  @default null */
		public modelviewMatrix3D:Matrix3D;
		protected get_modelviewMatrix3D():Matrix3D;
		protected set_modelviewMatrix3D(value:Matrix3D):Matrix3D;
	
		/** Returns the current projection matrix. You can use the method 'setProjectionMatrix3D'
		 *  to set it up in an intuitive way.
		 *  CAUTION: Use with care! Each call returns the same instance. If you modify the matrix
		 *           in place, you have to call <code>setProjectionMatrixChanged</code>.
		 *  @default identity matrix */
		public projectionMatrix3D:Matrix3D;
		protected get_projectionMatrix3D():Matrix3D;
		protected set_projectionMatrix3D(value:Matrix3D):Matrix3D;
	
		/** Calculates the product of modelview and projection matrix and stores it in a 3D matrix.
		 *  CAUTION: Use with care! Each call returns the same instance. */
		public readonly mvpMatrix3D:Matrix3D;
		protected get_mvpMatrix3D():Matrix3D;
	
		// other methods
	
		/** Changes the the current render target.
		 *
		 *  @param target     Either a texture or <code>null</code> to render into the back buffer.
		 *  @param enableDepthAndStencil  Indicates if depth and stencil testing will be available.
		 *                    This parameter affects only texture targets.
		 *  @param antiAlias  The anti-aliasing quality (range: <code>0 - 4</code>).
		 *                    This parameter affects only texture targets. Note that at the time
		 *                    of this writing, AIR supports anti-aliasing only on Desktop.
		 */
		public setRenderTarget(target:Texture, enableDepthAndStencil?:boolean,
										antiAlias?:number):void;
	
		// other properties
	
		/** The current, cumulated alpha value. Beware that, in a standard 'render' method,
		 *  this already includes the current object! The value is the product of current object's
		 *  alpha value and all its parents. @default 1.0
		 */
		public alpha:number;
		protected get_alpha():number;
		protected set_alpha(value:number):number;
	
		/** The blend mode to be used on rendering. A value of "auto" is ignored, since it
		 *  means that the mode should remain unchanged.
		 *
		 *  @default BlendMode.NORMAL
		 *  @see starling.display.BlendMode
		 */
		public blendMode:string;
		protected get_blendMode():string;
		protected set_blendMode(value:string):string;
	
		/** The texture that is currently being rendered into, or <code>null</code>
		 *  to render into the back buffer. On assignment, calls <code>setRenderTarget</code>
		 *  with its default parameters. */
		public renderTarget:Texture;
		protected get_renderTarget():Texture;
		protected set_renderTarget(value:Texture):Texture;
	
		/** @protected */
		/*@:allow(starling)*/ protected readonly renderTargetBase:TextureBase;
		protected get_renderTargetBase():TextureBase;
	
		/** @protected */
		/*@:allow(starling)*/ readonly protected renderTargetOptions:number;
		protected get_renderTargetOptions():number;
	
		/** Sets the triangle culling mode. Allows to exclude triangles from rendering based on
		 *  their orientation relative to the view plane.
		 *  @default Context3DTriangleFace.NONE
		 */
		public culling:string;
		protected get_culling():string;
		protected set_culling(value:string):string;
	
		/** Enables or disables depth buffer writes.
			*  @default false
			*/
		public depthMask:boolean;
		protected get_depthMask():boolean;
		protected set_depthMask(value:boolean):boolean;
	
		/** Sets type of comparison used for depth testing.
			*  @default Context3DCompareMode.ALWAYS
			*/
		public depthTest:string;
		protected get_depthTest():string;
		protected set_depthTest(value:string):string;
	
		/** The clipping rectangle can be used to limit rendering in the current render target to
		 *  a certain area. This method expects the rectangle in stage coordinates. To prevent
		 *  any clipping, assign <code>null</code>.
		 *
		 *  @default null
		 */
		public clipRect:Rectangle;
		protected get_clipRect():Rectangle;
		protected set_clipRect(value:Rectangle):Rectangle;
	
		/** The anti-alias setting used when setting the current render target
		 *  via <code>setRenderTarget</code>. */
		public readonly renderTargetAntiAlias:number;
		protected get_renderTargetAntiAlias():number;
	
		/** Indicates if the render target (set via <code>setRenderTarget</code>)
		 *  has its depth and stencil buffers enabled. */
		public readonly renderTargetSupportsDepthAndStencil:boolean;
		protected get_renderTargetSupportsDepthAndStencil():boolean;
	
		/** Indicates if there have been any 3D transformations.
		 *  Returns <code>true</code> if the 3D modelview matrix contains a value. */
		public readonly is3D:boolean;
		protected get_is3D():boolean;
	
		/** @private
		 *
		 *  This callback is executed whenever a state change requires a draw operation.
		 *  This is the case if blend mode, render target, culling or clipping rectangle
		 *  are changing. */
		/*@:allow(starling)*/ protected onDrawRequired:()=>void;
		protected get_onDrawRequired():()=>void;
		protected set_onDrawRequired(value:()=>void):()=>void;
	}
}

export default starling.rendering.RenderState;