import './bim-engine.css';
import {ToolbarManager} from './managers/toolbar-manager';
import {ButtonGroupManager} from './managers/button-group-manager';
import {DialogManager} from './managers/dialog-manager';
import {EngineManager} from './managers/engine-manager';
import {RightKeyManager} from './managers/right-key-manager';
import {ConstructTreeManagerBtn} from './managers/construct-tree-manager-btn';
import {PropertyPanelManager} from './managers/property-panel-manager';
import {MeasureDialogManager} from './managers/measure-dialog-manager';
import type {EngineOptions, ModelLoadOptions} from './components/engine';
import {localeManager} from './services/locale';
import {themeManager} from './services/theme';
import type {LocaleType} from './locales/types';
import type {ThemeType, ThemeConfig} from './themes/types';
import {EventEmitter} from './core/event-emitter';
import {EngineEvents} from './types/events';

export type {EngineOptions, ModelLoadOptions};

export class BimEngine extends EventEmitter {
    public container: HTMLElement;
    private wrapper: HTMLElement | null = null;

    public toolbar: ToolbarManager | null = null;      // 底部专用
    public constructTreeBtn: ConstructTreeManagerBtn | null = null;      // 底部专用
    public buttonGroup: ButtonGroupManager | null = null; // 通用
    public dialog: DialogManager | null = null;
    public engine: EngineManager | null = null;    // 3D 引擎管理器
    public rightKey: RightKeyManager | null = null; // 右键菜单管理器
    public propertyPanel: PropertyPanelManager | null = null; // 属性面板 (演示 Collapse)
    public measure: MeasureDialogManager | null = null; // 测量面板


    constructor(
        container: HTMLElement | string,
        options?: {
            locale?: LocaleType;
            theme?: ThemeType;
        }
    ) {
        super();
        const el = typeof container === 'string' ? document.getElementById(container) : container;
        if (!el) throw new Error('Container not found');
        this.container = el;

        if (options?.locale) localeManager.setLocale(options.locale);
        if (options?.theme) {
            if (options.theme === 'custom') {
                console.warn('Custom theme should be set via setCustomTheme().');
            } else {
                themeManager.setTheme(options.theme);
            }
        }

        this.init();
    }

    // Typed wrappers for events
    public emit<K extends keyof EngineEvents>(event: K, payload: EngineEvents[K]) {
        super.emit(event, payload);
    }

    public on<K extends keyof EngineEvents>(event: K, listener: (payload: EngineEvents[K]) => void): () => void {
        return super.on(event, listener);
    }

    public setLocale(locale: LocaleType) {
        localeManager.setLocale(locale);
    }

    public getLocale(): LocaleType {
        return localeManager.getLocale();
    }

    public setTheme(theme: 'dark' | 'light') {
        themeManager.setTheme(theme);
    }

    public setCustomTheme(theme: ThemeConfig) {
        themeManager.setCustomTheme(theme);
    }

    private init() {
        this.container.innerHTML = '';
        this.wrapper = document.createElement('div');
        this.wrapper.className = 'bim-engine-wrapper';
        this.container.appendChild(this.wrapper);

        // 创建 3D 引擎管理器
        this.engine = new EngineManager(this, this.wrapper);
        this.dialog = new DialogManager(this, this.wrapper);
        this.toolbar = new ToolbarManager(this, this.wrapper);
        this.buttonGroup = new ButtonGroupManager(this, this.wrapper);
        this.rightKey = new RightKeyManager(this, this.wrapper);
        this.constructTreeBtn = new ConstructTreeManagerBtn(this, this.wrapper);
        this.propertyPanel = new PropertyPanelManager(this);
        this.measure = new MeasureDialogManager(this);

        // 初始主题
        this.updateTheme(themeManager.getTheme());
        // 订阅主题变化
        themeManager.subscribe((theme) => {
            this.updateTheme(theme);
        });
    }


    private updateTheme(theme: ThemeConfig) {
        if (this.wrapper) {
            this.wrapper.style.backgroundColor = theme.background;
            this.wrapper.style.color = theme.textPrimary;
        }
    }

    public destroy() {
        this.toolbar?.destroy();
        this.buttonGroup?.destroy();
        this.engine?.destroy();
        this.dialog?.destroy();
        this.rightKey?.destroy();
        this.propertyPanel?.destroy();
        this.measure?.destroy();
        this.container.innerHTML = '';
        this.clear();
    }
}
