package cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.paperInvoice.dto.response;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;

@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder(toBuilder = true)
@Data
public class _SzhtxxPaperInvoiceBaseResponse implements Serializable {

//    请求的报文为 json 结构，返回报文统一封装成 {“ENCMSG”:“编码串”}。“编码串”中内容为 Base64 编码后的内容，此内容 base64 解码后至少包含{“retcode”: “X”, “retmsg”:“XXX”}

    @ApiModelProperty("batchId")
    protected String batchId;

    @ApiModelProperty("fpqqlsh invoiceApplySn 发票请求唯一流水号")
    protected String fpqqlsh;

    @ApiModelProperty("编码串")
    @JsonProperty("ENCMSG")
    @JSONField(name = "ENCMSG")
    protected String ENCMSG;


    protected String base64ReplaceAndDecode(String input) throws UnsupportedEncodingException {

        String unShell = StrUtil.replaceChars(input, "_", "+");
        byte[] buf     = unShell.getBytes();

        String deCode = StrUtil.str(Base64.decode(buf), "GBK");

        String clearJson = StrUtil.removeAny(deCode,

                /**************************************** add at 20220519 start ****************************************/

                /* 水平制表符 */ StrUtil.TAB,
                /* 回车 classic Mac */ StrUtil.CR,
                /* 回车 classic Unix */ StrUtil.LF,
                /* 回车 classic Windows */ StrUtil.CRLF

                /**************************************** add at 20220519 start ****************************************/

        );

        return clearJson;

    }

    protected String base64Decode(String input) throws UnsupportedEncodingException {

        byte[] buf = input.getBytes();

        String deCode = StrUtil.str(Base64.decode(buf), "GBK");

        String clearJson = StrUtil.removeAny(deCode,

                /**************************************** add at 20220519 start ****************************************/

                /* 水平制表符 */ StrUtil.TAB,
                /* 回车 classic Mac */ StrUtil.CR,
                /* 回车 classic Unix */ StrUtil.LF,
                /* 回车 classic Windows */ StrUtil.CRLF

                /**************************************** add at 20220519 start ****************************************/

        );

        return clearJson;

//        return StrUtil.str(Base64.decode(buf), "GBK");

    }

}
