import Scene3D = Laya.Scene3D;
import Sprite3D = Laya.Sprite3D;
import  { CameraState,Camera } from "./Camera";
import Transform3D = Laya.Transform3D;

import { ParticleManager } from "./ParticleManager";

import {ResourcesManager} from "./ResourcesManager";
import {GameTool} from "../Util/GameTool";
import {GameManager} from "./GameManager";
import {ConstructManager} from "../Manager/ConstructManager";
import {MainRoleManager} from "../Manager/MainRoleManager";
import {MoundManager} from "../Manager/MoundManager";
import {CameraManager} from "../Manager/CameraManager";
import {PlaneManager} from "../Manager/PlaneManager";
import {LevelController} from "./LevelController";
import StreetManager from "../Manager/StreetManager";
import {GameVersion} from "../Net/GameVersion";


import Sprite = Laya.Sprite;
import Stage = Laya.Stage;
import BlurFilter = Laya.BlurFilter;
import Browser = Laya.Browser;
import Handler = Laya.Handler;
import WebGL = Laya.WebGL;

export class Scene3DManager {
    private static instance: Scene3DManager;
    public static get Instance(): Scene3DManager {
        return Scene3DManager.instance || (Scene3DManager.instance = new Scene3DManager());
    }
    
    InitGame() {
        Laya.timer.frameOnce(10, this, () => {
            MainRoleManager.Instance.MadePlayer();
            this.InItScene();
        });

    };
    public Init(_s: Scene3D) {
        if (_s) {
            this.MainScene = _s;
            Laya.stage.addChild(_s);
        }
        else {
            console.error("场景不存在");
            return;
        }

        let sceneNode: Sprite3D;
        sceneNode = this.MainScene.getChildByName("scene") as Sprite3D;
        if (!sceneNode) {
            console.error("场景资源配置错误!");
            return;
        }
        this.MainCamera = this.MainScene.getChildByName("Main Camera") as Laya.Camera;
        this.MainCamera.enableHDR = false;
        this.bulletCon = sceneNode.getChildByName("bulletContainer") as Sprite3D;
        this.effectCon = sceneNode.getChildByName("effectPos") as Sprite3D;
        this.SetSceneMaterial();
        //建筑初始化
        let build = sceneNode.getChildByName("buildings") as Sprite3D;
        ConstructManager.Instance.Init(build);
        //player初始化
        let playerStartPos = sceneNode.getChildByName("playerstartPoint") as Sprite3D;
        let playerCon = sceneNode.getChildByName("playerContainer") as Sprite3D;
        MainRoleManager.Instance.Init(playerStartPos, playerCon);

        //moveBlock
        let moveBlockContainer = sceneNode.getChildByName("BlockContainer") as Sprite3D;
        StreetManager.Instance.Init(moveBlockContainer);

        //tower
        let towerContainer = sceneNode.getChildByName("towerContainer") as Sprite3D;
        MoundManager.Instance.Init(towerContainer);

        //camer
        let startCampos1 = sceneNode.getChildByName("startCamPos") as Sprite3D;
        let startCamposX=sceneNode.getChildByName("startXCamPos") as Sprite3D;
        let ps;
        if(GameVersion.isX)ps=startCamposX;
        else ps=startCampos1;
        CameraManager.Instance.Init(this.MainCamera, ps);
        //airplane
        let finalPos = sceneNode.getChildByName("finalPos") as Sprite3D;
        PlaneManager.Instance.Init(finalPos);
        this.InitGame();
    }

    /*********************************************************** */
    private apePath: string = "res/apes/monkey2.png";
    private createApe(): void {
        var ape: Sprite = new Sprite();
        ape.loadImage(this.apePath);

        ape.x = (Laya.stage.width - ape.width) / 2;
        ape.y = (Laya.stage.height - ape.height) / 2;

        Laya.stage.addChild(ape);

        this.applayFilter(ape);
    }

    private applayFilter(ape: Sprite): void {
        var blurFilter: BlurFilter = new BlurFilter();
        blurFilter.strength = 5;
        ape.filters = [blurFilter];
    }
    /*********************************************************** */

    public MainCamera: Laya.Camera;
    public MainScene: Scene3D;
    public bulletCon: Sprite3D;
    public effectCon: Sprite3D;

    private mat: Laya.SkyBoxMaterial
    private SetSceneMaterial() {
        this.MainCamera.clearFlag = Laya.BaseCamera.CLEARFLAG_SKY;
        let skyRenderer = this.MainScene.skyRenderer;
        skyRenderer.mesh = Laya.SkyDome.instance;
        let mat = new Laya.SkyProceduralMaterial();
        skyRenderer.material = mat;
        //设置太阳状态为高质量状态
        mat.sunDisk = Laya.SkyProceduralMaterial.SUN_NODE;
        //设置天空颜色
        mat.skyTint = new Laya.Vector4(1, 0.74, 0.74, 1);
    }
    
    public InItScene() {
        LevelController.Instance.SetLevel();
        PlaneManager.Instance.SetAir();
        ConstructManager.Instance.SetConstruct();
        CameraManager.Instance.SetCameraAction(CameraState.game, 1);
        MainRoleManager.Instance.SetPlayerPosition();
    }
}