package com.jsc.oscm.controller;

import com.alibaba.fastjson.JSON;
import com.jsc.oscm.entity.Project;
import com.jsc.oscm.model.ReturnT;
import com.jsc.oscm.service.ProjectService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
public class ProjectController {

    @Resource
    ProjectService projectService;

    @PostMapping("/project/file")
    String upload(@RequestParam("file") MultipartFile file) {
        ReturnT<List<String>> returnT;
        String savedName = projectService.saveFile(file);
        if (savedName != null && savedName.length() > 0) {
            returnT = new ReturnT<>();
            returnT.code = 200;
            returnT.content = new ArrayList<>();
            returnT.content.add(file.getOriginalFilename());
            returnT.content.add(Objects.requireNonNull(file.getOriginalFilename()).substring(0, file.getOriginalFilename().indexOf(".")));
        } else {
            returnT = new ReturnT<>(500, "", null);
        }
        return JSON.toJSONString(returnT);
    }

    @PostMapping("/project")
    String saveProject(@RequestBody List<Project> project) {
        ReturnT<String> returnT;
        if (projectService.saveProject(project) != null) {
            returnT = new ReturnT<>(200, "", "");
        } else {
            returnT = new ReturnT<>(500, "保存失败", "");
        }

        return JSON.toJSONString(returnT);
    }

    @GetMapping("/project/deploy/zip")
    String deploy(@RequestParam String address,
                  @RequestParam String project,
                  @RequestParam String file) {
        ReturnT<String> returnT;
        String[] addressList = address.split(",");
        if (projectService.deployZip(addressList, project, file)) {
            returnT = new ReturnT<>(200, "", "");
        } else {
            returnT = new ReturnT<>(500, "", "");
        }
        return JSON.toJSONString(returnT);
    }

    @GetMapping("/project/deploy/egg")
    String deployEgg(@RequestParam String address, @RequestParam String project, @RequestParam String file) {
        ReturnT<String> returnT;
        String[] addressList = address.split(",");
        if (projectService.deployEgg(addressList, project, file)) {
            returnT = new ReturnT<>(200, "", "");
        } else {
            returnT = new ReturnT<>(500, "", "");
        }
        return JSON.toJSONString(returnT);
    }

    @GetMapping("/project")
    String getAllProject() {
        List<Project> projects = projectService.findAllProject();
        ReturnT<List<Project>> returnT = new ReturnT<>(200, "", projects);
        return JSON.toJSONString(returnT);
    }

    @GetMapping("/open/project/{name}")
    String getAllProjectByName(@PathVariable(name = "name") String projectName) {
        List<Project> projects = projectService.findProjectByName(projectName);
        ReturnT<List<Project>> returnT = new ReturnT<>(200, "", projects);
        return JSON.toJSONString(returnT);
    }

    @GetMapping("/project/{index}/{size}")
    String getProject(@PathVariable(name = "index") int index, @PathVariable(name = "size") int size) {
        List<Project> projects = projectService.findAllPageable(index, size);
        ReturnT<List<Project>> returnT = new ReturnT<>(200, "", projects);
        return JSON.toJSONString(returnT);
    }

    @GetMapping("/project/count")
    String getProjectCount() {
        long count = projectService.findAllCount();
        ReturnT<Long> returnT = new ReturnT<>(200, "", count);
        return JSON.toJSONString(returnT);
    }

    @DeleteMapping("/project")
    String delProject(@RequestBody List<Project> projects) {
        ReturnT<String> returnT;
        if (projectService.delProjects(projects)) {
            returnT = new ReturnT<>(200, "", "");
        } else {
            returnT = new ReturnT<>(500, "", "");
        }
        return JSON.toJSONString(returnT);
    }

    @GetMapping("/project/nodeserver")
    String getProjectByNode(@RequestParam String nodeServer) {
        List<Project> projects = projectService.queryProjectByNodeServer(nodeServer);
        ReturnT<List<Project>> returnT = new ReturnT<>(200, "", projects);
        return JSON.toJSONString(returnT);
    }

    @GetMapping("/project/spider")
    String getSpidersOfProject(@RequestParam String nodeServer, @RequestParam String project) {
        List<String> spiders = projectService.querySpidersOfProject(nodeServer, project);
        ReturnT<List<String>> returnT = new ReturnT<>(200, "", spiders);
        return JSON.toJSONString(returnT);
    }
}
