import { Message } from './Message'
import { PackageType } from './PackageType'
import { TransitType } from './TransitType'
import { MsgPackage } from './MsgPackage'
import { BitMapInstance } from './BitMapInstance'
import { PlatformSettings } from './PlatformSettings'
import { PlatformLogger } from './PlatformLogger'

/**
 * Message的扩展方法集合
 */
export class MessageExtension {
    /**
     * 执行通讯操作
     */
    public static doTransit(message: Message): boolean {
        let packageType = PackageType.Msg858301
        switch (message.TransitNode) {
            case TransitType.CallServer:
                packageType = PackageType.Msg858302
                break
            case TransitType.CallNoFileSys:
                packageType = PackageType.MsgXml01
                break
            default:
                break
        }
        
        if (message.CustomizeTransitEntry) {
            return message.CustomizeTransitEntry.process(message, packageType)
        }
        return MessageExtension.doTransitInternal(message, packageType)
    }

    /**
     * 执行通讯操作(内部方法)
     */
    private static doTransitInternal(message: Message, packageType: PackageType): boolean {
        return MsgPackage.trade(message, packageType, message.TransitName)
    }

    /**
     * Message对象域信息序列化，用于授权操作
     */
    public static serializeForAuth(message: Message): string {
        const ret: Record<string, string> = {}
        const empty: string[] = []
        
        for (const fd of Object.values(BitMapInstance.FdBitMap)) {
            const type = fd.getItemType()
            const key = fd.Code
            const value = message.getFdVaule(fd.Code).toString()

            let typeSuffix = ''
            if (type === String) {
                typeSuffix = '_0'
            } else if (type === Number) {
                typeSuffix = '_1'
            } else {
                throw new Error(`不支持的域类型，请检查定义！->${fd.Code}`)
            }

            if (!value.trim()) {
                empty.push(key + typeSuffix)
            } else {
                ret[key + typeSuffix] = value
            }
        }

        if (empty.length > 0) {
            ret['empty'] = empty.join(',')
        }

        return JSON.stringify(ret)
    }

    /**
     * 获取报文位图类型信息
     */
    public static getMsgBitMapTypes(message: Message): Record<string, string> {
        const result: Record<string, string> = {}
        
        for (const item of Object.values(BitMapInstance.FdBitMap)) {
            const type = item.getItemType()
            const value = type === String ? '1' : '0'
            result[`FD${item.Code}`] = value
        }
        
        return result
    }

    /**
     * 解析报文
     */
    public static analyze(type: string, returnData: Uint8Array, fileData: Uint8Array): { success: boolean, msg: Message } {
        const msg = new Message()
        const packageType = PackageType[type as keyof typeof PackageType]
        const packageInstance = MsgPackage.packageList[packageType]
        
        const totalData = new Uint8Array(returnData.length + fileData.length)
        totalData.set(returnData, 0)
        totalData.set(fileData, returnData.length)
        
        const success = packageInstance.analyze(msg, totalData)
        return { success, msg }
    }
}

// msgjson辅助类
class MsgJson {
    code: string = ''
    type: string = ''
    values: string = ''
}