import { ref, reactive } from 'vue'
import { PlatformSettings } from './PlatformSettings'
import { PlatformLogger } from './PlatformLogger'
import { BitMapInstance } from './BitMapInstance'

/**
 * 报文通讯域定义，交易调用
 */
export class Message {
    // 交易附件数据
    private _fileData = ref<string>('')
    private _filename = ref<string>('')
    private _fdDict = reactive<Record<string, Uint8Array>>({})
    private _BigFds_: Record<string, any> = {}

    /**
     * 交易文件附件传送标志
     */
    get fileFlag(): boolean {
        return this._fileData.value !== ''
    }
    set fileFlag(value: boolean) {
        if (!value) this._fileData.value = ''
    }

    /**
     * 传入的文件名
     */
    get filename(): string {
        return this._filename.value
    }
    set filename(value: string) {
        this._filename.value = value
    }

    /**
     * 交易文件附件数据
     */
    get fileData(): string {
        return this._fileData.value
    }
    set fileData(value: string) {
        this._fileData.value = value
    }

    /**
     * 获取Fd域值
     */
    public getFdVaule(fdName: string): any {
        let result = ''
        if (this._fdDict[fdName]) {
            const decoder = new TextDecoder()
            result = BitMapInstance.FdBitMap[fdName].RealValue(decoder.decode(this._fdDict[fdName]))
        }
        return result
    }

    /**
     * 获取参数指定域的组包数据
     */
    internal getFdFormatValue(fdName: string): string {
        let result = ''
        if (this._fdDict[fdName]) {
            const decoder = new TextDecoder()
            result = decoder.decode(this._fdDict[fdName])
        }
        return result
    }

    /**
     * 设置Fd域值
     */
    public setFdValue(fdName: string, value: any): void {
        this.setFdFormatValue(fdName, value)
        if (!fdName.endsWith('0')) {
            const bigFdName = `${fdName.substring(0, 3)}0`
            const fds = BitMapInstance.FdBitMap[bigFdName]
            delete this._BigFds_[fds.Code]
            
            for (const fd of fds.Children) {
                if (fd.Code === fdName) break
                if (!this._fdDict[fd.Code]) {
                    this.setFdFormatValue(fd.Code, null)
                }
            }
        }
    }

    /**
     * 根据域类型格式化输入数据
     */
    private setFdFormatValue(fdName: string, value: any): void {
        if (BitMapInstance.FdBitMap[fdName]) {
            const item = BitMapInstance.FdBitMap[fdName]
            const encoder = new TextEncoder()
            const formatValue = encoder.encode(item.FormatValue(value))
            
            this._fdDict[fdName] = formatValue
        }
    }

    // 以下是各域的定义，保持与C#代码相同的命名规范
    get Fd1(): string { return this.getFdVaule('0010').toString() }
    set Fd1(value: string) { this.setFdValue('0010', value) }

    get Fd2(): string { return this.getFdVaule('0020').toString() }
    set Fd2(value: string) { this.setFdValue('0020', value) }

    get Fd3(): string { return this.getFdVaule('0030').toString() }
    set Fd3(value: string) { this.setFdValue('0030', value) }

    get Fd4(): string { return this.getFdVaule('0040').toString() }
    set Fd4(value: string) { this.setFdValue('0040', value) }

    get Fd5(): string { return this.getFdVaule('0050').toString() }
    set Fd5(value: string) { this.setFdValue('0050', value) }

    get Fd6(): string { return this.getFdVaule('0060').toString() }
    set Fd6(value: string) { this.setFdValue('0060', value) }

    get Fd7(): string { return this.getFdVaule('0070').toString() }
    set Fd7(value: string) { this.setFdValue('0070', value) }

    get Fd8(): string { return this.getFdVaule('0080').toString() }
    set Fd8(value: string) { this.setFdValue('0080', value) }

    get Fd9_1(): string { return this.getFdVaule('0091').toString() }
    set Fd9_1(value: string) { this.setFdValue('0091', value) }

    get Fd9_2(): string { return this.getFdVaule('0092').toString() }
    set Fd9_2(value: string) { this.setFdValue('0092', value) }

    get Fd9_3(): string { return this.getFdVaule('0093').toString() }
    set Fd9_3(value: string) { this.setFdValue('0093', value) }

    get Fd9_4(): string { return this.getFdVaule('0094').toString() }
    set Fd9_4(value: string) { this.setFdValue('0094', value) }

    get Fd10(): string { return this.getFdVaule('0100').toString() }
    set Fd10(value: string) { this.setFdValue('0100', value) }

    // 继续实现其他域...
    get Fd11(): string { return this.getFdVaule('0110').toString() }
    set Fd11(value: string) { this.setFdValue('0110', value) }

