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

import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.xml.AnXMLUtils;
import cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.paperInvoice.dto.response._SzhtxxPaperInvoiceBaseResponse;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
@JsonIgnoreProperties(ignoreUnknown = true)
@SuperBuilder(toBuilder = true)
@AllArgsConstructor
@NoArgsConstructor
@Data
public class SID20BitchLlqResponseBody extends _SzhtxxPaperInvoiceBaseResponse {

    @ApiModelProperty("密文-解密-反序列化")
    private SID20BitchLlqResponseBodyEncmsgVo encmsgVo;
    @ApiModelProperty("FPXT_COM_OUTPUT->DATA(base64(返回内容的 XML 结构体))")
    private FpxtComOutputStruct               fpxtComOutputStruct;

    @ApiModelProperty("ID0200 红字专用发票信息表上传_0200")
    private cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.paperInvoice.dto.response.SID20BitchLlq.ID0200.FpxtType fpxtTypeID0200;
    @ApiModelProperty("ID0201 红字专用发票信息表下载_0201")
    private cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.paperInvoice.dto.response.SID20BitchLlq.ID0201.FpxtType fpxtTypeID0201;

    /**
     * 第一层套
     *
     * @return
     */
    public SID20BitchLlqResponseBody unPacking() {

        try {
            String jsonStr = this.base64ReplaceAndDecode(this.ENCMSG);

            // Illegal unquoted character ((CTRL-CHAR, code 10))

            log.info("\nunPacking\n{}\n{}", this.ENCMSG, jsonStr);
            encmsgVo = AnJsonUtils.toObject(jsonStr, SID20BitchLlqResponseBodyEncmsgVo.class);
        } catch (UnsupportedEncodingException unsupportedEncodingException) {
            unsupportedEncodingException.printStackTrace();
        }

        return this;
    }

    /**
     * 第二层套
     *
     * @return
     */
    public SID20BitchLlqResponseBody unShell() {

        try {
            // 此处 responseMsg 为 xml
            log.info("\nunShell\n{}", this.encmsgVo.getResponseMsg());
            // 需要对DATA进行第二次解密处理
            String dataStr = this.regexLabel(this.encmsgVo.getResponseMsg(),"DATA");
            if(StringUtils.isNotBlank(dataStr)){
                dataStr =  this.base64ReplaceAndDecode(dataStr);
                String dataStr1 = dataStr;
                log.info("\ndataStr1\n{}", dataStr);
                dataStr =  dataStr.replaceAll("<OUTPUT>","");
                dataStr =  dataStr.replaceAll("</OUTPUT>","");
                fpxtComOutputStruct  = AnXMLUtils.xmlToObj(dataStr, FpxtComOutputStruct.class);
                fpxtComOutputStruct.setDATA(dataStr1);
                log.info("\n fpxtComOutputStruct \n{}", fpxtComOutputStruct);
            }else{
                // 此处 responseMsg 为 xml
                fpxtComOutputStruct = AnXMLUtils.xmlToObj(this.encmsgVo.getResponseMsg(), FpxtComOutputStruct.class);
            }

        } catch (Exception exception) {
            exception.printStackTrace();
        }

        return this;
    }

    /**
     * 第三层套
     *
     * @return
     */
    public SID20BitchLlqResponseBody unDataID0200() {

        try {


            // 此处的<DATA></DATA> 节有可以为空,怎样判定呢?
//            错误代码
//            0000 – 表示处理成功

            if ("0000".equals(this.fpxtComOutputStruct.getCODE())) {
                log.info("\nunDataID0201\n{}", this.fpxtComOutputStruct.getDATA());
                fpxtTypeID0200 = AnXMLUtils.xmlToObj(this.fpxtComOutputStruct.getDATA(), cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.paperInvoice.dto.response.SID20BitchLlq.ID0200.FpxtType.class);
            }


        } catch (Exception exception) {
            exception.printStackTrace();
        }

        return this;
    }

