// 用户相关请求模块
import { DelistingType, OperateType, OptionType, PriceType } from '@/common/constants/enumCommon';
import APP from '@/services';
import { getUserId } from '@/services/bus/account';
import { getLongTypeLoginID } from '@/services/bus/login';
import {buildProtoReq50, parseProtoRsp50, protoMiddleware} from '@/services/socket/protobuf/buildReq';
import { Callback } from '@/utils/websocket/index';
import moment from 'moment';
import { v4 as uuidv4 } from 'uuid';
import * as orderType from './interface/index';
import {OrderReq} from "@/services/socket/order/interface";
import * as Long from "long";
import {LongType} from "@/services/socket/login/interface";

/**
 * 下单接口
 * @param param
 * @constructor
 */
export const Order = (param: OrderReq): Promise<any> => {
    return protoMiddleware<OrderReq>(param, 'OrderReq', 'OrderRsp', 8)
}

/**
 * 交易委托请求
 */
export const orderReq = (param: orderType.OrderReqType): Promise<string> => {
    return new Promise((resolve, reject) => {
        const params = {
            protobufName: 'OrderReq',
            funCodeName: 'OrderReq',
            reqParams: {
                ClientSerialNo: uuidv4(), // 客户端流水号
                ClientType: 4, // 终端类型
                ClientOrderTime: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
                LoginID: getLongTypeLoginID(), // 登陆账号
                OperatorID: getUserId(), // 操作员账号ID
                OrderSrc: 1, // 客户端下单
                TriggerType: 1, // 终端默认为1
                TimevalidType: 1, // 终端默认为1
                ValidType: 1, // 校验类型
                OptionType: OptionType.Type_C, // 期权类型(1:认购(看涨)number:认沽(看跌))
                OperateType: OperateType.ApplyFor, // 操作类型
                MarketMaxSub: 0, // 市价允许最大偏差(做市)
                CurtQuotePrice: 0, // 保留，计算冻结金额使用
                PriceMode: PriceType.limit, // 取价方式
                TriggerOperator: 0, // 触发条件-1:大于等于 2:小于等于
                DelistingType: DelistingType.selected, // 摘牌类型
                ServiceTime: '', // 服务端时间
            },
            msgHeadParams: {
                AccountID: param.AccountID,
                MarketID: param.MarketID,
                GoodsID: param.GoodsID,
            },
        };
        Object.assign(params.reqParams, param);
        const package50 = buildProtoReq50(params);
        APP.sendTradingServer(package50, undefined, {
            onSuccess: (res) => {
                const { isSuccess, result } = parseProtoRsp50(res, 'OrderRsp');
                if (isSuccess) {
                    const orderId = String(result.OrderID);
                    resolve(orderId);
                } else {
                    reject(result);
                }
            },
            onFail: (err) => reject(err.message),
        } as Callback);
    });
};

/**
 * 交易撤单请求
 */
export const cancelOrderReq = (param: orderType.CancelOrderReq): Promise<string> => {
    return new Promise((resolve, reject) => {
        const params = {
            protobufName: 'CancelOrderReq',
            funCodeName: 'CancelOrderReq',
            reqParams: {
                ClientSerialNo: uuidv4(), // 客户端流水号
                ClientType: 4, // 终端类型
                ClientOrderTime: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
                OperateType: 1, // 操作类型
                OrderSrc: 1, // 客户端下单
                OperatorID: getLongTypeLoginID(), // 操作员账号ID
                OldOrderId: param.OldOrderId, // 原委托单号
                AccountID: param.AccountID, // 交易账号
            },
            msgHeadParams: {
                AccountID: param.AccountID,
                MarketID: param.MarketID,
                GoodsID: param.GoodsID,
            },
        };
        const package50 = buildProtoReq50(params);
        APP.sendTradingServer(package50, undefined, {
            onSuccess: (res) => {
                const { isSuccess, result } = parseProtoRsp50(res, 'CancelOrderRsp');
                if (isSuccess) {
                    resolve('ok');
                } else {
                    reject(result);
                }
            },
            onFail: (err) => reject(err.message),
        } as Callback);
    });
};

/**
 * 撤销支付请求
 * @param param CancelPaymentReq
 * @returns
 */
export function CancelPaymentReq(param: orderType.CancelPaymentReq): Promise<string> {
    return new Promise((resolve, reject) => {
        const params = {
            protobufName: 'CancelPaymentReq',
            funCodeName: 'CancelPaymentReq',
            reqParams: {
                TradeID: param.tradeID,
                AccountID: param.accountID,
            },
            msgHeadParams: {
                AccountID: param.accountID,
                MarketID: param.marketID,
                GoodsID: param.goodsID,
            },
        };
        const package50 = buildProtoReq50(params);
        APP.sendTradingServer(package50, undefined, {
            onSuccess: (res) => {
                const { isSuccess, result } = parseProtoRsp50(res, 'CancelPaymentRsp');
                if (isSuccess) {
                    resolve('ok');
                } else {
                    reject(result);
                }
            },
            onFail: (err) => reject(err.message),
        } as Callback);
    });
}
