import Transform3D = Laya.Transform3D;
import Sprite3D = Laya.Sprite3D;
import Vector3 = Laya.Vector3;
import StreetManager, { ThingArr } from "../Manager/StreetManager";
import { Scene3DManager } from "./Scene3DManager";
import { GamePool } from "./GamePool";
import {MainRoleManager} from "../Manager/MainRoleManager";
import { MathTool } from "../Util/MathTool";
import { GlobalParam } from "../Net/GlobalParam";

import Sprite = Laya.Sprite;
import Stage = Laya.Stage;
import ColorFilter = Laya.ColorFilter;
import Texture = Laya.Texture;
import Browser = Laya.Browser;
import Handler = Laya.Handler;
import WebGL = Laya.WebGL;

export class Street extends Laya.Script3D {

    private selfobj: Sprite3D;
    private tran: Transform3D;

    private child: Transform3D;
    onAwake() {
        this.selfobj = this.owner as Sprite3D;
        this.tran = this.selfobj.transform;

    }

   

    private _transform: Transform3D;
    private rotate: Vector3 = MathTool.VecZero;
    private speed: Vector3 = MathTool.VecZero;
    SetMoveType() {
        if (this.thing.type == 5) {
            this._transform = (this.selfobj.getChildAt(0) as Sprite3D).transform;
        } else {
            this._transform = this.tran;
            if (this.thing.type == 4) {
                StreetManager.Instance.StreetAddCube(this.selfobj);
            }
        }
        if (this.thing.moveType == 1) {
            Laya.timer.frameLoop(3, this, this.CheckRecover);
            return;
        } else if (this.thing.moveType == 2) {
            this.rotate.x = 0;
            this.rotate.y = -this.thing.moveSpeed;
            this.rotate.z = 0;
            Laya.timer.frameLoop(2, this, this.MoveType2);
        } else if (this.thing.moveType == 3) {
            this.rotate.x = 0;
            this.rotate.y = this.thing.moveSpeed;
            this.rotate.z = 0;
            Laya.timer.frameLoop(2, this, this.MoveType2);
        } else if (this.thing.moveType == 4) {
            this.rotate.x = 0;
            this.rotate.y = 0;
            this.rotate.z = this.thing.moveSpeed;
            Laya.timer.frameLoop(1, this, this.MoveType3);
        } else if (this.thing.moveType == 5) {
            this.rotate.x = 0;
            this.rotate.y = 0;
            this.rotate.z = -this.thing.moveSpeed;
            Laya.timer.frameLoop(1, this, this.MoveType3);
        } else if (this.thing.moveType == 6) {
            this.speed.x = -this.thing.moveSpeed;
            this.speed.y = 0;
            this.speed.z = 0;
            Laya.timer.frameLoop(1, this, this.MoveType4);
        } else if (this.thing.moveType == 7) {
            this.speed.x = this.thing.moveSpeed;
            this.speed.y = 0;
            this.speed.z = 0;
            Laya.timer.frameLoop(1, this, this.MoveType4);
        } else if (this.thing.moveType == 8) {

        } else if (this.thing.moveType == 9) {

        }
    }


    /********************************************************** */
    private ApePath: string = "res/apes/monkey2.png";

    private apeTexture: Texture;
    private setup(): void {
        this.normalizeApe();
        this.makeRedApe();
        this.grayingApe();
    }

    private normalizeApe(): void {
        var originalApe: Sprite = this.createApe();

        this.apeTexture = Laya.loader.getRes(this.ApePath);
        originalApe.x = (Laya.stage.width - this.apeTexture.width * 3) / 2;
        originalApe.y = (Laya.stage.height - this.apeTexture.height) / 2;
    }

    private makeRedApe(): void {
        //由 20 个项目（排列成 4 x 5 矩阵）组成的数组，红色
        var redMat: Array<number> =
            [
                1, 0, 0, 0, 0, //R
                0, 0, 0, 0, 0, //G
                0, 0, 0, 0, 0, //B
                0, 0, 0, 1, 0, //A
            ];

        //创建一个颜色滤镜对象,红色
        var redFilter: ColorFilter = new ColorFilter(redMat);

        // 赤化猩猩
        var redApe: Sprite = this.createApe();
        redApe.filters = [redFilter];

        var firstChild: any = Laya.stage.getChildAt(0);
        redApe.x = firstChild.x + this.apeTexture.width;
        redApe.y = firstChild.y;
    }

    private grayingApe(): void {
        //由 20 个项目（排列成 4 x 5 矩阵）组成的数组，灰图
        var grayscaleMat: Array<number> = [0.3086, 0.6094, 0.0820, 0, 0, 0.3086, 0.6094, 0.0820, 0, 0, 0.3086, 0.6094, 0.0820, 0, 0, 0, 0, 0, 1, 0];

        //创建一个颜色滤镜对象，灰图
        var grayscaleFilter: ColorFilter = new ColorFilter(grayscaleMat);

        // 灰度猩猩
        var grayApe: Sprite = this.createApe();
        grayApe.filters = [grayscaleFilter];

        var secondChild: any = Laya.stage.getChildAt(1);
        grayApe.x = secondChild.x + this.apeTexture.width;
        grayApe.y = secondChild.y;
    }

    private createApe(): Sprite {
        var ape: Sprite = new Sprite();
        ape.loadImage("res/apes/monkey2.png");
        Laya.stage.addChild(ape);

        return ape;
    }
    /********************************************************** */

    MoveType4() {
        this._transform.translate(this.speed);
        if (Math.abs(this._transform.localPositionX) >= GlobalParam.blockLRmoveMaxDis) this.speed.x = -this.speed.x;
        this.CheckRecover();
    }
    //  正逆旋转
    MoveType2() {
        this._transform.rotate(this.rotate);
        this.CheckRecover();
    }
    //左右摆动
    MoveType3() {
        this._transform.rotate(this.rotate);
        //console.log(this.ttttt.localRotationEulerZ);
        if (Math.abs(this._transform.localRotationEulerZ) >= 50) this.rotate.z = -this.rotate.z;
        this.CheckRecover();
    }

    Recover() {
        if(this.thing.type){
            StreetManager.Instance.StreetResetCube(this.selfobj.id);
        }
        Laya.timer.clearAll(this);
        StreetManager.Instance.RecoverStreet(this.selfobj);
        GamePool.Instance.RecoverModel(this.selfobj);
    }

    CheckRecover() {
        if (this.tarTran.localPositionZ - this.tran.localPositionZ >= 10) {
            console.log("回收block", this.selfobj.name);
            this.Recover(); return;
        }
    }

    private tarTran: Transform3D;
    private thing: ThingArr;
    SetStreet(thing: ThingArr) {
        this.tran.localPositionZ = Number(thing.z);
        this.tran.localPositionX = Number(thing.x);
        this.tran.rotationEuler = MathTool.VecZero;

        this.thing = thing;
        if (!MainRoleManager.Instance.player) return;
        this.tarTran = MainRoleManager.Instance.player.transform;

        this.SetMoveType();
    }

}