import { Component, game, Game, ResolutionPolicy, screen, Size, UITransform, view, _decorator } from "cc";
import { AudioManager } from './audio/AudioManager';
import { Audio_Conf } from "./config/AudioConf";
import { FrameworkConf } from "./config/FrameworkConf";
import { Layer_Conf } from "./config/LayerConf";
import { Framework } from './Framework';
import { LayerManager } from "./layer/LayerManager";
import { TimeManager } from "./time/TimeManager";
import { TipsManager } from "./tips/TipsManager";
const { ccclass, requireComponent, property } = _decorator;

@ccclass('Root')
@requireComponent(UITransform)
export class Root extends Component {

    private _transform: UITransform = null;
    private _portrait_size: Size = null;
    private _landscape_szie: Size = null!;

    onLoad() {
        Framework.storage.init(FrameworkConf.storage_key, FrameworkConf.storage_iv);

        this._transform = this.node.getComponent(UITransform);
        if (view.getDesignResolutionSize().width > view.getDesignResolutionSize().height) {
            this._landscape_szie = view.getDesignResolutionSize();
            this._portrait_size = new Size(this._landscape_szie.height, this._landscape_szie.width);
        } else {
            this._portrait_size = view.getDesignResolutionSize();
            this._landscape_szie = new Size(this._portrait_size.height, this._portrait_size.width);
        }

        Framework.time = new TimeManager(this);
        Framework.audio = AudioManager.instance;
        Framework.audio.init(Audio_Conf);
        Framework.layer = new LayerManager(this.node, Layer_Conf);
        Framework.tips = new TipsManager(this.node);

        game.on(Game.EVENT_SHOW, () => {
            Framework.event.fireEvent(FrameworkConf.Event.GAME_SHOW);
            console.log("Game.EVENT_SHOW")
        });

        game.on(Game.EVENT_HIDE, () => {
            Framework.event.fireEvent(FrameworkConf.Event.GAME_HIDE);
            console.log("Game.EVENT_HIDE")
        });

        screen.on("window-resize",()=>{
            console.log("window-resize")
            this.resize();
            Framework.event.fireEvent(FrameworkConf.Event.GAME_RESIZE);
        })

        document.addEventListener('visibilitychange', function() {
            if (document.hidden) {
                console.log('用户已经切换到后台');
            } else {
                console.log('用户已经切换回前台');
            }
        });
        // view.setResizeCallback(() => {
        //     this.resize();
        //     Framework.event.fireEvent(FrameworkConf.Event.GAME_RESIZE);
        // });

        this.resize();

        console.log("Framework初始化完成");
        Framework.event.fireEvent(FrameworkConf.Event.INIT_FINISH);
    }

    //屏幕适配
    resize() {
        let dr = (view.getDesignResolutionSize().width > view.getDesignResolutionSize().height) ? this._landscape_szie : this._portrait_size;

        let s = screen.windowSize;
        let rw = s.width;
        let rh = s.height;
        let finalW = rw;
        let finalH = rh;

        if ((rw / rh) > (dr.width / dr.height)) {
            finalH = dr.height;
            finalW = finalH * rw / rh;
        } else {
            finalW = dr.width;
            finalH = finalW * rh / rw;
        }
        view.setDesignResolutionSize(finalW, finalH, ResolutionPolicy.UNKNOWN);
        this._transform!.width = finalW;
        this._transform!.height = finalH;
    }
}