package common.utils.bsn;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.HttpKit;
import common.jfinal.AppConfig;

import java.util.HashMap;
import java.util.Map;

public class BsnSDK {
    private static final String DOMAIN = "https://slzxopenapi.ddc.bsnbase.com";
    private static final String API_TOKEN = System.getenv("BSN_API_TOKEN");
    private static final int CHAIN_ID = 3;// 3是泰安链
    private static final int TOKEN_TYPE = 1155;

    public static JSONObject accountInfo() throws Exception {
        // 加载固定参数
        Map<String, Object> fullParams = new HashMap<>();
        
        return invokeApi("/ddcoai/sys/v1/user/account/info/search", fullParams);
    }

    public static JSONObject createChainAccount(String accountName) throws Exception {
        // 加载固定参数
        Map<String, Object> fullParams = new HashMap<>();
        
        fullParams.put("opbChainClientName", accountName);
        fullParams.put("opbChainId", CHAIN_ID + "");

        return invokeApi("/ddcoai/sys/v1/chainaccount/save", fullParams);
    }

    public static JSONObject createNft(String desc, String filePath, int pubTotal, String userTradeCode) throws Exception {
        return createNft(desc, filePath, pubTotal, userTradeCode, System.getenv("BSN_OWNER"));
    }

    public static JSONObject createNft(String desc, String filePath, int pubTotal, String userTradeCode, String owner) throws Exception {
        // 加载固定参数
        Map<String, Object> fullParams = new HashMap<>();

        fullParams.put("desc", desc);
        fullParams.put("filePath", filePath);
        fullParams.put("opbChainId", CHAIN_ID);
        fullParams.put("owner", owner);
        fullParams.put("pubTotal", pubTotal);
        fullParams.put("securityData", "1");
        fullParams.put("tokenType", TOKEN_TYPE);
        fullParams.put("userTradeCode", userTradeCode);

        return invokeApi("/ddcoai/sys/v1/nft/url/save", fullParams);
    }

    public static JSONObject sendNft(String recipientAccount, int sendNumber, String tokenId, String orderSn) throws Exception {
        return sendNft(System.getenv("BSN_OWNER"), recipientAccount, sendNumber, tokenId, orderSn);
    }

    public static JSONObject sendNft(String owner, String recipientAccount, int sendNumber, String tokenId, String orderSn) throws Exception {
        // 加载固定参数
        Map<String, Object> fullParams = new HashMap<>();
        fullParams.put("opbChainId", CHAIN_ID);
        fullParams.put("owner", System.getenv("BSN_OWNER"));
        fullParams.put("recipientAccount", recipientAccount);
        fullParams.put("sendNumber", sendNumber);
        fullParams.put("tokenId", tokenId);
        fullParams.put("tokenType", TOKEN_TYPE);
        fullParams.put("userTradeCode", orderSn);
        fullParams.put("securityData", "1");

        return invokeApi("/ddcoai/sys/v1/nft/send", fullParams);
    }

    public static JSONObject search(String userTradeCode) throws Exception {
        // 加载固定参数
        Map<String, Object> fullParams = new HashMap<>();
        fullParams.put("userTradeCode", userTradeCode);

        return invokeApi("/ddcoai/sys/v2/nft/tx/search", fullParams);
    }

    private static JSONObject invokeApi(String apiPath, Map<String, Object> bizParams) throws Exception {
        // 请求头
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("apitoken", API_TOKEN);

        // 发送请求
        String postParams = JSON.toJSONString(bizParams);// 构建请求 URL
        String requestUrl = DOMAIN + apiPath;
        String responseBody = HttpKit.post(requestUrl, null, postParams, headers);

        return JSONObject.parseObject(responseBody);
    }
    
    public static void main(String[] args) {
        try {
            // AppConfig.LOGGER.info(accountInfo());
            // {"code":-1,"errorLogCode":"ERR:SL-ST_OAI1b264496b3b6477c99e3dc083b02f5bd52_1758076191510_ED","message":"MSG_10051035"}
            // {"code":0,"data":{"privateKey":"xxx","opbChainClientAddress":"0xcf6203a04a418d765141d285a9956c8046f1a343","publicKey":"xxx","opbChainClientName":"DLTBH_10002"},"errorLogMessage":"","message":"MSG_00000"}
            // AppConfig.LOGGER.info(createChainAccount("DLTBH_10002"));
            
            // AppConfig.LOGGER.info(createNft("nft", "http://117.72.208.239/uploads/20250910/2e9f839835224bb68726743caaa1a6b2.png", 100, "DLTBH_NFT_1"));
            // {"code":0,"data":{"txHash":"79444b6937494ed1c5fa605fc439653dfff8074696a84c70cbaf6127a3cbcb2a","status":0},"errorLogMessage":"","message":"MSG_00000"}
            // {"code":0,"data":{"txStatus":0,"tokenId":"144498","txHash":"0x79444b6937494ed1c5fa605fc439653dfff8074696a84c70cbaf6127a3cbcb2a","status":1},"errorLogMessage":"","message":"MSG_00000"}
            // AppConfig.LOGGER.info(search("DLTBH_NFT_1"));

            // {"code":0,"errorLogMessage":"","message":"MSG_00000"}
            // AppConfig.LOGGER.info(sendNft("0xe84cc57ea90db89e3a65bc704ce57198cc32daa0", 1000, "144498", "DLTBH_ORDER_101"));
            // {"code":0,"data":{"txStatus":0,"tokenId":"144498","txHash":"0x45a75d406dadad4b14d1aa148f87d34788db7ac5721cd653344d81da7395f7d4","status":1},"errorLogMessage":"","message":"MSG_00000"}
            // {"code":0,"data":{"txStatus":1,"txStatusMsg":"DDC1155:insufficient balance","tokenId":"144498","txHash":"0xc997b7c6790fbc7ea8d9b0753da25f215c781303dc7d9172e79cb89f48568eed","failMessage":"0x16:null","status":2},"errorLogMessage":"","message":"MSG_00000"}
            AppConfig.LOGGER.info(search("DLTBH_ORDER_101").toJSONString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
