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

import cn.reghao.bnt.common.msg.constant.PackType;
import cn.reghao.bnt.web.util.SelectOption;
import cn.reghao.bnt.web.devops.build.db.repository.DockerRegistryRepository;
import cn.reghao.bnt.web.devops.build.db.repository.PackerConfigRepository;
import cn.reghao.bnt.web.devops.build.model.po.PackerConfig;
import cn.reghao.bnt.web.devops.build.service.PackerConfigService;
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2021-09-17 10:31:15
 */
@Slf4j
@Tag(name = "应用打包配置页面")
@RestController
@RequestMapping("/api/devops/build/packer")
public class PackerController {
    private final PackerConfigRepository packerConfigRepository;
    private final DockerRegistryRepository dockerRegistryRepository;;
    private final PackerConfigService packerConfigService;
    private final int pageSize = 10;

    public PackerController(PackerConfigRepository packerConfigRepository,
                            DockerRegistryRepository dockerRegistryRepository,
                            PackerConfigService packerConfigService) {
        this.packerConfigRepository = packerConfigRepository;
        this.dockerRegistryRepository = dockerRegistryRepository;
        this.packerConfigService = packerConfigService;
    }

    @Operation(summary = "打包配置页面", description = "N")
    @GetMapping(value = "")
    public String packerPage(@RequestParam("pn") int pageNumber) {
        PageRequest pageRequest = PageRequest.of(pageNumber-1, pageSize);
        Page<PackerConfig> page = packerConfigRepository.findAll(pageRequest);
        PageList<PackerConfig> pageList = getPageList(page);
        return WebResult.success(pageList);
    }

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

    @Operation(summary = "打包类型列表", description = "N")
    @GetMapping("/pack_types")
    public String addPackerPage() {
        List<SelectOption> registryList = dockerRegistryRepository.findAll()
                .stream()
                .map(dockerRegistry -> {
                    Integer id = dockerRegistry.getId();
                    String registryUrl = dockerRegistry.getRegistryUrl();
                    return new SelectOption(registryUrl, String.valueOf(id));
                })
                .collect(Collectors.toList());

        List<SelectOption> packTypes = Arrays.stream(PackType.values())
                .map(packType -> new SelectOption(packType.name(), packType.name()))
                .collect(Collectors.toList());

        Map<String, Object> map = new HashMap<>();
        map.put("registryList", registryList);
        map.put("packTypes", packTypes);
        return WebResult.success(map);
    }

    @Operation(summary = "添加应用打包配置", description = "N")
    @PostMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addPackConfig(@Validated PackerConfig packerConfig) {
        Result result = packerConfigService.addOrUpdate(packerConfig);
        return WebResult.result(result);
    }

    @Operation(summary = "删除应用打包配置", description = "N")
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deletePackConfig(Integer id) {
        Result result = packerConfigService.delete(id);
        return WebResult.result(result);
    }
}
