// 假设这些类的定义
class TreeViewListItemLeaf {
    Name: string = '';
    MenuCode: string = '';
    Action: string = '';
    Title: string = '';
    Tag: string = '';
    Rights: string = '';
    IsVisible: boolean = true;
    TreeItemLeafAppendPropertyCollection: TreeViewListItemLeaf[] = [];
    DescriptionName: string = '';
    Key: string = '';
    Value: string = '';
}

class TreeViewListItemCategory {
    Name: string = '';
    IsVisible: boolean = true;
    DescriptionName: string = '';
    Items: TreeViewListItem[] = [];
    SortIndex: string = '';
}

class TreeViewListItem {
    Name: string = '';
    IsVisible: boolean = true;
    DescriptionName: string = '';
    Items: TreeViewListItem[] = [];
    SortIndex: string = '';
}

// 模拟日志记录器
class Logger {
    error(message: string) {
        console.error(message);
    }
}

class TreeViewSource {
    log = new Logger();

    GetTreeItemLeef(xmlNode: any): TreeViewListItemLeaf | null {
        const treeItemLeaf = new TreeViewListItemLeaf();
        const tradeElement = xmlNode;

        for (let i = 0; i < tradeElement.attributes.length; i++) {
            const key = tradeElement.attributes[i].name.toLowerCase();
            const value = tradeElement.attributes[i].value.trim();

            switch (key) {
                case 'name':
                    if (!value) {
                        this.log.error('主菜单配置信息有误，交易节点的name属性配置为空！');
                        return null;
                    }
                    treeItemLeaf.Name = value;
                    break;
                case 'menucode':
                    if (!value) {
                        this.log.error(`主菜单配置信息有误，交易节点${treeItemLeaf.Name}的menucode属性配置为空！`);
                        return null;
                    }
                    treeItemLeaf.MenuCode = value;
                    break;
                case 'action':
                    if (!value) {
                        this.log.error(`主菜单配置信息有误，交易节点${treeItemLeaf.Name}的action属性配置为空！`);
                        return null;
                    }
                    treeItemLeaf.Action = value;
                    break;
                case 'title':
                    treeItemLeaf.Title = value;
                    break;
                case 'tag':
                    treeItemLeaf.Tag = value;
                    break;
                case 'rights':
                    treeItemLeaf.Rights = value;
                    break;
                case 'isvisible':
                    treeItemLeaf.IsVisible = value.toLowerCase() !== 'false';
                    break;
            }
        }

        if (tradeElement.childNodes.length > 0) {
            for (const node of tradeElement.childNodes) {
                const treeItemLeafAppendProperty = new TreeViewListItemLeaf();
                const xmlElement = node;

                for (let i = 0; i < xmlElement.attributes.length; i++) {
                    const key = xmlElement.attributes[i].name.toLowerCase();
                    const value = xmlElement.attributes[i].value.trim();

                    switch (key) {
                        case 'key':
                            treeItemLeafAppendProperty.Key = value;
                            break;
                        case 'value':
                            treeItemLeafAppendProperty.Value = value;
                            break;
                    }
                }
                treeItemLeaf.TreeItemLeafAppendPropertyCollection.push(treeItemLeafAppendProperty);
            }
        } else {
            treeItemLeaf.TreeItemLeafAppendPropertyCollection = [];
        }

        treeItemLeaf.DescriptionName = `${treeItemLeaf.MenuCode} ${treeItemLeaf.Name}`;
        return treeItemLeaf;
    }

    GetTreeItemBranch(xmlNode: any): TreeViewListItemCategory | null {
        const xmlElement = xmlNode;
        const name = xmlElement.getAttribute('name');

        if (!name) {
            this.log.error('主菜单配置信息有误，交易分类节点的name属性配置为空！');
            return null;
        }

        const isvisible = xmlElement.getAttribute('isvisible');
        const treeItemBranch = new TreeViewListItemCategory();
        let treeItemBase: TreeViewListItem | null;

        treeItemBranch.Name = name.trim();
        treeItemBranch.IsVisible = isvisible?.toLowerCase() !== 'false';
        treeItemBranch.DescriptionName = treeItemBranch.Name;

        let sortindex = 0;

        for (const xn of xmlElement.childNodes) {
            treeItemBase = xn.nodeName === 'category' ? this.GetTreeItemBranch(xn) : xn.nodeName === 'trade' ? this.GetTreeItemLeef(xn) : null;

            if (treeItemBase) {
                const len = (xmlNode.childNodes.length - 1).toString().length;
                treeItemBase.SortIndex = sortindex.toString().padStart(len, '0');
                sortindex++;
                treeItemBranch.Items.push(treeItemBase);
            } else if (xn.nodeName === 'category') {
                this.log.error('主菜单配置信息有误，获取交易分类节点的配置信息失败！');
                return null;
            } else if (xn.nodeName === 'trade') {
                this.log.error('主菜单配置信息有误，获取交易节点的配置信息失败！');
                return null;
            }
        }

        return treeItemBranch;
    }

