package io.wdd.func.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.wdd.common.response.R;
import io.wdd.func.xray.beans.node.ProxyNode;
import io.wdd.func.xray.beans.node.XrayConfigInfo;
import io.wdd.func.xray.service.XrayCallAgent;
import io.wdd.func.xray.service.XrayCoreService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

import static io.wdd.func.xray.persisit.cache.ProxyNodeCache.PROXY_NODE_TOPIC_NAME_MAP;

@RestController
@RequestMapping("/server/func/xray")
@Api(tags = "Xray控制")
public class XrayController {

    @Resource
    XrayCoreService xrayCoreService;

    @Resource
    XrayCallAgent xrayCallAgent;


    @PostMapping("/chain/transform")
    @ApiOperation("[解析] - 解析代理链字符串")
    public R<List<List<ProxyNode>>> transformProxyChainString(
            @ApiParam(name = "proxyChainList", value = "代理链字符串，seoul2->tokyo2, 多条链使用,分隔")
            @RequestParam("proxyChainList") List<String> proxyChainList,
            @ApiParam(name = "isTopicName", value = "是否使用AgentTopicName, 默认为AgentName")
            @RequestParam(value = "isTopicName", required = false, defaultValue = "false") boolean isTopicName
    ) {

        return R.ok(
                xrayCoreService.transformProxyChainString(
                        proxyChainList,
                        isTopicName
                )
        );

    }

    @PostMapping("/chain/debug")
    @ApiOperation("[解析] - debug 代理链完整配置")
    public R<List<XrayConfigInfo>> debugProxyChainString(
            @ApiParam(name = "proxyChainList", value = "代理链字符串，seoul2->tokyo2, 多条链使用,分隔")
            @RequestParam("proxyChainList") List<String> proxyChainList,
            @ApiParam(name = "isTopicName", value = "是否使用AgentTopicName, 默认为AgentName")
            @RequestParam(value = "isTopicName", required = false, defaultValue = "false") boolean isTopicName
    ) {

        return R.ok(
                xrayCoreService.generateXrayConfigFromNodeList(
                        xrayCoreService.transformProxyChainString(
                                proxyChainList,
                                isTopicName
                        )
                )
        );
    }

    @PostMapping("/chain/procedure")
    @ApiOperation("[实际] - 代理链全流程配置上传下发")
    public R<List<XrayConfigInfo>> ProxyChainUpdateProcedure(
            @ApiParam(name = "proxyChainList", value = "代理链字符串，seoul2->tokyo2, 多条链使用,分隔")
            @RequestParam("proxyChainList") List<String> proxyChainList,
            @ApiParam(name = "isTopicName", value = "是否使用AgentTopicName, 默认为AgentName")
            @RequestParam(value = "isTopicName", required = false, defaultValue = "false") boolean isTopicName
    ) {

        return R.ok(
                xrayCoreService.ProxyChainUpdateProcedure(
                        proxyChainList,
                        isTopicName
                )
        );
    }

    @PostMapping("/cache/manual")
    @ApiOperation("[缓存] - 手动更新代理链缓存")
    public R<HashMap<String, ProxyNode>> manualUpdateProxyNodeListCache() {

        return R.ok(
                xrayCoreService.manualUpdateProxyNodeListCache()
        );
    }

    @GetMapping("/cache/get")
    @ApiOperation("[缓存] - 获取代理链缓存")
    public R<HashMap<String, ProxyNode>> getProxyNodeListCache() {

        return R.ok(PROXY_NODE_TOPIC_NAME_MAP);

    }


    /*@GetMapping("/test")
    public void test() {

        List<List<ProxyNode>> allNetworkPathList = new ArrayList<>();

        ArrayList<ProxyNode> pathA = new ArrayList<>(
                Arrays.asList(
                        shanghai,
                        seoul2,
                        tokyo2,
                        phoenix2
                )
        );

        ArrayList<ProxyNode> pathB = new ArrayList<>(
                Arrays.asList(
                        shanghai,
                        seoul2,
                        london2
                )
        );

        ArrayList<ProxyNode> pathC = new ArrayList<>(
                Arrays.asList(
                        seoul2,
                        phoenix2
                )
        );

        ArrayList<ProxyNode> pathD = new ArrayList<>(
                Collections.singletonList(
                        chengduAgent
                )
        );

        ArrayList<ProxyNode> pathE = new ArrayList<>(
                Collections.singletonList(
                        seoul5
                )
        );

//        allNetworkPathList.add(pathA);
//        allNetworkPathList.add(pathD);
        allNetworkPathList.add(pathE);
//        allNetworkPathList.add(pathB);
//        allNetworkPathList.add(pathC);

        xrayCoreService.generateXrayConfigFromNodeList(allNetworkPathList);

        xrayCallAgent.uploadXrayConfigToOSS(allNetworkPathList.get(0));

        xrayCallAgent.buildXrayUpdateResult(allNetworkPathList.get(0));

        System.out.println("Xray controller test() 结束！");

    }*/
}