    public SID20BitchLlqResponseBody unDataID0201() {

        try {

            // 此处的<DATA></DATA> 节有可以为空,怎样判定呢?
//            错误代码
//            0000 – 表示处理成功

            if ("0000".equals(this.fpxtComOutputStruct.getCODE())) {
                log.info("\nunDataID0201\n{}", this.fpxtComOutputStruct.getDATA());
                fpxtTypeID0201 = AnXMLUtils.xmlToObj(this.fpxtComOutputStruct.getDATA(), cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.paperInvoice.dto.response.SID20BitchLlq.ID0201.FpxtType.class);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }

        return this;
    }

    public String regexLabel(String xml,String data) {
        //正则表达式
        String regex = "<"+data+">(.*?)</"+data+">";
        String context = "";
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(xml);
        while(m.find()){
            int i = 1;
            context = context + m.group(i) + ",";
            i++;

        }
        if(context.length() != 0) {
            context = context.substring(0,context.length()-1);
        }
        return context;
    }

    public static void main(String[] args) {

//        String encMsg = "ew0KICAgICJwYXJhbSI6ICI8P3htbCB2ZXJzaW9uPScxLjAnIGVuY29kaW5nPVwiR0JLXCI/PjxGUFhUX0NPTV9JTlBVVD48SUQ_MDIwMDwvSUQ_PERBVEE_UEQ5NGJXd2dkbVZ5YzJsdmJqMG5NUzR3SnlCbGJtTnZaR2x1WnowblIwSkxKejgrRFFvOFJsQllWRDROQ2lBZ0lDQThTVTVRVlZRK0RRb2dJQ0FnSUNBZ0lEeFNaV1JKYm5aU1pYRkNhV3hzUGcwS0lDQWdJQ0FnSUNBZ0lDQWdQRkpsY1U1emNuTmlhRDQ1TVRRME1ETXdNRGN3T0RReU9UUTFNVGs4TDFKbGNVNXpjbk5pYUQ0TkNpQWdJQ0FnSUNBZ0lDQWdJRHhMY0dwb1BqQThMMHR3YW1nK0RRb2dJQ0FnSUNBZ0lDQWdJQ0E4VTJKaWFENDJOakUxTkRZeE5qWXhOalk4TDFOaVltZytEUW9nSUNBZ0lDQWdJQ0FnSUNBOFVtVnhRbWxzYkU1dlBqWTJNVFUwTmpFMk5qRTJOakUzTURjeU5ERTNNekkwTmp3dlVtVnhRbWxzYkU1dlBnMEtJQ0FnSUNBZ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gIDxTemxiPjE8L1N6bGI+DQogICAgICAgICAgICA8SXNNdXRpUmF0ZT4wPC9Jc011dGlSYXRlPg0KICAgICAgICAgICAgPERhdGU+MjAxNy0wNy0yNjwvRGF0ZT4NCiAgICAgICAgICAgIDxCdXllck5hbWU+s6PW3dS0vNPQwrLEwc+/xry809DP3rmry748L0J1eWVyTmFtZT4NCiAgICAgICAgICAgIDxCdXlUYXhDb2RlPjkxMzIwNDEzTUExTU02WEg0RjwvQnV5VGF4Q29kZT4NCiAgICAgICAgICAgIDxTZWxsZXJOYW1lPs7k0sPJvTE8L1NlbGxlck5hbWU+DQogICAgICAgICAgICA8U2VsbFRheENvZGU+OTE0NDAzMDA3MDg0Mjk0NTE5PC9TZWxsVGF4Q29kZT4NCiAgICAgICAgICAgIDxBbW91bnQ+LTE3Mi4wNTwvQW1vdW50Pg0KICAgICAgICAgICAgPFRheFJhdGU+MC4xNzwvVGF4UmF0ZT4NCiAgICAgICAgICAgIDxUYXg+LTI5LjI1PC9UYXg+DQogICAgICAgICAgICA8UmVxTWVtbz4wMDAwMDAwMTEwPC9SZXFNZW1vPg0KICAgICAgICAgICAgPFNQQk1CQkg+MTMuMDwvU1BCTUJCSD4NCiAgICAgICAgICAgIDxTTEJaPjA8L1NMQlo+DQogICAgICAgICAgICA8UmVkSW52UmVxQmlsbE14Pg0KICAgICAgICAgICAgICAgIDxHb29kc014Pg0KICAgICAgICAgICAgICAgICAgICA8R29vZHNOYW1lPsT4v/M8L0dvb2RzTmFtZT4NCiAgICAgICAgICAgICAgICAgICAgPEdvb2RzVW5pdD621jwvR29vZHNVbml0Pg0KICAgICAgICAgICAgICAgICAgICA8R29vZHNQcmljZT4xNzIuMDU8L0dvb2RzUHJpY2U+DQogICAgICAgICAgICAgICAgICAgIDxHb29kc1RheFJhdGU+MC4xNzwvR29vZHNUYXhSYXRlPg0KICAgICAgICAgICAgICAgICAgICA8R29vZHNHZ3hoPjwvR29vZHNHZ3hoPg0KICAgICAgICAgICAgICAgIC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0lOUFVUPiJ9";

        SID20BitchLlqResponseBody responseBody = SID20BitchLlqResponseBody
                .builder()
                .ENCMSG(encMsg)
                .build();

        responseBody.unPacking();

//        log.info("{}", JSONUtil.toJsonStr(responseBody.getEncmsgVo()));


    }
}
