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

import cn.reghao.devops.mgr.build.db.repository.CompilerConfigRepository;
import cn.reghao.devops.mgr.build.model.po.CompilerBind;
import cn.reghao.devops.mgr.build.model.po.CompilerConfig;
import cn.reghao.devops.common.util.KeyValue;
import cn.reghao.devops.mgr.builder.model.constant.CompileType;
import cn.reghao.devops.mgr.build.service.CompilerConfigService;
import cn.reghao.devops.mgr.util.PageSort;
import cn.reghao.devops.common.machine.Machine;
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.Collections;
import java.util.List;

/**
 * @author reghao
 * @date 2021-09-17 10:31:15
 */
@Slf4j
@Api(tags = "应用编译配置页面")
@Controller
@RequestMapping("/build/compiler")
public class CompilerPageController {
    private final CompilerConfigRepository compilerConfigRepository;
    private final CompilerConfigService compilerConfigService;

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

    @ApiOperation(value = "编译配置列表页面", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping
    public String compilerConfigPage(Model model) {
        PageRequest pageRequest = PageSort.pageRequest();
        Page<CompilerConfig> page = compilerConfigRepository.findAll(pageRequest);

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

    @ApiOperation(value = "编译添加页面", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping("/add")
    public String addCompilerPage(Model model) {
        setCompilerModel(model);
        return "/devops/build/compiler/add";
    }

    private void setCompilerModel(Model model) {
        List<KeyValue> compileTypes = new ArrayList<>();
        for (CompileType compileType : CompileType.values()) {
            compileTypes.add(new KeyValue(compileType.name(), compileType.name()));
        }
        model.addAttribute("compileTypes", compileTypes);
        model.addAttribute("machineId", Machine.ID);
        model.addAttribute("machineIpv4", Machine.IPV4);
    }

    @ApiOperation(value = "docker 编译器映射目录页面", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping("/bind/{id}")
    public String compilerBindPage(@PathVariable("id") int id, Model model) {
        CompilerConfig compilerConfig = compilerConfigRepository.findById(id).orElse(null);
        boolean dockerType = true;
        List<CompilerBind> list;
        if (compilerConfig != null && compilerConfig.getType().equals(CompileType.docker.getName())) {
            list = compilerConfig.getCompilerBinds();
        } else {
            dockerType = false;
            list = new ArrayList<>();
        }

        model.addAttribute("dockerType", dockerType);
        model.addAttribute("list", list);
        return "/devops/build/compiler/bind";
    }

    @ApiOperation(value = "添加编译配置", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String addOrUpdateCompileConfig(@Validated CompilerConfig compilerConfig) {
        Result result = compilerConfigService.addOrUpdate(compilerConfig);
        return WebResult.result(result);
    }

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