import type { LineTreeDecorators, LineTreeLeaves } from './lineTreeUtils'

/** getDeviceTree 接口专用装饰（与 getLineTree 层级不同） */
export function decorateDeviceTree(
    data: any[],
    type: string | undefined,
    decorators: LineTreeDecorators
): LineTreeLeaves {
    const leaves: LineTreeLeaves = { govern: [], portable: [], monitor: [], engineering: [] }
    const { primary, statusColor, applyMeta } = decorators

    data.forEach(item => {
        if (type === '2') {
            applyMeta(item, { icon: 'el-icon-HomeFilled', color: primary() })
            item.children?.forEach((child: any) => {
                applyMeta(child, { icon: 'el-icon-Menu', color: primary() })
                child.children?.forEach((grand: any) => {
                    applyMeta(grand, {
                        icon: 'el-icon-Platform',
                        color: statusColor(grand.comFlag)
                    })
                    leaves.engineering.push(grand)
                })
            })
            return
        }

        if (item.name === '治理设备') {
            item.children?.forEach((l1: any) => {
                applyMeta(l1, { icon: 'el-icon-HomeFilled', color: primary() })
                l1.children?.forEach((l2: any) => {
                    applyMeta(l2, { icon: 'el-icon-Menu', color: primary() })
                    l2.children?.forEach((l3: any) => {
                        l3.pName = '治理设备'
                        applyMeta(l3, {
                            icon: 'el-icon-Platform',
                            level: 2,
                            color: l3.comFlag === 1 ? '#e26257 !important' : primary()
                        })
                        leaves.govern.push(l3)
                    })
                })
            })
        } else if (item.name === '便携式设备') {
            item.children?.forEach((l1: any) => {
                applyMeta(l1, {
                    icon: 'el-icon-Platform',
                    color: statusColor(l1.comFlag)
                })
                l1.pName = '便携式设备'
                if (l1.type === 'device') {
                    leaves.portable.push(l1)
                }
                l1.children?.forEach((l2: any) => {
                    applyMeta(l2, {
                        icon: 'el-icon-Platform',
                        color: statusColor(l2.comFlag)
                    })
                    l2.pName = '便携式设备'
                })
            })
        } else if (item.name === '监测设备') {
            item.children?.forEach((l1: any) => {
                applyMeta(l1, { icon: 'el-icon-HomeFilled', color: primary() })
                l1.children?.forEach((l2: any) => {
                    applyMeta(l2, { icon: 'el-icon-Menu', color: primary() })
                    l2.children?.forEach((l3: any) => {
                        l3.pName = '监测设备'
                        applyMeta(l3, {
                            icon: 'el-icon-Platform',
                            color: l3.comFlag === 1 ? '#e26257 !important' : primary()
                        })
                        leaves.monitor.push(l3)
                    })
                })
            })
        }
    })

    return leaves
}

/** 装饰 getDeviceTree 扁平列表（deviceInfoTree） */
export function decorateDeviceInfoTree(list: any[], decorators: LineTreeDecorators): any[] {
    const { primary, applyMeta } = decorators
    const leaves: any[] = []

    list.forEach(item => {
        applyMeta(item, { icon: 'el-icon-HomeFilled', color: primary() })
        item.children?.forEach((l2: any) => {
            applyMeta(l2, { icon: 'el-icon-Menu', color: primary() })
            l2.children?.forEach((l3: any) => {
                applyMeta(l3, {
                    icon: 'el-icon-Platform',
                    color: l3.comFlag === 1 || l3.comFlag === 3 ? '#e26257 !important' : primary()
                })
                leaves.push(l3)
            })
        })
    })

    return leaves
}

