package com.xhpc.evcs.dto;

import com.xhpc.evcs.domain.AuthSecretToken;
import com.xhpc.evcs.encryption.Aes128Cbc;
import com.xhpc.evcs.http.ServerInternalException;
import com.xhpc.evcs.utils.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class DTOJsonHelper {

    public static <T> T parseResponseData(String responseBody, Class<T> clz, String jsonListKey) {

        if (responseBody != null) {
            try {
                CommonResponse commonResponse = JSONUtil.readParams(responseBody, CommonResponse.class);
                return (T) commonResponse.anyDataType(clz, jsonListKey);
            } catch (Exception e) {
                String msg = e.getMessage();
                log.error(msg);
                throw new ServerInternalException(msg);
            }
        }
        return null;
    }

    public static <T> T parseResponseData(String responseBody, Class<T> clz, AuthSecretToken authSecretToken) {

        if (responseBody != null) {
            try {
                CommonResponse commonResponse = JSONUtil.readParams(responseBody, CommonResponse.class);
                String data = (String) commonResponse.getData();
                if (authSecretToken != null && !data.isEmpty()) {
                    data = Aes128Cbc.decryptString(data, authSecretToken.getDataSecret(), authSecretToken.getDataSecretIV());
                }
                if (!data.isEmpty()) {
                    return JSONUtil.readParams(data, clz);
                } else {
                    log.error(commonResponse.getMsg());
                    return null;
                }
            } catch (Exception e) {
                String msg = e.getMessage();
                log.error("×operator3rdpty[{}]: {}", authSecretToken.getOperatorId3irdpty(), msg);
            }
        }
        return null;
    }

    public static <T> T parseResponseData(String responseBody, Class<T> clz) {

        if (responseBody != null) {
            try {
                CommonResponse commonResponse = JSONUtil.readParams(responseBody, CommonResponse.class);
                String data = (String) commonResponse.getData();
                if (!data.isEmpty()) {
                    return JSONUtil.readParams(data, clz);
                } else {
                    log.error(commonResponse.getMsg());
                    return null;
                }
            } catch (Exception e) {
                String msg = e.getMessage();
                log.error(msg);
                throw new ServerInternalException(msg);
            }
        }
        return null;
    }

    public static <T> List<T> parseResponseDataAsList(String responseBody, Class<T> clz, String jsonListKey) {

        if (responseBody != null) {
            try {
                CommonResponse commonResponse = JSONUtil.readParams(responseBody, CommonResponse.class);
                return (List<T>) commonResponse.anyDataTypeList(clz, jsonListKey);
            } catch (Exception e) {
                String msg = e.getMessage();
                log.error(msg);
                throw new ServerInternalException(msg);
            }
        }
        return null;
    }

    public static <T> List<T> parseResponseDataAsList(String responseBody, Class<T> clz) {

        if (responseBody != null) {
            try {
                CommonResponse commonResponse = JSONUtil.readParams(responseBody, CommonResponse.class);
                return (List<T>) commonResponse.anyDataTypeList(clz);
            } catch (Exception e) {
                String msg = e.getMessage();
                log.error(msg);
                throw new ServerInternalException(msg);
            }
        }
        return null;
    }

}
