import { BimComponent } from '../core/component';
import { BimEngine } from '../bim-engine';
import { BimDialog } from '../components/dialog';
import { SectionBoxPanel } from '../components/section-box-panel';
import type { SectionBoxRange } from '../components/section-box-panel/types';

/**
 * 剖切盒弹窗管理器
 */
export class SectionBoxDialogManager extends BimComponent {
    private dialogId = 'section-box-dialog';
    private dialog: BimDialog | null = null;
    private panel: SectionBoxPanel | 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 SectionBoxPanel({
            defaultHidden: false,
            defaultReversed: false,
            onHideToggle: (isHidden) => {
                console.log('[SectionBoxDialogManager] 隐藏切换：', isHidden);
                // TODO: 实现隐藏/显示剖切盒的逻辑
            },
            onReverseToggle: (isReversed) => {
                console.log('[SectionBoxDialogManager] 反向切换：', isReversed);
                // TODO: 实现反向剖切的逻辑
            },
            onFitToModel: () => {
                console.log('[SectionBoxDialogManager] 适应到模型');
                // TODO: 实现自动适应模型的逻辑
            },
            onReset: () => {
                console.log('[SectionBoxDialogManager] 重置');
                // 注意：不要在这里调用 panel.reset()，会造成无限递归
                // panel 的 reset 按钮已经在内部处理了状态重置
                // TODO: 这里只需要通知 3D 引擎重置剖切盒即可
            },
            onRangeChange: (range) => {
                console.log('[SectionBoxDialogManager] 范围变化：', range);
                // TODO: 实现范围变化的逻辑
            }
        });
        this.panel.init();

        // 创建弹窗
        const dialogWidth = 280;
        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 - paddingBottom - 300; // 临时y值，会被fitHeight调整

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

        // 自适应高度
        this.dialog.fitHeight(false);
    }

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

    /**
     * 获取隐藏状态
     */
    public getHiddenState(): boolean {
        return this.panel?.getHiddenState() ?? false;
    }

    /**
     * 设置隐藏状态
     */
    public setHiddenState(isHidden: boolean): void {
        this.panel?.setHiddenState(isHidden);
    }

    /**
     * 获取反向状态
     */
    public getReversedState(): boolean {
        return this.panel?.getReversedState() ?? false;
    }

    /**
     * 设置反向状态
     */
    public setReversedState(isReversed: boolean): void {
        this.panel?.setReversedState(isReversed);
    }

    /**
     * 获取范围值
     */
    public getRange(): SectionBoxRange | null {
        return this.panel?.getRange() ?? null;
    }

    /**
     * 设置范围值
     */
    public setRange(range: Partial<SectionBoxRange>): void {
        this.panel?.setRange(range);
    }

    /**
     * 销毁弹窗和面板
     */
    public destroy(): void {
        // 关闭弹窗
        if (this.dialog) {
            this.dialog.destroy();
            this.dialog = null;
        }

        // 销毁面板
        if (this.panel) {
            this.panel.destroy();
            this.panel = null;
        }
    }
}
