
import { DispatcherManager } from "../Game/DispatcherManager";
import { ui } from "../ui/layaMaxUI";
import {DisplayerCarController} from "../Net/DisplayerCarController";
import {GuangGao} from "../Net/GuangGao";
import  {ViewManager, ViewType } from "../Net/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 { MathTool } from "../Util/MathTool";

export default class GuideView extends ui.GuideViewUI implements SC {
    private static instance: GuideView;
    static get Instance() {
        return GuideView.instance;
    }
    constructor() {
        super();
        GuideView.instance = this;
        this.Resize();
        DispatcherManager.on(Laya.Event.RESIZE, this, this.Resize);
    }
    OnShow(data?: any) {
        this.RegButton();
        this.RefreshData();
        this.RefreshUI();
    }
    RefreshUI() {

    }
    RefreshData() {
    }
    RegButton() {

    }

    
    
    TwoGuide() {
        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._twoguide);
        this.guide1pos.x = -4.5;
        this.guide1pos.y = 0;
    }
    _twoguide()  {
        let p = MatrixTransform.WorldToScreen2(this._camera, GuideController.Instance.guidetwoPos.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.guidetwoPos.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._twoguide);
            Laya.timer.once(5000,this,()=>{
                GuideController.Instance.GuideOff();
            })
        }
    }


    private playerTran: Laya.Transform3D;
    private _camera: Laya.Camera;
    private isguide2or3 = false;
    private guide1pos: Laya.Vector3 = MathTool.VecZero;
    private guide1Com = false;
    SetGuide(type) {
        if (!this.playerTran) this.playerTran = MainRoleManager.Instance.playerTra;
        if (!this._camera) this._camera = CameraManager.Instance._maincamera;
        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.guideonepos.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.guideonepos.clone();
            Laya.timer.frameLoop(1, this, this.OneGuide);
            this.guide1Com = false;
        } else if (type == 2) {

        }
    }

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

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

    OneGuide() {
        if (!this.guide1Com) {
            let p = MatrixTransform.WorldToScreen2(this._camera, GuideController.Instance.guideonepos.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.guideonepos.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.guideonepos.z) {
                console.log(111);
                Laya.timer.clear(this, this.OneGuide);
                this.TwoGuide();
            }
        }
    }
}