package cn.reghao.devops.mgr.build.controller;

import cn.reghao.devops.mgr.build.db.repository.DockerRegistryRepository;
import cn.reghao.devops.mgr.build.db.repository.PackerConfigRepository;
import cn.reghao.devops.mgr.build.model.po.PackerConfig;
import cn.reghao.devops.common.util.KeyValue;
import cn.reghao.devops.mgr.build.service.PackerConfigService;
import cn.reghao.devops.mgr.util.PageSort;
import cn.reghao.devops.common.machine.Machine;
import cn.reghao.devops.common.msg.constant.PackType;
import cn.reghao.jutil.jdk.result.Result;
import cn.reghao.jutil.web.WebResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author reghao
 * @date 2021-09-17 10:31:15
 */
@Slf4j
@Api(tags = "应用打包配置页面")
@Controller
@RequestMapping("/build/packer")
public class PackerPageController {
    private final PackerConfigRepository packerConfigRepository;
    private final DockerRegistryRepository dockerRegistryRepository;;
    private final PackerConfigService packerConfigService;

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

    @ApiOperation(value = "打包配置页面", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping
    public String packerPage(Model model) {
        PageRequest pageRequest = PageSort.pageRequest();
        Page<PackerConfig> page = packerConfigRepository.findAll(pageRequest);

        model.addAttribute("page", page);
        model.addAttribute("list", page.getContent());
        return "/devops/build/packer/index";
    }

    @ApiOperation(value = "打包添加页面", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping("/add")
    public String addPackerPage(Model model) {
        List<KeyValue> registryList = dockerRegistryRepository.findAll()
                .stream()
                .map(dockerRegistry -> {
                    Integer id = dockerRegistry.getId();
                    String registryUrl = dockerRegistry.getRegistryUrl();
                    return new KeyValue(String.valueOf(id), registryUrl);
                })
                .collect(Collectors.toList());

        setPackerModel(model);
        model.addAttribute("registryList", registryList);
        return "/devops/build/packer/add";
    }

    private void setPackerModel(Model model) {
        List<KeyValue> packTypes = new ArrayList<>();
        for (PackType packType : PackType.values()) {
            packTypes.add(new KeyValue(packType.name(), packType.name()));
        }
        model.addAttribute("packTypes", packTypes);
        model.addAttribute("machineId", Machine.ID);
        model.addAttribute("machineIpv4", Machine.IPV4);
    }

    @ApiOperation(value = "添加应用打包配置", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String addOrUpdatePackConfig(@Validated PackerConfig packerConfig) {
        Result result = packerConfigService.addOrUpdate(packerConfig);
        return WebResult.result(result);
    }

    @ApiOperation(value = "删除应用打包配置", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @DeleteMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String deletePackConfig(@PathVariable("id")  int id) {
        Result result = packerConfigService.delete(id);
        return WebResult.result(result);
    }
}
