import APP from '@/services';
import { getAccoutIdList } from '@/services/bus/account';
import { buildProtoReq50, parseProtoRsp50 } from '@/services/socket/protobuf/buildReq';
import { Callback } from '@/utils/websocket/index';
import { sha256 } from 'js-sha256';
import * as type from './interface/index';


/**
 * 构建出金请求包的方法
 */
export function buildOutMoney(param: type.buildOutMoneyReqParam) {
    return new Promise((resolve, reject) => {
        const params = {
            protobufName: 't2bBankWithdrawReq',
            funCodeName: 't2bBankWithdrawReq',
            reqParams: {
                ExtOperatorID: Date.now(), // 外部操作流水号
                ExchId: '', // 交易所编号
                Currency: 'RMB', // 币种
                BankChildAccount: '', // 银行子账号
                BankChildAccountName: '', // 银行子账号名
                OpenCardBankId: '', // 银行卡行号
                AppDateTime: null, // 申请日期和时间
                Desc: '', // 备注
                IdentifyCode: null, // 验证码
                BranchBankName: null, // 收款支行名称
                Remark: '', // 备注
                ExchTicket: '', // 交易所流水号
            },
        };
        Object.assign(params.reqParams, param, { OldTaPWD: sha256.hex(param.OldTaPWD) });
        console.log('params', params);
        const package50 = buildProtoReq50(params);
        APP.sendTradingServer(package50, undefined, {
            onSuccess: (res) => {
                const { isSuccess, result } = parseProtoRsp50(res, 't2bBankWithdrawRsp');
                if (isSuccess) {
                    resolve('ok');
                } else {
                    reject(result);
                }
            },
            onFail: (err) => {
                reject(err.message)
            },
        } as Callback);
    });
}

/**
 * 签约请求
 */
export const requestBankSign = (param: type.t2bBankSignReq): Promise<type.t2bBankSignRsp[]> => {
    return new Promise((resolve, reject) => {
        const params = {
            protobufName: 't2bBankSignReq',
            funCodeName: 't2bBankSignReq',
            reqParams: Object.assign(
                {
                    ExtOperatorID: new Date().getTime(), // 外部操作流水号
                    ExchTicket: '', // 交易所流水号
                },
                param
            ),
        };
        const package50 = buildProtoReq50(params);
        APP.sendTradingServer(package50, undefined, {
            onSuccess: (res) => {
                const { isSuccess, result } = parseProtoRsp50(res, 't2bBankSignRsp');
                if (isSuccess) {
                    resolve(result);
                } else {
                    reject(result);
                }
            },
            onFail: (err) => reject(err.message),
        } as Callback);
    });
};

/**
 * 解约签约请求
 */
export const requestBankCancelSign = (param: type.t2bBankCancelSignReq): Promise<type.t2bBankCancelSignRsp[]> => {
    return new Promise((resolve, reject) => {
        const params = {
            protobufName: 't2bBankCancelSignReq',
            funCodeName: 't2bBankCancelSignReq',
            reqParams: Object.assign(
                {
                    ExtOperatorID: new Date().getTime(), // 外部操作流水号
                    ExchTicket: '', // 交易所流水号
                },
                param
            ),
        };
        const package50 = buildProtoReq50(params);
        APP.sendTradingServer(package50, undefined, {
            onSuccess: (res) => {
                const { isSuccess, result } = parseProtoRsp50(res, 't2bBankCancelSignRsp');
                if (isSuccess) {
                    resolve(result);
                } else {
                    reject(result);
                }
            },
            onFail: (err) => reject(err.message),
        } as Callback);
    });
};

/**
 * 已签约信息查询请求
 */
export const queryBankCustomerSign = (): Promise<type.BankAccountSign[]> => {
    return new Promise((resolve, reject) => {
        const params = {
            protobufName: 'BankSignReq',
            funCodeName: 'BankSignReq',
            reqParams: {
                AccountID: Number(getAccoutIdList()),
            },
        };
        const package50 = buildProtoReq50(params);
        APP.sendTradingServer(package50, undefined, {
            onSuccess: (res) => {
                const { isSuccess, result } = parseProtoRsp50(res, 'BankSignRsp');
                if (isSuccess) {
                    resolve(result.BankSign);
                } else {
                    reject(result);
                }
            },
            onFail: (err) => reject(err.message),
        } as Callback);
    });
};

/**
 * 获取托管银行信息
 */
export const queryCusBankInfo = (): Promise<type.CusBankInfoRsp> => {
    return new Promise((resolve, reject) => {
        const params = {
            statement: 'QueryBankCusBankInfo',
            params: {},
        };
        const package50 = buildProtoReq50(params);
        APP.sendTradingServer(package50, undefined, {
            onSuccess: (res) => {
                const { isSuccess, result } = parseProtoRsp50(res);
                if (isSuccess) {
                    resolve(result);
                } else {
                    reject(result);
                }
            },
            onFail: (err) => {
                reject(err.message);
            },
        } as Callback);
    });
};

/**
 * 商城查询托管银行支持银行
 */
export const queryBankInfo = (cusBankId: number): Promise<any> => {
    return new Promise((resolve, reject) => {
        const params = {
            statement: 'QueryBankInfo',
            params: { cusBankId },
        };
        const package50 = buildProtoReq50(params);
        APP.sendTradingServer(package50, undefined, {
            onSuccess: (res) => {
                const { isSuccess, result } = parseProtoRsp50(res);
                if (isSuccess) {
                    resolve(result);
                } else {
                    reject(result);
                }
            },
            onFail: (err) => {
                reject(err.message);
            },
        } as Callback);
    });
};
/**
 * 查询出入金明细信息
 */
export const queryQueryBankInOut = (beginDate: string, endDate: string): Promise<any> => {
    const param = {
        AccountId: getAccoutIdList(),
        beginDate,
        endDate,
    }
    console.log(param);

    return new Promise((resolve, reject) => {
        const params = {
            statement: 'SearchClientMoneyOutIn',
            params: param,
        };
        const package50 = buildProtoReq50(params);
        APP.sendTradingServer(package50, undefined, {
            onSuccess: (res) => {
                const { isSuccess, result } = parseProtoRsp50(res);
                if (isSuccess) {
                    resolve(result);
                } else {
                    reject(result);
                }
            },
            onFail: (err) => {
                reject(err.message);
            },
        } as Callback);
    });
};
