import {SceneControl} from "../Control/SceneControl";
import { AssetPath } from "../Config/AssetPath";
import {ResDataControl} from "../Control/ResDataControl";
import {PropManager} from "../PrefabControl/PropManager";
import { GameUtils } from "../utils/GameUtils";
import GameCFG from "../CFG/GameCFG";

import Sprite = Laya.Sprite;
	import Stage = Laya.Stage;
	import MapLayer = Laya.MapLayer;
	import TiledMap = Laya.TiledMap;
	import Point = Laya.Point;
	import Rectangle = Laya.Rectangle;
	import Handler = Laya.Handler;
    import WebGL = Laya.WebGL;
    

export class CameraManager extends Laya.Script3D
{
    protected camera: Laya.Camera;
    private _transform:Laya.Transform3D;
    private _disVector:Laya.Vector3;
    private _toVec:Laya.Vector3;
    public iscanfollow:boolean = false;
    constructor() {
        super();
    }

    public onAwake():void{
        //获取脚本所属对象
        this.camera = this.owner as Laya.Camera;
        this._toVec = new Laya.Vector3();
        this.camera.fieldOfView = 90;
    }

    

    public onStart():void{
        this._transform = this.camera.transform;
        //旋转摄像机角度
        this._transform.rotate(new Laya.Vector3( 0, 0, 0), true, false);
        // this.camera.clearColor = null;
    }

    /***************************************************************** */
    private tiledMap: TiledMap;
		private layer: MapLayer;
		private sprite: Sprite;
    private createMap(): void {
        this.tiledMap = new TiledMap();
        this.tiledMap.createMap("res/tiledMap/isometric_grass_and_water.json", new Rectangle(0, 0, Laya.stage.width, Laya.stage.height), Handler.create(this, this.mapLoaded), null, new Point(1600, 800));
    }

    private onStageClick(): void {
        var p: Point = new Point(0, 0);
        this.layer.getTilePositionByScreenPos(Laya.stage.mouseX, Laya.stage.mouseY, p);
        this.layer.getScreenPositionByTilePos(Math.floor(p.x), Math.floor(p.y), p);
        this.sprite.pos(p.x, p.y);
    }

    private mapLoaded(): void {
        this.layer = this.tiledMap.getLayerByIndex(0);

        var radiusX: number = 32;
        var radiusY: number = Math.tan(180 / Math.PI * 30) * radiusX;
        var color: string = "#FF7F50";

        this.sprite = new Sprite();
        this.sprite.graphics.drawLine(0, 0, -radiusX, radiusY, color);
        this.sprite.graphics.drawLine(0, 0, radiusX, radiusY, color);
        this.sprite.graphics.drawLine(-radiusX, radiusY, 0, radiusY * 2, color);
        this.sprite.graphics.drawLine(radiusX, radiusY, 0, radiusY * 2, color);
        Laya.stage.addChild(this.sprite);
    }
    /***************************************************************** */

    private _upvec:Laya.Vector3 = new Laya.Vector3( 0,1,0 );
    private _index:number = 0;
    private _slerpValue:number = 8;
    private _lookatVec:Laya.Vector3 = new Laya.Vector3;
    public onUpdate(): void
    {
        if( this.iscanfollow )
        {
            if( PropManager.iscarmanpc )
            {
                if( PropManager.npcDict["npc_0"] )
                {
                    Laya.Vector3.add( PropManager.npcDict["npc_0"].transform.position,this._disVector,this._toVec );
                    this._transform.position = this._toVec;
                    this._transform.position.x = PropManager.npcDict["npc_0"].transform.position.x;
                        this._transform.lookAt( PropManager.npcDict["npc_0"].transform.position,this._upvec );
                }
            }
            else
            {
                if( PropManager.mainRole )
                {
                    Laya.Vector3.add( PropManager.mainRole.transform.position,this._disVector,this._toVec );
                    // this._toVec.x = PropManager.mainRole.transform.position.x;
                    // this._transform.position = this._toVec;
                    // this._transform.lookAt( PropManager.mainRole.transform.position,this._upvec );

                    Laya.Vector3.lerp( this._transform.position,this._toVec,16/1000 * this._slerpValue,this._toVec );
                    // this._toVec.z = PropManager.mainRole.transform.position.z+this._disVector.z;
                    // this._toVec.y = PropManager.mainRole.transform.position.y + this._disVector.y;
                    this._transform.position = this._toVec;
                    this._lookatVec.x = this._toVec.x;
                    this._lookatVec.y = PropManager.mainRole.transform.position.y;
                    this._lookatVec.z = PropManager.mainRole.transform.position.z;
                    this._transform.lookAt( this._lookatVec,this._upvec );
                }
            }
        }
    }

    public GameEndRotate(targetPos:Laya.Vector3, speed:Laya.Vector3 ):void
    {
        this.iscanfollow = false;
        this._transform.translate( speed,false );
        this._transform.lookAt( targetPos,this._upvec );
    }



    public SetCameraPosition( vec:Laya.Vector3 ):void
    {
        var valueArr:Array<string> = GameUtils.cutstrtoArr( GameCFG.dict[1].value );
        this._disVector = new Laya.Vector3(Number( valueArr[0] ),Number( valueArr[1] ),Number( valueArr[2] ));
        Laya.Vector3.add(vec ,this._disVector,this._toVec );
        this.camera.transform.position = this._toVec;
        this.camera.transform.position.x = vec.x;
        // this.camera.transform.lookAt( vec,new Laya.Vector3( 0,1,0 ) );
    }

}