package cn.reghao.tnb.admin.controller;

import cn.reghao.tnb.common.auth.AuthUser;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.admin.model.vo.EurekaInstance;
import cn.reghao.tnb.admin.model.vo.EurekaService;
import cn.reghao.tnb.admin.model.vo.ServiceInfo;
import cn.reghao.tnb.admin.service.TnbEurekaService;
import cn.reghao.tnb.admin.service.TnbService;
import cn.reghao.tnb.admin.service.TnbZkService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author reghao
 * @date 2024-11-12 15:29:36
 */
@Tag(name = "tnb 服务治理接口")
@RestController
@RequestMapping("/api/admin1/tnb")
@AuthUser
public class TnbServiceController {
    private final TnbZkService tnbZkService;
    private final TnbService tnbService;
    private final TnbEurekaService tnbEurekaService;

    public TnbServiceController(TnbZkService tnbZkService, TnbService tnbService, TnbEurekaService tnbEurekaService) {
        this.tnbZkService = tnbZkService;
        this.tnbService = tnbService;
        this.tnbEurekaService = tnbEurekaService;
    }

    @Operation(summary = "zk 中的服务")
    @GetMapping(value = "/zk", produces = MediaType.APPLICATION_JSON_VALUE)
    public String zkServicePage() throws Exception {
        String zkPath = "/";
        List<String> list = tnbZkService.getZkService(zkPath);
        return WebResult.success(list);
    }

    @Operation(summary = "zk 中的服务")
    @GetMapping(value = "/zookeeper", produces = MediaType.APPLICATION_JSON_VALUE)
    public String zkService(@RequestParam("path") String path) throws Exception {
        List<ServiceInfo> list = new ArrayList<>();
        if ("dubbo".equals(path)) {
            list = tnbZkService.getDubboService();
        } else if ("services".equals(path)) {
            list = tnbZkService.getCloudService();
        } else if ("zookeeper".equals(path)) {
        }

        return WebResult.success(list);
    }

    @Operation(summary = "zk 中的 dubbo 服务")
    @GetMapping(value = "/zk/dubbo", produces = MediaType.APPLICATION_JSON_VALUE)
    public String zkDubboService() throws Exception {
        List<ServiceInfo> list = tnbZkService.getDubboService();
        return WebResult.success(list);
    }

    @Operation(summary = "zk 中的 springcloud 服务")
    @GetMapping(value = "/zk/springcloud", produces = MediaType.APPLICATION_JSON_VALUE)
    public String zkSpringcloudService() throws Exception {
        List<ServiceInfo> list = tnbZkService.getCloudService();
        return WebResult.success(list);
    }

    @Operation(summary = "eureka 中的 springcloud 服务")
    @GetMapping(value = "/eureka/springcloud", produces = MediaType.APPLICATION_JSON_VALUE)
    public String springcloudServicePage() {
        List<EurekaInstance> instanceList = tnbEurekaService.getRegistryServices();
        return WebResult.success(instanceList);
    }

    @Operation(summary = "eureka 中的 springcloud 服务")
    @GetMapping(value = "/eureka/springcloud1", produces = MediaType.APPLICATION_JSON_VALUE)
    public String springcloudService() {
        List<EurekaService> serviceList = tnbEurekaService.getServiceList();
        return WebResult.success(serviceList);
    }

    @Operation(summary = "springcloud 服务")
    @PostMapping(value = "/springcloud/service", produces = MediaType.APPLICATION_JSON_VALUE)
    public String selectServices(@RequestParam("appName") List<String> appNames) {
        tnbService.add(appNames);
        return WebResult.success();
    }

    @Operation(summary = "springcloud 服务页面")
    @PostMapping(value = "/springcloud/delete/{appName}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteSpringcloudService(@PathVariable("appName") String appName) {
        tnbZkService.deleteNode(appName);
        return WebResult.success();
    }
}
