package com.eshore.gringotts.chain.configuration;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import org.fisco.bcos.sdk.BcosSDK;
import org.fisco.bcos.sdk.config.ConfigOption;
import org.fisco.bcos.sdk.config.exceptions.ConfigException;
import org.fisco.bcos.sdk.config.model.ConfigProperty;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Fisco配置
 *
 * @author lanyuanxiaoyao
 * @date 2024-09-27
 */
@Configuration
public class FiscoConfiguration {
    @Bean
    public ConfigProperty defaultConfigProperty() {
        ConfigProperty property = new ConfigProperty();
        property.setCryptoMaterial(
                MapUtil.<String, Object>builder()
                        .put("certPath", "conf")
                        .build()
        );
        property.setNetwork(
                MapUtil.<String, Object>builder()
                        .put("peers", ListUtil.of(
                                // "192.168.100.122:20200",
                                // "192.168.100.122:20201"
                                // "192.168.100.122:30301",
                                // "192.168.100.122:30302",
                                // "192.168.100.122:30303"
                                "192.168.100.118:20200",
                                "192.168.100.118:20201"
                                // "192.168.100.119:30300",
                                // "192.168.100.119:30301",
                                // "192.168.100.120:30300",
                                // "192.168.100.120:30301"
                        ))
                        .build()
        );
        property.setAccount(
                MapUtil.<String, Object>builder()
                        .put("keyStoreDir", "account")
                        .put("accountAddress", "account")
                        .put("accountFileFormat", "pem")
                        .put("password", "")
                        .put("accountFilePath", "")
                        .build()
        );
        property.setThreadPool(
                MapUtil.<String, Object>builder()
                        .put("channelProcessorThreadSize", "16")
                        .put("receiptProcessorThreadSize", "16")
                        .put("maxBlockingQueueSize", "102400")
                        .build()
        );
        return property;
    }

    @Bean
    public ConfigOption defaultConfigOption(@Qualifier("defaultConfigProperty") ConfigProperty configProperty) throws ConfigException {
        return new ConfigOption(configProperty);
    }

    @Bean
    public BcosSDK bcosSDK(@Qualifier("defaultConfigOption") ConfigOption configOption) {
        return new BcosSDK(configOption);
    }
}
