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

import { Mathf } from "../Util/Mathf";

import { ParticleManager } from "./ParticleManager";

import AssetController from "./AssetController";
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 GameSetting from "../game_module/GameSetting";

export class Scene3DManager {
    private static instance: Scene3DManager;
    public static get Instance(): Scene3DManager {
        return Scene3DManager.instance || (Scene3DManager.instance = new Scene3DManager());
    }
    public _camera: Laya.Camera;
    public _mainscene: Scene3D;
    public bulletCon: Sprite3D;
    public effectCon: Sprite3D;
    public Init(_s: Scene3D) {
        if (_s) {
            this._mainscene = _s;
            Laya.stage.addChild(_s);
            //Laya.stage.ca
        }
        else {
            console.error("场景不存在");
            return;
        }

        let sceneNode: Sprite3D;
        sceneNode = this._mainscene.getChildByName("scene") as Sprite3D;
        if (!sceneNode) {
            console.error("场景资源配置错误!");
            return;
        }

        this._camera = this._mainscene.getChildByName("Main Camera") as Laya.Camera;

        this._camera.enableHDR = false;
        this.bulletCon = sceneNode.getChildByName("bulletContainer") as Sprite3D;
        this.effectCon = sceneNode.getChildByName("effectPos") as Sprite3D;

        this.InitSceneArg();
        //建筑初始化
        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(GameSetting.isX)ps=startCamposX;
        else ps=startCampos1;
        CameraManager.Instance.Init(this._camera, ps);
        //airplane
        let finalPos = sceneNode.getChildByName("finalPos") as Sprite3D;
        PlaneManager.Instance.Init(finalPos);

        this.INITGAME();
    }

    INITGAME() {
        Laya.timer.frameOnce(10, this, () => {
            MainRoleManager.Instance.CreatePlayer();
            this.InItScene();
        });

    };


    private mat: Laya.SkyBoxMaterial
    private InitSceneArg() {
        this._camera.clearFlag = Laya.BaseCamera.CLEARFLAG_SKY;
        //this._camera.farPlane = 1000;
        // Laya.BaseMaterial.load("subpackage/skyMat/skymat.lmat", Laya.Handler.create(this, function (mat) {
        //     var skyRenderer = this._camera.skyRenderer;
        //     skyRenderer.mesh = Laya.SkyBox.instance;
        //     this.mat = mat as Laya.SkyBoxMaterial;
        //     //  a.exposure = 0.4;
        //     skyRenderer.material = this.mat;
        //     //this.SkyRotate();
        //     // this.mat.rotation =30;
        // }));
        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);
        //设置地面颜色
        //  mat.groundTint = new Laya.Vector4(1,1,1,1);

        // this._mainscene.enableFog = true;
        // this._mainscene.fogStart = 100;
        // this._mainscene.fogRange = 400;
        // this._mainscene.fogColor = new Laya.Vector3(1, 1, 1);
    }
    public SkyRotate() {
        Laya.timer.frameLoop(2, this, this.St);
    }
    private St() {
        if (this.mat) this.mat.rotation += 0.1;
    }


    public InItScene() {
        LevelController.Instance.SetLevel();
        PlaneManager.Instance.SetAir();
        ConstructManager.Instance.SetBuild();
        CameraManager.Instance.SetCamState(CameraState.game, 1);
        MainRoleManager.Instance.InitPlayerPos();
    }
}