    GetMenuTreeListByXML(xmlNode: any): TreeViewListItem[] | null {
        const transactionMenuBaseCollection: TreeViewListItem[] = [];
        let treeItem: TreeViewListItem | null;
        let sortindex = 0;

        for (const xn of xmlNode.childNodes) {
            treeItem = xn.nodeName === 'category' ? this.GetTreeItemBranch(xn) : xn.nodeName === 'trade' ? this.GetTreeItemLeef(xn) : null;

            if (treeItem) {
                const len = (xmlNode.childNodes.length - 1).toString().length;
                treeItem.SortIndex = sortindex.toString().padStart(len, '0');
                sortindex++;
                transactionMenuBaseCollection.push(treeItem);
            } else if (xn.nodeName === 'category') {
                this.log.error('主菜单配置信息有误，获取交易分类节点的配置信息失败！');
                return null;
            } else if (xn.nodeName === 'trade') {
                this.log.error('主菜单配置信息有误，获取交易节点的配置信息失败！');
                return null;
            }
        }

        return transactionMenuBaseCollection;
    }

    GetMenuTreeListByDataTable(dataTable: any[]): TreeViewListItem[] {
        const menuList: TreeViewListItem[] = [];
        let childs = '';

        for (const row of dataTable) {
            childs += `${row.children},`;
        }

        for (const row of dataTable) {
            let bo = true;
            const id = row.id;

            if (!id) continue;

            const arrID = childs.split(',');
            for (const item of arrID) {
                if (item.trim() === id.trim()) bo = false;
            }

            if (bo) {
                const treeItemBase = this.GetTreeItemBranchFromDataTable(dataTable, id);
                menuList.push(treeItemBase);
            }
        }

        return menuList;
    }

    GetTreeItemLeefFromDataTable(dataTable: any[], ID: string): TreeViewListItemLeaf | null {
        const treeItemLeaf = new TreeViewListItemLeaf();

        for (const row of dataTable) {
            let boRow = false;

            for (const col in row) {
                if (col.toLowerCase() === 'id' && row[col] === ID) boRow = true;

                if (boRow) {
                    switch (col.toLowerCase()) {
                        case 'name':
                            treeItemLeaf.Name = row[col];
                            if (!treeItemLeaf.Name) {
                                this.log.error('主菜单配置信息有误，交易节点的name属性配置为空！');
                                return null;
                            }
                            break;
                        case 'menucode':
                            treeItemLeaf.MenuCode = row[col];
                            if (!treeItemLeaf.MenuCode) {
                                this.log.error(`主菜单配置信息有误，交易节点${treeItemLeaf.Name}的menucode属性配置为空！`);
                                return null;
                            }
                            break;
                        case 'action':
                            treeItemLeaf.Action = row[col];
                            if (!treeItemLeaf.Action) {
                                this.log.error(`主菜单配置信息有误，交易节点${treeItemLeaf.Name}的action属性配置为空！`);
                                return null;
                            }
                            break;
                        case 'title':
                            treeItemLeaf.Title = row[col];
                            break;
                        case 'tag':
                            treeItemLeaf.Tag = row[col];
                            break;
                        case 'rights':
                            treeItemLeaf.Rights = row[col];
                            break;
                        case 'metadata':
                            const arrs = row[col].split('|');
                            if (arrs.length > 0) {
                                for (const item of arrs) {
                                    const arrValue = item.split(':');
                                    if (arrValue.length === 2) {
                                        const treeItemLeafAppendProperty = new TreeViewListItemLeaf();
                                        treeItemLeafAppendProperty.Key = arrValue[0];
                                        treeItemLeafAppendProperty.Value = arrValue[1];
                                        treeItemLeaf.TreeItemLeafAppendPropertyCollection.push(treeItemLeafAppendProperty);
                                    }
                                }
                            }
                            break;
                        case 'isvisible':
                            treeItemLeaf.IsVisible = row[col].toLowerCase() !== 'false';
                            break;
                    }
                }
            }

            treeItemLeaf.DescriptionName = `${treeItemLeaf.MenuCode} ${treeItemLeaf.Name}`;
            if (boRow) break;
        }

        return treeItemLeaf;
    }

    GetTreeItemBranchFromDataTable(dataTable: any[], ID: string): TreeViewListItem {
        const treeItemBranch = new TreeViewListItem();

        for (const row of dataTable) {
            if (row.id === ID) {
                treeItemBranch.Name = row.name;
                treeItemBranch.IsVisible = row.isvisible?.toLowerCase() !== 'false';
                treeItemBranch.DescriptionName = treeItemBranch.Name;

                if (row.children?.trim()) {
                    const arr = row.children.trim().split(',');
                    for (const item of arr) {
                        treeItemBranch.Items.push(this.GetTreeItemBranchFromDataTable(dataTable, item));
                    }
                } else {
                    return this.GetTreeItemLeefFromDataTable(dataTable, ID)!;
                }
            }
        }

        return treeItemBranch;
    }
}

export default TreeViewSource;