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

import cn.reghao.devops.mgr.build.db.repository.RepoAuthConfigRepository;
import cn.reghao.devops.mgr.build.model.po.RepoAuthConfig;
import cn.reghao.devops.common.util.KeyValue;
import cn.reghao.devops.mgr.build.service.RepoAuthConfigService;
import cn.reghao.devops.mgr.builder.model.constant.RepoAuthType;
import cn.reghao.devops.mgr.builder.model.constant.RepoType;
import cn.reghao.devops.mgr.util.PageSort;
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;

/**
 * @author reghao
 * @date 2021-09-17 10:31:15
 */
@Slf4j
@Api(tags = "仓库认证配置页面")
@Controller
@RequestMapping("/build")
public class RepoAuthPageController {
    private final RepoAuthConfigRepository repoAuthConfigRepository;
    private final RepoAuthConfigService repoAuthConfigService;

    public RepoAuthPageController(RepoAuthConfigRepository repoAuthConfigRepository,
                                  RepoAuthConfigService repoAuthConfigService) {
        this.repoAuthConfigRepository = repoAuthConfigRepository;
        this.repoAuthConfigService = repoAuthConfigService;
    }

    @ApiOperation(value = "仓库认证列表页面", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping("/repoauth")
    public String repoAuthPage(Model model) {
        PageRequest pageRequest = PageSort.pageRequest();
        Page<RepoAuthConfig> page = repoAuthConfigRepository.findAll(pageRequest);

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

    @ApiOperation(value = "仓库认证添加页面", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping("/repoauth/add")
    public String addRepoAuthPage(Model model) {
        setRepoAuthModel(model);
        return "/devops/build/repoauth/add";
    }

    private void setRepoAuthModel(Model model) {
        List<KeyValue> repos = new ArrayList<>();
        for (RepoType repoType : RepoType.values()) {
            repos.add(new KeyValue(repoType.name(), repoType.name()));
        }

        List<KeyValue> auths = new ArrayList<>();
        for (RepoAuthType authType : RepoAuthType.values()) {
            auths.add(new KeyValue(authType.name(), authType.name()));
        }

        model.addAttribute("repos", repos);
        model.addAttribute("auths", auths);
    }

    @ApiOperation(value = "添加仓库认证配置", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PostMapping(value = "/repoauth", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String addOrUpdateRepoConfig(@Validated RepoAuthConfig repoAuth) {
        Result result = repoAuthConfigService.addOrUpdate(repoAuth);
        return WebResult.result(result);
    }

    @ApiOperation(value = "删除仓库认证配置", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @DeleteMapping(value = "/repoauth/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String deleteRepoConfig(@PathVariable("id") int id) {
        Result result = repoAuthConfigService.delete(id);
        return WebResult.result(result);
    }
}