    get Fd12(): string { return this.getFdVaule('0120').toString() }
    set Fd12(value: string) { this.setFdValue('0120', value) }

    get Fd13(): string { return this.getFdVaule('0130').toString() }
    set Fd13(value: string) { this.setFdValue('0130', value) }

    get Fd14(): string { return this.getFdVaule('0140').toString() }
    set Fd14(value: string) { this.setFdValue('0140', value) }

    get Fd15_1(): string { return this.getFdVaule('0151').toString() }
    set Fd15_1(value: string) { this.setFdValue('0151', value) }

    get Fd15_2(): string { return this.getFdVaule('0152').toString() }
    set Fd15_2(value: string) { this.setFdValue('0152', value) }

    get Fd15_3(): string { return this.getFdVaule('0153').toString() }
    set Fd15_3(value: string) { this.setFdValue('0153', value) }

    get Fd15_4(): string { return this.getFdVaule('0154').toString() }
    set Fd15_4(value: string) { this.setFdValue('0154', value) }

    get Fd15_5(): string { return this.getFdVaule('0155').toString() }
    set Fd15_5(value: string) { this.setFdValue('0155', value) }

    get Fd15_6(): string { return this.getFdVaule('0156').toString() }
    set Fd15_6(value: string) { this.setFdValue('0156', value) }

    get Fd15_7(): string { return this.getFdVaule('0157').toString() }
    set Fd15_7(value: string) { this.setFdValue('0157', value) }

    get Fd15_8(): string { return this.getFdVaule('0158').toString() }
    set Fd15_8(value: string) { this.setFdValue('0158', value) }

    get Fd15_9(): string { return this.getFdVaule('0159').toString() }
    set Fd15_9(value: string) { this.setFdValue('0159', value) }

    get Fd15_A(): string { return this.getFdVaule('015A').toString() }
    set Fd15_A(value: string) { this.setFdValue('015A', value) }

    get Fd15_B(): string { return this.getFdVaule('015B').toString() }
    set Fd15_B(value: string) { this.setFdValue('015B', value) }

    get Fd15_C(): string { return this.getFdVaule('015C').toString() }
    set Fd15_C(value: string) { this.setFdValue('015C', value) }

    get Fd15_D(): string { return this.getFdVaule('015D').toString() }
    set Fd15_D(value: string) { this.setFdValue('015D', value) }

    get Fd15_E(): string { return this.getFdVaule('015E').toString() }
    set Fd15_E(value: string) { this.setFdValue('015E', value) }

    get Fd15_F(): string { return this.getFdVaule('015F').toString() }
    set Fd15_F(value: string) { this.setFdValue('015F', value) }

    get Fd15_G(): string { return this.getFdVaule('015G').toString() }
    set Fd15_G(value: string) { this.setFdValue('015G', value) }

    get Fd15_H(): string { return this.getFdVaule('015H').toString() }
    set Fd15_H(value: string) { this.setFdValue('015H', value) }

    get Fd15_I(): string { return this.getFdVaule('015I').toString() }
    set Fd15_I(value: string) { this.setFdValue('015I', value) }

    get Fd15_J(): string { return this.getFdVaule('015J').toString() }
    set Fd15_J(value: string) { this.setFdValue('015J', value) }

    // 继续实现剩余域...
    get Fd16(): string { return this.getFdVaule('0160').toString() }
    set Fd16(value: string) { this.setFdValue('0160', value) }

    get Fd17(): string { return this.getFdVaule('0170').toString() }
    set Fd17(value: string) { this.setFdValue('0170', value) }

    get Fd18(): string { return this.getFdVaule('0180').toString() }
    set Fd18(value: string) { this.setFdValue('0180', value) }

    get Fd19_1(): string { return this.getFdVaule('0191').toString() }
    set Fd19_1(value: string) { this.setFdValue('0191', value) }

    get Fd19_2(): string { return this.getFdVaule('0192').toString() }
    set Fd19_2(value: string) { this.setFdValue('0192', value) }

    get Fd19_3(): string { return this.getFdVaule('0193').toString() }
    set Fd19_3(value: string) { this.setFdValue('0193', value) }

    get Fd19_4(): string { return this.getFdVaule('0194').toString() }
    set Fd19_4(value: string) { this.setFdValue('0194', value) }

    get Fd19_5(): string { return this.getFdVaule('0195').toString() }
    set Fd19_5(value: string) { this.setFdValue('0195', value) }

    get Fd20(): string { return this.getFdVaule('0200').toString() }
    set Fd20(value: string) { this.setFdValue('0200', value) }

    // 实现剩余所有域...
    // 按照相同模式实现Fd21到Fd47的所有域
}