import { Engine } from '@babylonjs/core/Engines/engine';
import { Monobehiver } from '../base/Monobehiver';
import { AppConfig } from './AppConfig';

/**
 * 渲染引擎管理类 - 负责创建和管理3D渲染引擎
 */
export class AppEngin extends Monobehiver {
    object: Engine | null;
    canvas: HTMLCanvasElement | null;

    constructor(mainApp: any) {
        super(mainApp);
        this.object = null;
        this.canvas = null;
    }

    Awake(): void {
        this.canvas = AppConfig.container;
        if (!this.canvas) {
            throw new Error('Render canvas not found');
        }
        this.object = new Engine(this.canvas, true, {
            preserveDrawingBuffer: false, // 不保留绘图缓冲区
            stencil: true,                // 启用模板缓冲
            alpha: true                   // 启用透明背景
        });
        this.object.setSize(window.innerWidth, window.innerHeight);
        this.object.setHardwareScalingLevel(1); // 1:1像素比例
    }

    /** 处理窗口大小变化 */
    handleResize(): void {
        if (this.object) {
            this.object.setSize(window.innerWidth, window.innerHeight);
            this.object.resize();
        }
    }
}