/** 装饰云端设备树 getCldTree */
export function decorateCloudTree(root: any, decorators: LineTreeDecorators): any[] {
    const { primary, statusColor, applyMeta } = decorators
    const leaves: any[] = []

    applyMeta(root, { icon: 'el-icon-ChromeFilled', color: primary() })
    root.children?.forEach((l1: any) => {
        applyMeta(l1, { icon: 'el-icon-HomeFilled', color: primary() })
        l1.children?.forEach((l2: any) => {
            applyMeta(l2, { icon: 'el-icon-Menu', color: primary() })
            l2.children?.forEach((l3: any) => {
                applyMeta(l3, {
                    icon: 'el-icon-Platform',
                    // level: 2,
                    color: statusColor(l3.comFlag)
                })
               l3.children?.forEach((l4: any) => {
                applyMeta(l4, {
                    icon: 'local-监测点',
                    color: statusColor(l4.comFlag)
                })
                leaves.push(l4)
               })
            })
        })
    })

    return leaves
}

/** 装饰 objTree（治理对象树） */
export function decorateObjTree(data: any[], decorators: LineTreeDecorators): any[] {
    const { primary, applyMeta } = decorators
    const leaves: any[] = []

    data.forEach(l1 => {
        applyMeta(l1, { icon: 'el-icon-HomeFilled', color: primary(), level: 1 })
        l1.children?.forEach((l2: any) => {
            applyMeta(l2, { icon: 'el-icon-Menu', color: primary(), level: 2 })
            l2.children?.forEach((l3: any) => {
                applyMeta(l3, { icon: 'el-icon-Platform', color: primary(), level: 3 })
                leaves.push(l3)
            })
        })
    })

    return leaves
}

/** 装饰 lineTree 台账线路树（监测设备根节点） */
export function decorateLedgerLineTree(root: any, decorators: LineTreeDecorators): any[] {
    const { primary, statusColor, applyMeta } = decorators
    const leaves: any[] = []

    if (!root) return leaves

    applyMeta(root, { icon: 'el-icon-Menu', color: primary(), level: 0 })
    if (!Array.isArray(root.children)) root.children = []

    root.children.forEach((l1: any) => {
        applyMeta(l1, { icon: 'el-icon-HomeFilled', color: primary(), level: 1 })
        if (!Array.isArray(l1.children)) l1.children = []
        l1.children.forEach((l2: any) => {
            applyMeta(l2, { icon: 'el-icon-Menu', color: primary(), level: 2 })
            if (!Array.isArray(l2.children)) l2.children = []
            l2.children.forEach((l3: any) => {
                applyMeta(l3, {
                    icon: 'el-icon-Platform',
                    color: statusColor(l3.comFlag),
                    level: 3
                })
                if (!Array.isArray(l3.children)) l3.children = []
                l3.children.forEach((l4: any) => {
                    applyMeta(l4, {
                        icon: 'el-icon-Platform',
                        color: statusColor(l4.comFlag),
                        level: 4
                    })
                    leaves.push(l4)
                })
            })
        })
    })

    return leaves
}

/** getDeviceTree 接口叶子收集（3 层结构，便携式为 type=device 节点） */
export function collectDeviceApiLeaves(
    governNodes: any[],
    portableNodes: any[],
    monitorNodes: any[]
): { govern: any[]; portable: any[]; monitor: any[] } {
    const govern: any[] = []
    const portable: any[] = []
    const monitor: any[] = []

    governNodes.forEach(l1 => {
        l1.children?.forEach((l2: any) => {
            l2.children?.forEach((l3: any) => govern.push(l3))
        })
    })

    portableNodes.forEach(l1 => {
        if (l1.type === 'device') portable.push(l1)
    })

    monitorNodes.forEach(l1 => {
        l1.children?.forEach((l2: any) => {
            l2.children?.forEach((l3: any) => monitor.push(l3))
        })
    })

    return { govern, portable, monitor }
}

/** 从 lineTree 数据中解析监测设备根节点 */
export function resolveMonitorRoot(data: any): any | null {
    if (Array.isArray(data)) {
        return data.find(item => item.name === '监测设备') ?? null
    }
    if (data?.name === '监测设备') return data
    return null
}
