import { BimComponent } from '../core/component';
import { BimEngine } from '../bim-engine';
import { BimCollapse } from '../components/collapse/index';
import { BimDescription } from '../components/description/index';
import { BimTab } from '../components/tab/index';

/**
 * 属性面板管理器
 * 负责展示和管理属性面板弹窗 (演示 Tab + Collapse + Description 组件)
 */
export class PropertyPanelManager extends BimComponent {
    private dialogId = 'property-panel-dialog';

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

    public init(): void {
        // 监听来自 Demo 的打开属性面板事件
        document.addEventListener('bim-demo:open-property-panel', () => {
            this.show();
        });
    }

    /**
     * 显示属性面板
     */
    public show() {
        if (!this.engine.dialog) {
            console.warn('Dialog manager is not initialized');
            return;
        }

        // 1. 创建弹窗
        const width = 360; // 稍微加宽一点以容纳 Tab
        const x = document.body.clientWidth - width - 40;
        console.log('x', x)

        const dialog = this.engine.dialog.create({
            id: this.dialogId,
            title: 'panel.property.title', // '构件详情'
            content: '',
            width: `${width}px`,
            height: '500px',
            position: { x, y: 20 },
            showMask: false,
            resizable: true
        } as any);

        // 2. 创建内容容器
        const contentContainer = document.createElement('div');
        contentContainer.style.height = '100%';
        contentContainer.style.display = 'flex';
        contentContainer.style.flexDirection = 'column';

        dialog.setContent(contentContainer);

        // 3. 创建标签页组件
        const tab = new BimTab({
            container: contentContainer,
            tabs: [
                {
                    id: 'props',
                    title: 'panel.property.tab.props', // '属性'
                    content: this.createPropsTabContent()
                },
                {
                    id: 'material',
                    title: 'panel.property.tab.material', // '材质'
                    content: this.createMaterialTabContent()
                }
            ]
        });
        tab.init();

    }

    /**
     * 创建"属性"标签页的内容 (包含 Collapse)
     */
    private createPropsTabContent(): HTMLElement {
        const container = document.createElement('div');
        container.style.height = '100%';
        container.style.overflowY = 'auto'; // 内容区域滚动

        new BimCollapse({
            container: container,
            accordion: true,
            activeIds: ['base', 'location'],
            items: [
                {
                    id: 'base',
                    title: 'panel.property.base', // '基本属性'
                    content: this.createBaseInfoContent(),
                },
                {
                    id: 'advanced',
                    title: 'panel.property.advanced', // '高级设置'
                    content: this.createAdvancedInfoContent(), // 新增一个内容
                    disabled: false
                }
            ]
        });

        return container;
    }

    /**
     * 创建"材质"标签页的内容 (包含 Collapse)
     */
    private createMaterialTabContent(): HTMLElement {
        const container = document.createElement('div');
        container.style.height = '100%';
        container.style.overflowY = 'auto';

        new BimCollapse({
            container: container,
            accordion: true,
            activeIds: ['material'],
            items: [
                {
                    id: 'material',
                    title: 'panel.property.material', // '材质信息'
                    content: this.createMaterialContent(),
                }
            ]
        });

        return container;
    }

    private createBaseInfoContent(): HTMLElement {
        const container = document.createElement('div');

        new BimDescription({
            container: container,
            labelWidth: '80px',
            bordered: true,
            items: [
                { label: 'Guid', value: '<span style="color:#666">1f8d-4a2e-9c</span>' },
                { label: 'Name', value: '<b>Basic Wall: Generic - 200mm</b>' },
                { label: 'Type', value: 'Basic Wall' },
                { label: 'Level', value: 'Trane - Centrifugal Water Chiller - CVHF 2 Stage direct drive TAG(BP-RHS-1100RT) 0202104531 1' }
            ]
        });

        return container;
    }

    private createAdvancedInfoContent(): HTMLElement {
        const container = document.createElement('div');

        new BimDescription({
            container: container,
            labelWidth: '100px',
            bordered: true,
            items: [
                { label: 'Area', value: '32.5 m²' },
                { label: 'Volume', value: '6.5 m³' },
                { label: 'Length', value: '5000 mm' },
                { label: 'Phase', value: 'New Construction' }
            ]
        });

        return container;
    }

    private createMaterialContent(): HTMLElement {
        const container = document.createElement('div');

        // 材质预览块
        const preview = document.createElement('div');
        preview.style.display = 'flex';
        preview.style.alignItems = 'center';
        preview.style.marginBottom = '4px';
        preview.innerHTML = `
            <div style="width:24px;height:24px;background:#9ca3af;margin-right:8px;border:1px solid #6b7280;border-radius:2px;"></div>
            <span>Concrete - Cast-in-Place Gray</span>
        `;

        const descContainer = document.createElement('div');

        new BimDescription({
            container: descContainer,
            items: [
                { label: 'Preview', value: preview },
                { label: 'Class', value: 'Concrete' },
                { label: 'Density', value: '2400 kg/m³' },
                { label: 'Thermal', value: '0.6 W/(m·K)' }
            ]
        });

        container.appendChild(descContainer);
        return container;
    }

    public destroy(): void {
        // 如果有需要清理的资源
    }
}
