package io.wdd.func.xray.persisit;

import io.wdd.common.handler.MyRuntimeException;
import io.wdd.func.oss.config.OctopusObjectSummary;
import io.wdd.func.oss.config.OssConfig;
import io.wdd.func.oss.service.OSSCoreService;
import io.wdd.func.oss.service.OssBackendSelect;
import io.wdd.func.xray.beans.node.ProxyNode;
import io.wdd.func.xray.beans.node.XrayConfigInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;

import static io.wdd.func.xray.persisit.cache.ProxyNodeCache.PROXY_NODE_MAP;
import static io.wdd.func.xray.persisit.cache.ProxyNodeCache.SUBSCRIBE_NODE_NAME;

@Service
@Slf4j
public class XrayOssOperator {

    /**
     * 订阅文件实际存储的桶的名称
     * 实际为 Oracle-Seoul-2 下面的桶
     */
    private static final String SUBSCRIBE_OSS_BUCKET_PREFIX = "seoul";
    @Resource
    OssBackendSelect ossBackendSelect;
    @Resource
    OSSCoreService ossCoreService;

    /**
     * 为代理链的每一个节点 上传配置文件到OSS中
     *
     * @param allProxyNodeList
     */
    public void uploadXrayConfigToOSS(Collection<ProxyNode> allProxyNodeList) {

        // 所有的文件全部存储至一个对象存储的后端中
        OssConfig ossConfig = ossBackendSelect.oneRandom();
        log.info(
                "[Xray] 开始上传所有的xray config 至对象存储中 => [{}]",
                ossConfig.getName() + "-" + ossConfig.getRegion()
        );

        // 依次存储所有的config文件
        allProxyNodeList
                .stream()
                .forEach(
                        node -> {
                            XrayConfigInfo xrayConfigInfo = node.getXrayConfigInfo();
                            // 上传OSS
                            OctopusObjectSummary octopusObjectSummary = ossCoreService
                                    .createObject(
                                            ossConfig,
                                            xrayConfigInfo.getXrayConfigFileName(),
                                            xrayConfigInfo.getXrayConfigFile()
                                    );
                            // 设置oss的相关信息

                            xrayConfigInfo.setObjectSummary(octopusObjectSummary);
                        }
                );
    }

    /**
     * 上传 订阅文件信息 件到OSS
     *
     * @return
     */
    public XrayConfigInfo uploadSubscribeFileToOSS() {

        XrayConfigInfo subscribeXrayConfigInfo = PROXY_NODE_MAP
                .get(SUBSCRIBE_NODE_NAME).
                getXrayConfigInfo();


        // 存储到特定的桶中
        OssConfig ossConfig = ossBackendSelect.one(SUBSCRIBE_OSS_BUCKET_PREFIX);

        log.info(
                "开始存储 Xray订阅文件 [ {} ] 至OSS [ {} ]中",
                subscribeXrayConfigInfo,
                ossConfig
        );

        // 删除旧的订阅信息
        boolean deleteObject = ossCoreService.deleteObject(
                ossConfig,
                SUBSCRIBE_OSS_BUCKET_PREFIX,
                subscribeXrayConfigInfo.getXrayConfigFileName()
        );
        if (!deleteObject) {
            log.error(
                    "删除旧的订阅信息失败! 请检查桶 => [{}]， 文件名称为 => [ {} ]",
                    ossConfig,
                    subscribeXrayConfigInfo.getXrayConfigFileName()
            );
            throw new MyRuntimeException();
        }

        // 上传新的订阅信息
        OctopusObjectSummary objectSummary = ossCoreService.createObject(
                ossConfig,
                subscribeXrayConfigInfo.getXrayConfigFileName(),
                subscribeXrayConfigInfo.getXrayConfigFile()
        );

        subscribeXrayConfigInfo.setObjectSummary(objectSummary);

        return subscribeXrayConfigInfo;
    }


}
