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

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 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.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
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";
    }

    @ApiOperation(value = "编译更新页面", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping("/edit/{id}")
    public String editCompilerPage(@PathVariable("id") CompilerConfig compilerConfig, Model model) {
        model.addAttribute("compiler", compilerConfig);
        return "/devops/build/compiler/edit";
    }

    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 = "编译详情页面", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping("/detail/{id}")
    public String compilerDetailPage(@PathVariable("id") CompilerConfig compilerConfig, Model model) {
        model.addAttribute("compiler", compilerConfig);
        return "/devops/build/compiler/detail";
    }

    @ApiOperation(value = "编译器版本页面", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping("/version/{id}")
    public String compilerVersionPage(@PathVariable("id") int id, Model model) {
        String result = compilerConfigService.getCompilerVersion(id);
        model.addAttribute("content", result);
        return "/devops/build/compiler/version";
    }

    @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);
        if (compilerConfig != null && compilerConfig.getType().equals(CompileType.docker.getName())) {
            List<CompilerBind> list = compilerConfig.getCompilerBinds();
            model.addAttribute("list", list);
            return "/devops/build/compiler/bind";
        } else {
            String content = "not docker type";
            model.addAttribute("content", content);
            return "/devops/build/compiler/version";
        }
    }
}
