import type { ButtonGroupColors, ButtonConfig } from '../components/button-group/index.type';
import { Toolbar } from '../components/button-group/toolbar';
import type { ThemeConfig } from '../themes/types';
import { BimComponent } from '../core/component';
import type { BimEngine } from '../bim-engine';

/**
 * 底部工具栏管理器 (ToolbarManager)
 * 仅负责管理底部工具栏实例。
 */
export class ToolbarManager extends BimComponent {
    private toolbar: Toolbar | null = null;
    private toolbarContainer: HTMLElement | null = null;
    private container: HTMLElement;

    constructor(engine: BimEngine, container: HTMLElement) {
        super(engine);
        this.container = container;
        this.init();
    }

    private init() {
        // 创建底部工具栏专用容器
        this.toolbarContainer = document.createElement('div');
        this.toolbarContainer.id = 'opt-btn-groups';
        this.toolbarContainer.className = 'bim-engine-opt-btn-container is-bottom-toolbar';
        this.container.appendChild(this.toolbarContainer);

        this.toolbar = new Toolbar({
            container: this.toolbarContainer,
            showLabel: true,
            direction: 'row',
            position: 'bottom-center', // 底部居中
            align: 'vertical',         // 图标在上
            expand: 'up'               // 向上展开
        });

        // 注入 engine 到 Toolbar
        // @ts-ignore - Toolbar 还没更新类型，暂时忽略
        this.toolbar.setEngine(this.engine);

        this.toolbar.init();
    }

    public updateTheme(theme: ThemeConfig) {
        this.toolbar?.setTheme(theme);
    }

    public refresh() {
        this.toolbar?.render();
    }

    public destroy() {
        this.toolbar?.destroy();
        this.toolbar = null;
    }

    // --- 转发 API ---
    public addGroup(groupId: string, beforeGroupId?: string) { this.toolbar?.addGroup(groupId, beforeGroupId); this.toolbar?.render(); }
    public addButton(config: ButtonConfig) { this.toolbar?.addButton(config); this.toolbar?.render(); }
    public setButtonVisibility(id: string, v: boolean) { this.toolbar?.updateButtonVisibility(id, v); }
    public setShowLabel(show: boolean) { this.toolbar?.setShowLabel(show); }
    public setBtnActive(id: string, active?: boolean) { this.toolbar?.setBtnActive(id, active); }
    public setVisible(visible: boolean) {
        if (this.toolbarContainer) {
            this.toolbarContainer.style.visibility = visible ? 'visible' : 'hidden';
        }
    }
    public setBackgroundColor(color: string) { this.toolbar?.setBackgroundColor(color); }
    public setColors(colors: ButtonGroupColors) { this.toolbar?.setColors(colors); }
}
