package cn.reghao.bnt.web.devops.build.controller;

import cn.reghao.bnt.web.devops.build.db.repository.CompilerConfigRepository;
import cn.reghao.bnt.web.devops.build.model.po.CompilerConfig;
import cn.reghao.bnt.web.devops.build.service.CompilerConfigService;
import cn.reghao.bnt.web.devops.builder.model.constant.CompileType;
import cn.reghao.bnt.web.util.SelectOption;
import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.jutil.web.WebResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2021-09-17 10:31:15
 */
@Slf4j
@Tag(name = "应用编译配置页面")
@RestController
@RequestMapping("/api/devops/build/compiler")
public class CompilerController {
    private final CompilerConfigRepository compilerConfigRepository;
    private final CompilerConfigService compilerConfigService;
    private int pageSize = 10;

    public CompilerController(CompilerConfigRepository compilerConfigRepository, CompilerConfigService compilerConfigService) {
        this.compilerConfigRepository = compilerConfigRepository;
        this.compilerConfigService = compilerConfigService;
    }

    @Operation(summary = "编译配置列表页面", description = "N")
    @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public String compilerConfigPage(@RequestParam("pn") int pageNumber) {
        PageRequest pageRequest = PageRequest.of(pageNumber-1, pageSize);
        Page<CompilerConfig> page = compilerConfigRepository.findAll(pageRequest);
        PageList<CompilerConfig> pageList = getPageList(page);
        return WebResult.success(pageList);
    }

    private PageList<CompilerConfig> getPageList(Page<CompilerConfig> page) {
        int pageNumber = page.getNumber() + 1;
        int pageSize = page.getSize();
        long total = page.getTotalElements();
        List<CompilerConfig> list = page.getContent();
        return PageList.pageList(pageNumber, pageSize, (int) total, list);
    }

    @Operation(summary = "编译类型列表", description = "N")
    @GetMapping(value = "/types", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addCompilerPage() {
        List<SelectOption> compileTypes = Arrays.stream(CompileType.values())
                .map(compileType -> new SelectOption(compileType.getName(), compileType.getName()))
                .collect(Collectors.toList());
        return WebResult.success(compileTypes);
    }

    @Operation(summary = "docker 编译器映射目录页面", description = "N")
    @GetMapping(value = "/bind", produces = MediaType.APPLICATION_JSON_VALUE)
    public String compilerBindPage(@RequestParam("id") int id) {
        CompilerConfig compilerConfig = compilerConfigRepository.findById(id).orElse(null);
        if (compilerConfig != null && compilerConfig.getType().equals(CompileType.docker.getName())) {
            return WebResult.success(compilerConfig.getCompilerBinds());
        }

        return WebResult.failWithMsg("compiler not docker type");
    }

    @Operation(summary = "添加编译配置", description = "N")
    @PostMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String addCompileConfig(@Validated CompilerConfig compilerConfig) {
        Result result = compilerConfigService.addOrUpdate(compilerConfig);
        return WebResult.result(result);
    }

    @Operation(summary = "删除应用编译配置", description = "N")
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String deleteCompileConfig(Integer id) {
        Result result = compilerConfigService.delete(id);
        return WebResult.result(result);
    }
}
