
import { DispatcherManager } from "../GameLogic/DispatcherManager";
import { ui } from "../ui/layaMaxUI";
import DisplayerCarController from "../game_module/DisplayerCarController";
import BannerAndVideo1 from "../game_module/BannerAndVideo1";
import ViewManager, { ViewType } from "../game_module/ViewManager";
import MateManager from "../Manager/MateManager";
import MainRoleManager from "../Manager/MainRoleManager";
import GuideController from "../Manager/GuideController";
import { MatrixTransform } from "../Util/MatrixTransform";
import CameraManager from "../Manager/CameraManager";
import { Mathf } from "../Util/Mathf";

export default class GuideView extends ui.GuideViewUI implements UI_Interface {
    private static instance: GuideView;
    static get Instance() {
        return GuideView.instance;
    }
    constructor() {
        super();
        GuideView.instance = this;
        this.resizeHander();
        DispatcherManager.on(Laya.Event.RESIZE, this, this.resizeHander);
    }
    OnShow(data?: any) {
        this.InitHandler();
        this.InitDate();
        this.InitHandler();
        this.InitUi();
    }
    InitUi() {

    }
    InitDate() {
    }
    InitHandler() {

    }

    private playerTran: Laya.Transform3D;
    private _camera: Laya.Camera;
    private isguide2or3 = false;
    private guide1pos: Laya.Vector3 = Mathf.VecZero;
    private guide1Com = false;
    SetGuide(type) {
        if (!this.playerTran) this.playerTran = MainRoleManager.Instance.playerTra;
        if (!this._camera) this._camera = CameraManager.Instance._camer;
        if (type == 1) {
            this.guide1Image.visible = true;
            this.guide2Image.visible = false;
            this.guide3Image.visible = false;
            this.guide1Box.visible = true;
            this.guide1.play(0, true);
            this.guide4Box.visible = false;
            if (this.playerTran.localPositionX <= GuideController.Instance.guide1pos.x) {
                this.guide2Box.visible = true;
                this.guide3Box.visible = false;
                this.isguide2or3 = true;
                this.guide2.play(0, true);
            } else {
                this.guide2Box.visible = false;
                this.guide3Box.visible = true;
                this.isguide2or3 = false;
                this.guide3.play(0, true);
            }
            this.guide1pos = GuideController.Instance.guide1pos.clone();
            Laya.timer.frameLoop(1, this, this._guide1);
            this.guide1Com = false;
        } else if (type == 2) {

        }
    }
    _guide1() {
        if (!this.guide1Com) {
            let p = MatrixTransform.WorldToScreen2(this._camera, GuideController.Instance.guide1pos.clone());
            this.guide1Box.pos(p.x, p.y);
            this.guide1pos.z = this.playerTran.localPositionZ + 10;
            let p1 = MatrixTransform.WorldToScreen2(this._camera, this.guide1pos);
            if (this.isguide2or3) {
                this.guide2Box.pos(p1.x, p1.y);
            } else {
                this.guide3Box.pos(p1.x, p1.y);
            }
            if (this.playerTran.localPositionZ >= GuideController.Instance.guide1pos.z - 5) {
                this.guide2.stop();
                this.guide3.stop();
                this.guide2Box.visible = false;
                this.guide3Box.visible = false;;
                this.guide1Box.visible = false;
                this.guide1Com = true;
                this.guide1Image.visible = false;
            }
        } else {
            if (this.playerTran.localPositionZ >= GuideController.Instance.guide1pos.z) {
                console.log(111);
                Laya.timer.clear(this, this._guide1);
                this.Guide2();
            }
        }

    }
    Guide2() {
        this.guide3Box.visible = true;
        this.guide4Box.visible = true;
        this.guide2Image.visible = true;
        this.guide3.play(0, true);
        this.guide4.play(0, true);
        Laya.timer.frameLoop(1, this, this._guide2);
        this.guide1pos.x = -4.5;
        this.guide1pos.y = 0;

    }
    _guide2()  {
        let p = MatrixTransform.WorldToScreen2(this._camera, GuideController.Instance.guide2Pos.clone());
        this.guide4Box.pos(p.x, p.y);
        this.guide1pos.z = this.playerTran.localPositionZ + 5;
        let p1 = MatrixTransform.WorldToScreen2(this._camera, this.guide1pos);
        this.guide3Box.pos(p1.x, p1.y);
        if (this.playerTran.localPositionZ >= GuideController.Instance.guide2Pos.z - 10) {
            this.guide3Box.visible = false;
            this.guide4Box.visible = false;
            this.guide2Image.visible=false;
            this.guide3Image.visible = true;
            this.guide3.stop();
            this.guide4.stop();
            Laya.timer.clear(this,this._guide2);
            Laya.timer.once(5000,this,()=>{
                GuideController.Instance.OverGuide();
            })
        }

    }



    OnClose() {
        DispatcherManager.off(Laya.Event.RESIZE, this, this.resizeHander);
        GuideView.instance = null;
        Laya.timer.clearAll(this);
    }

    resizeHander() {
        this.height = Laya.stage.height;
    }
}