import { BimComponent } from '../core/component';
import { BimEngine } from '../bim-engine';
import { BimDialog } from '../components/dialog';
import { SectionAxisPanel } from '../components/section-axis-panel';
import type { SectionAxis } from '../components/section-axis-panel/types';

/**
 * 轴向剖切弹窗管理器
 */
export class SectionAxisDialogManager extends BimComponent {
    private dialogId = 'section-axis-dialog';
    private dialog: BimDialog | null = null;
    private panel: SectionAxisPanel | 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 SectionAxisPanel({
            defaultAxis: 'x',
            defaultHidden: false,
            onHideToggle: (isHidden) => {
                console.log('[SectionAxisDialogManager] 隐藏切换：', isHidden);
                // TODO: 实现隐藏/显示剖切面的逻辑
            },
            onReverse: () => {
                console.log('[SectionAxisDialogManager] 反向剖切');
                // TODO: 实现反向剖切的逻辑
            },
            onAxisChange: (axis) => {
                console.log('[SectionAxisDialogManager] 切换轴向：', axis);
                // TODO: 实现轴向切换的逻辑
            }
        });
        this.panel.init();

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

        this.dialog = this.engine.dialog.create({
            id: this.dialogId,
            title: 'sectionAxis.dialogTitle',
            width: dialogWidth,
            height: 'auto', // 自动高度
            position: { x, y },
            draggable: true,
            resizable: false,
            content: this.panel.element,
            onClose: () => {
                this.engine.toolbar?.setBtnActive('section-axis', 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 getActiveAxis(): SectionAxis {
        return this.panel?.getActiveAxis() ?? 'x';
    }

    /**
     * 设置激活的轴向
     */
    public setActiveAxis(axis: SectionAxis): void {
        this.panel?.setActiveAxis(axis);
    }

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

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