// 用户相关请求模块
import APP from '@/services';
import { getUserId } from '@/services/bus/account';
import { buildProtoReq50, parseProtoRsp50 } from '@/services/socket/protobuf/buildReq';
import { getUUID } from '@/utils/qt/common';
import { Callback } from '@/utils/websocket/index';
import { delUserReceiveInfoReq, queryProvinceAndCityParam, receiverInfo, userReceiveInfoReq, userReceiveIsDefaultReq } from './interface/index';

/**
 * 查询用户收货信息表
 */
export const queryAddressInfoList = (): Promise<receiverInfo[]> => {
    return new Promise((resolve, reject) => {
        const params = {
            protobufName: 'SearchReceiverReq',
            funCodeName: 'SearchReceiverReq',
            reqParams: {
                UserID: getUserId().toString(),
            },
        };
        const package50 = buildProtoReq50(params);
        APP.sendTradingServer(package50, undefined, {
            onSuccess: (res) => {
                const { isSuccess, result } = parseProtoRsp50(res, 'SearchReceiverRsp');
                if (isSuccess) {
                    resolve(result.InfoList);
                } else {
                    reject(result);
                }
            },
            onFail: (err) => reject(err.message),
        } as Callback);
    });
};

/**
 * 获取省市区
 * @param divisionlevel 省为province，其余为空
 * @param parentcode 查市时填省代码，查区时填市代码
 */
export const queryProvinceAndCity = (param: queryProvinceAndCityParam): Promise<any> => {
    return new Promise((resolve, reject) => {
        const params = {
            statement: 'searchSingnProvince',
            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);
    });
};

/**
 * 新增地址
 */
export const addOrModifyUserReceiveInfo = (param: userReceiveInfoReq): Promise<any> => {
    return new Promise((resolve, reject) => {
        const req = {
            UserID: getUserId(),
            ClientSerialID: getUUID(),
            CardTypeID: 0,
        };
        const params = {
            protobufName: 'UserReceiveInfoReq',
            funCodeName: 'UserReceiveInfoReq',
            reqParams: Object.assign(req, param),
        };
        const package50 = buildProtoReq50(params);
        APP.sendTradingServer(package50, undefined, {
            onSuccess: (res) => {
                const { isSuccess, result } = parseProtoRsp50(res, 'UserReceiveInfoRsp');
                if (isSuccess) {
                    resolve(result);
                } else {
                    reject(result);
                }
            },
            onFail: (err) => reject(err.message),
        } as Callback);
    });
};

/**
 * 设置默认地址
 */
export const setDefaultAddress = (param: userReceiveIsDefaultReq): Promise<any> => {
    return new Promise((resolve, reject) => {
        const req = {
            UserID: getUserId(),
        };
        const params = {
            protobufName: 'UserReceiveIsDefaultReq',
            funCodeName: 'UserReceiveIsDefaultReq',
            reqParams: Object.assign(req, param),
        };
        const package50 = buildProtoReq50(params);
        APP.sendTradingServer(package50, undefined, {
            onSuccess: (res) => {
                const { isSuccess, result } = parseProtoRsp50(res, 'UserReceiveIsDefaultRsp');
                if (isSuccess) {
                    resolve(result);
                } else {
                    reject(result);
                }
            },
            onFail: (err) => reject(err.message),
        } as Callback);
    });
};

/**
 * 新增修改收货地址请求
 */
export const changeAddress = (param: userReceiveInfoReq): Promise<any> => {
    return new Promise((resolve, reject) => {
        const req = {
            UserID: getUserId(),
            ClientSerialID: getUUID(),
            CardTypeID: 0,
        };
        const params = {
            protobufName: 'UserReceiveInfoReq',
            funCodeName: 'UserReceiveInfoReq',
            reqParams: Object.assign(req, param),
        };
        const package50 = buildProtoReq50(params);
        APP.sendTradingServer(package50, undefined, {
            onSuccess: (res) => {
                const { isSuccess, result } = parseProtoRsp50(res, 'UserReceiveInfoRsp');
                if (isSuccess) {
                    resolve(result);
                } else {
                    reject(result);
                }
            },
            onFail: (err) => reject(err.message),
        } as Callback);
    });
};

/**
 * 删除收货地址请求
 */
export const delAddress = (param: delUserReceiveInfoReq): Promise<any> => {
    return new Promise((resolve, reject) => {
        const params = {
            protobufName: 'DelUserReceiveInfoReq',
            funCodeName: 'DelUserReceiveInfoReq',
            reqParams: param,
        };
        const package50 = buildProtoReq50(params);
        APP.sendTradingServer(package50, undefined, {
            onSuccess: (res) => {
                const { isSuccess, result } = parseProtoRsp50(res, 'DelUserReceiveInfoRsp');
                if (isSuccess) {
                    resolve(result);
                } else {
                    reject(result);
                }
            },
            onFail: (err) => reject(err.message),
        } as Callback);
    });
};
