import { BimComponent } from '../core/component';
import { BimEngine } from '../bim-engine';
import { BimDialog } from '../components/dialog';
import { SectionPlanePanel } from '../components/section-plane-panel';

/**
 * 拾取面剖切弹窗管理器
 */
export class SectionPlaneDialogManager extends BimComponent {
    private dialogId = 'section-plane-dialog';
    private dialog: BimDialog | null = null;
    private panel: SectionPlanePanel | null = null;

    constructor(engine: BimEngine) {
        super(engine);
    }

    public init(): void {
        // 可以在这里监听事件
    }

    /**
     * 显示拾取面剖切弹窗
     */
    public show(): void {
        if (!this.engine.dialog || !this.engine.container) {
            console.warn('Dialog manager or container is not initialized');
            return;
        }

        // 如果已打开过，先销毁旧实例
        this.destroy();

        // 创建面板
        this.panel = new SectionPlanePanel({
            onHide: () => {
                console.log('[SectionPlaneDialogManager] 隐藏');
                // TODO: 调用引擎的隐藏功能
            },
            onReverse: () => {
                console.log('[SectionPlaneDialogManager] 反向');
                // TODO: 调用引擎的反向功能
            },
            onReset: () => {
                console.log('[SectionPlaneDialogManager] 重置');
                // TODO: 调用引擎的重置功能
            }
        });
        this.panel.init();

        // 创建弹窗
        const dialogWidth = 240;
        const dialogHeight = 120;
        const paddingRight = 20;
        const paddingBottom = 50;
        const container = this.engine.container;
        const containerWidth = container.clientWidth;
        const containerHeight = container.clientHeight;
        const x = containerWidth - dialogWidth - paddingRight;
        const y = containerHeight - dialogHeight - paddingBottom;

        this.dialog = this.engine.dialog.create({
            id: this.dialogId,
            title: 'sectionPlane.dialogTitle',
            width: dialogWidth,
            height: dialogHeight,
            position: { x, y },
            draggable: true,
            resizable: false,
            content: this.panel.element,
            onClose: () => {
                this.engine.toolbar?.setBtnActive('section-plane', false);
                this.hide();
            }
        });
    }

    /**
     * 隐藏弹窗
     */
    public hide(): void {
        this.destroy();
    }

    /**
     * 销毁弹窗
     */
    public destroy(): void {
        if (this.dialog) {
            this.dialog.destroy();
            this.dialog = null;
        }
        if (this.panel) {
            this.panel.destroy();
            this.panel = null;
        }
    }
}
