package com.ruoyi.platform.controller.service;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.GenericsAjaxResult;
import com.ruoyi.platform.domain.Service;
import com.ruoyi.platform.domain.ServiceVersion;
import com.ruoyi.platform.service.ServiceService;
import com.ruoyi.platform.vo.serviceVos.ServiceVersionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("service")
@Api("服务")
public class ServiceController extends BaseController {
    @Resource
    private ServiceService serviceService;


    @GetMapping
    @ApiOperation("分页查询服务列表")
    public GenericsAjaxResult<Page<Service>> queryByPageService(@RequestParam(value = "service_name", required = false) String serviceName,
                                                                @RequestParam(value = "service_type", required = false) String serviceType,
                                                                @RequestParam("page") int page,
                                                                @RequestParam("size") int size) throws IOException {
        Service service = new Service();
        service.setServiceName(serviceName);
        service.setServiceType(serviceType);
        PageRequest pageRequest = PageRequest.of(page, size);
        return genericsSuccess(serviceService.queryByPageService(service, pageRequest));
    }

    @GetMapping("/serviceVersion")
    @ApiOperation("分页查询服务版本列表")
    public GenericsAjaxResult<Page<ServiceVersionVo>> queryByPageServiceVersion(@RequestParam(value = "run_state", required = false) String runState,
                                                                                @RequestParam(value = "version", required = false) String version,
                                                                                @RequestParam("service_id") Long serviceId,
                                                                                @RequestParam("page") int page,
                                                                                @RequestParam("size") int size) throws IOException {
        ServiceVersion serviceVersion = new ServiceVersion();
        serviceVersion.setRunState(runState);
        serviceVersion.setVersion(version);
        serviceVersion.setServiceId(serviceId);
        return genericsSuccess(serviceService.queryByPageServiceVersion(serviceVersion, page, size));
    }

    @PostMapping
    @ApiOperation("新增服务")
    public GenericsAjaxResult<Service> addService(@RequestBody Service service) {
        return genericsSuccess(serviceService.addService(service));
    }

    @PostMapping("/serviceVersion")
    @ApiOperation("新增服务版本")
    public GenericsAjaxResult<ServiceVersion> addServiceVersion(@RequestBody ServiceVersionVo serviceVersionVo) throws Exception {
        return genericsSuccess(serviceService.addServiceVersion(serviceVersionVo));
    }

    @PutMapping
    @ApiOperation("编辑服务")
    public GenericsAjaxResult<Service> editService(@RequestBody Service service) {
        return genericsSuccess(serviceService.editService(service));
    }

    @PutMapping("/serviceVersion")
    @ApiOperation("编辑服务版本")
    public GenericsAjaxResult<String> editServiceVersion(@RequestBody ServiceVersionVo serviceVersionVo) throws Exception {
        return genericsSuccess(serviceService.editServiceVersion(serviceVersionVo));
    }

    @GetMapping("/serviceDetail/{id}")
    @ApiOperation("查询服务详细信息")
    public GenericsAjaxResult<Service> getService(@PathVariable("id") Long id) {
        return genericsSuccess(serviceService.getService(id));
    }

    @GetMapping("/serviceVersionDetail/{id}")
    @ApiOperation("查询服务版本详细信息")
    public GenericsAjaxResult<ServiceVersionVo> getServiceVersion(@PathVariable("id") Long id) throws IOException {
        return genericsSuccess(serviceService.getServiceVersion(id));
    }

    @GetMapping("serviceVersionCompare")
    @ApiOperation("服务版本版本对比")
    public GenericsAjaxResult<Map<String, Object>> serviceVersionCompare(@RequestParam("id1") Long id1, @RequestParam("id2") Long id2) throws IllegalAccessException, IOException {
        return genericsSuccess(serviceService.serviceVersionCompare(id1, id2));
    }

    @GetMapping("/serviceVersionList/{id}")
    @ApiOperation("查询服务版本列表")
    public GenericsAjaxResult<List<ServiceVersion>> serviceVersionList(@PathVariable("id") Long id) {
        return genericsSuccess(serviceService.serviceVersionList(id));
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除服务")
    public GenericsAjaxResult<String> deleteService(@PathVariable("id") Long id) throws Exception {
        return genericsSuccess(serviceService.deleteService(id));
    }

    @DeleteMapping("/serviceVersion/{id}")
    @ApiOperation("删除服务版本")
    public GenericsAjaxResult<String> deleteServiceVersion(@PathVariable("id") Long id) {
        return genericsSuccess(serviceService.deleteServiceVersion(id));
    }

//    @PostMapping("/runServiceVersion/{id}")
//    @ApiOperation("启动服务版本")
//    public GenericsAjaxResult<String> runServiceVersion(@PathVariable("id") Long id) {
//        return genericsSuccess(serviceService.runServiceVersion(id));
//    }

    @DeleteMapping("/stopServiceVersion/{id}")
    @ApiOperation("停止服务版本")
    public GenericsAjaxResult<String> stopServiceVersion(@PathVariable("id") Long id) {
        return genericsSuccess(serviceService.stopServiceVersion(id));
    }

//    @PutMapping("/updateServiceVersion")
//    @ApiOperation("更新服务版本")
//    public GenericsAjaxResult<String> updateServiceVersion(@RequestBody ServiceVersion serviceVersion) {
//        return genericsSuccess(serviceService.updateServiceVersion(serviceVersion));
//    }

    @GetMapping("/getServiceVersionLog")
    @ApiOperation("获取服务版本日志")
    public GenericsAjaxResult<HashMap<String, String>> getServiceVersionLog(@RequestParam("id") Long id,
                                                                            @RequestParam("start_time") String startTime, @RequestParam("end_time") String endTime) {
        return genericsSuccess(serviceService.getServiceVersionLog(id, startTime, endTime));
    }

    @GetMapping("/getServiceVersionDocs/{id}")
    @ApiOperation("获取服务版本文档")
    public GenericsAjaxResult<Map<String, Object>> getServiceVersionDocs(@PathVariable("id") Long id) {
        return genericsSuccess(serviceService.getServiceVersionDocs(id));
    }
}
