import { _decorator, Button, Component, Node,director ,tween, Vec3, Vec2, UIOpacity, Animation,} from 'cc';
import { resMgr } from './Frames/ResourcesMgr';
import { dataMgr } from './Frames/DataManager';
import { UIBase } from './Game/GameFrameWork/UIBase';
const { ccclass, property } = _decorator;

@ccclass('StartScene')
export class StartScene extends UIBase {
    private _bird:Node = null;
    private _carrot:Node = null;
    private _btnLogin: Node = null;

    private _progressBar:Node = null;
    private _bar:Node = null;
    private _logoPic:Node = null;
    private _loadingPic:Node = null;
    private _startNode: Node = null;
    private _logoNode: Node = null;

    protected onLoad(): void {
        this._startNode = this.node.getChildByName("Start");
        this._logoNode = this.node.getChildByName("Logo");

        this._bird = this.node.getChildByPath("Start/Bird");
        this._carrot = this.node.getChildByPath("Start/Carrot");
        this._btnLogin = this.node.getChildByPath("Start/Button");

        this._progressBar = this.node.getChildByPath("Logo/ProgressBar");
        this._bar = this.node.getChildByPath("Logo/ProgressBar/Bar");
        this._logoPic = this.node.getChildByPath("Logo/LogoBg");
        this._loadingPic = this.node.getChildByPath("Logo/LoadingBg");
        
    }
    async onStart() {
        await dataMgr.loadDataDir("Data");
        await resMgr.loadAllRes("Res");
        await resMgr.loadAllRes("Theme");

        this._logo();

        this._btnLogin.on(Button.EventType.CLICK,()=>{
            director.loadScene("SelectScene");
        })
        this._carrot.setScale(0.5,0.5);
        this._birdAnimation();
    }
    private _logo(){
            let opacityLogo = this._logoPic.getComponent(UIOpacity);
            tween(opacityLogo)
                .call(()=>{opacityLogo.opacity = 10;})
                .to(2,{opacity:255})
                .call(()=>{
                    this._loading();
                    opacityLogo.opacity = 0;})
                .start();
    }
    private _loading(){
        const setOpacity = (component,targetOpacity) => {
            const opacityComponent = component.getComponent(UIOpacity);
            tween(opacityComponent)
                .call(()=>{opacityComponent.opacity = targetOpacity;})
                .start();
        }

        setOpacity(this._loadingPic,255);
        setOpacity(this._progressBar,255);

        tween(this._bar)
            .call(()=>{this._bar.setScale(0,1)})
            .to(2,{scale:new Vec2(2,1)})
            .call(()=>{
                this._logoNode.active = false;
                if(!this._startNode.active){
                    this._startNode.active = true;
                }
            })
            .start();
    }

    private _birdAnimation(){
        let x = this._bird.getPosition().x;
        let y = this._bird.getPosition().y;

        tween(this._bird)
            .repeatForever(
                tween()
                    .to(1,{position : new Vec3(x,y - 10)},{easing : 'sineInOut'})
                    .to(1,{position : new Vec3(x,y + 10)},{easing : 'sineInOut'})
            )
            .start();
    }

    //节点隐藏的时候调用
    // protected onDisable(): void {
    //     this.getComponentsInChildren(Animation).forEach(ani=>{
    //         ani.stop();
    //     });
    // }
    //节点激活的时候调用
    // protected onEnable(): void {
    //     this.getComponentsInChildren(Animation).forEach(ani=>{
    //         ani.play();
    //     })
    // }
    update(deltaTime: number) {
        let x = this._bird.getPosition().x;
        let y = this._bird.getPosition().y;
        this._bird.setPosition(x,y)
    }
}