import { Global } from "../Global";
import { LayOUT } from "../utils/LayOUT";

/*
* name;
*/
export class LayoutControl extends Laya.EventDispatcher
{
    private static ins:LayoutControl;
    private _uilayerZorder:number = 0;
    private _upuilayerZorder:number = 200;
    private _tipslayerZorder:number = 1000;
    private _guidelayerZorder:number = 600;

    /**
     * ui层
     */
    private uiLayer:fairygui.GComponent = new fairygui.GComponent();
    
     /**
      * 最定层
      */
    public topLayer:fairygui.GComponent = new fairygui.GComponent();
    
    /**
     * 背景层( 或者3D场景层 )
     */
    public background:fairygui.GComponent = new fairygui.GComponent();

    
    constructor(){
        super();
        LayoutControl.ins = this;
    }

    public static get Instance():LayoutControl
    {
        if( LayoutControl.ins == null )
        {
            return new LayoutControl();
        }
        return LayoutControl.ins;
    }

    
    public AddLayerByName( itemview:fairygui.GObject,layer:string ):void
    {
        if(layer == LayOUT.BG )
        {
            LayoutControl.Instance.background.addChild( itemview );
        }
        else if(layer == LayOUT.UI)
        {
            this._uilayerZorder++;
            LayoutControl.Instance.uiLayer.addChild(itemview);
            itemview.sortingOrder = this._uilayerZorder;
        }
        else if( layer == LayOUT.UITop )
        {
            this._upuilayerZorder++;
            LayoutControl.Instance.uiLayer.addChild( itemview );
            itemview.sortingOrder  = this._upuilayerZorder;
        }
        else if(layer == LayOUT.TIP)
        {
            // GameLayerManager.Instance.tipsLayer.addChild(itemview);
            this._tipslayerZorder++;
            LayoutControl.Instance.uiLayer.addChild( itemview );
            itemview.sortingOrder  = this._tipslayerZorder;
        }
        else if( layer == LayOUT.GUIDE )
        {
            this._guidelayerZorder++;
            LayoutControl.Instance.uiLayer.addChild(itemview);
            itemview.sortingOrder  = this._guidelayerZorder;
        }
    }


    //初始化层级
	public initlayer():void
	{
		Global.stage.addChild( fairygui.GRoot.inst.displayObject );
        fairygui.GRoot.inst.addChild( this.background );
        fairygui.GRoot.inst.addChild( this.uiLayer );
        fairygui.GRoot.inst.addChild( this.topLayer );
    }

    /************************************************ */
    private scene:Laya.Scene3D;
    private tmpVector:Laya.Vector3;
    private mat1:Laya.BlinnPhongMaterial; 
    private mesh1:Laya.Mesh;
    private mesh2:Laya.Mesh;
    public addHorizontalBox(x:number, y:number, z:number):void
    {
        var box:Laya.MeshSprite3D = this.scene.addChild(new Laya.MeshSprite3D(this.mesh2)) as Laya.MeshSprite3D;
        box.meshRenderer.material = this.mat1;
        box.meshRenderer.castShadow = true;
        box.meshRenderer.receiveShadow = true;
        this.tmpVector.setValue(x, y, z);
        box.transform.position = this.tmpVector;
        
        var rigidBody:Laya.Rigidbody3D = box.addComponent(Laya.Rigidbody3D) as Laya.Rigidbody3D;
        rigidBody.mass = 10;
        rigidBody.friction = 1.0;
        rigidBody.restitution = 0.2;
        var boxShape:Laya.BoxColliderShape = new Laya.BoxColliderShape(2, 0.33, 0.5);
        rigidBody.colliderShape = boxShape;
    }

    /************************************************ */

    public ClearLayerByName( layer:string ):void
    {
        switch( layer )
        {
            case LayOUT.UI:
                this._uilayerZorder--;
                if( this._uilayerZorder < 0 )
                {
                    this._uilayerZorder = 0;
                }
            break;
            case LayOUT.UITop:
                this._upuilayerZorder--;
                if( this._upuilayerZorder < 200 )
                {
                    this._upuilayerZorder = 200;
                }
            break;
            case LayOUT.TIP:
                this._tipslayerZorder--;
                if( this._tipslayerZorder < 1000 )
                {
                    this._tipslayerZorder = 1000;
                }
            break;
            case LayOUT.GUIDE:
                this._tipslayerZorder--;
                if( this._tipslayerZorder < 600 )
                {
                    this._tipslayerZorder = 600;
                }
            break;
        }
    }

/******************************* */
    private camera:Laya.Camera;
    private layaMonkey3D:Laya.Sprite3D;
    private layaMonkey2D:Laya.Image;
    private _position = new Laya.Vector3();
    private _outPos = new Laya.Vector3();
    private scaleDelta:number = 0;

    private onComplete() {
        //加载三维地面
        var grid = this.scene.addChild(Laya.Loader.getRes("res/threeDimen/staticModel/grid/plane.lh")) as Laya.Sprite3D;
        //加载二维猴子
        this.layaMonkey2D = Laya.stage.addChild(new Laya.Image("res/threeDimen/monkey.png")) as Laya.Image;
        //开启定时器循环
        Laya.timer.frameLoop(1, this, this.animate);
    }
		
	private animate() {
        //变换位置
        this._position.x = Math.sin(this.scaleDelta += 0.01);
        //计算位置
        this.camera.viewport.project(this._position, this.camera.projectionViewMatrix, this._outPos);
        this.layaMonkey2D.pos(this._outPos.x / Laya.stage.clientScaleX, this._outPos.y / Laya.stage.clientScaleY);
    }	
    /******************************* */
}