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

import cn.reghao.bnt.web.devops.build.db.repository.RepoAuthConfigRepository;
import cn.reghao.bnt.web.devops.build.model.po.RepoAuthConfig;
import cn.reghao.bnt.web.devops.build.service.RepoAuthConfigService;
import cn.reghao.bnt.web.devops.builder.model.constant.RepoAuthType;
import cn.reghao.bnt.web.devops.builder.model.constant.RepoType;
import cn.reghao.bnt.web.util.SelectOption;
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/repoauth")
public class RepoAuthController {
    private final RepoAuthConfigRepository repoAuthConfigRepository;
    private final RepoAuthConfigService repoAuthConfigService;
    private final int pageSize = 10;

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

    @Operation(summary = "仓库认证列表页面", description = "N")
    @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public String repoAuthPage(@RequestParam("pn") int pageNumber) {
        PageRequest pageRequest = PageRequest.of(pageNumber-1, pageSize);
        Page<RepoAuthConfig> page = repoAuthConfigRepository.findAll(pageRequest);
        PageList<RepoAuthConfig> pageList = getPageList(page);
        return WebResult.success(pageList);
    }

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

    @Operation(summary = "仓库认证添加页面", description = "N")
    @GetMapping("/repo_types")
    public String addRepoAuthPage() {
        List<SelectOption> repoTypes = Arrays.stream(RepoType.values())
                .map(repoType -> new SelectOption(repoType.getName(), repoType.getName()))
                .collect(Collectors.toList());

        List<SelectOption> authTypes = Arrays.stream(RepoAuthType.values())
                .map(repoAuthType -> new SelectOption(repoAuthType.getName(), repoAuthType.getName()))
                .collect(Collectors.toList());

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

    @Operation(summary = "添加仓库认证配置", description = "N")
    @PostMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addOrUpdateRepoConfig(@Validated RepoAuthConfig repoAuth) {
        Result result = repoAuthConfigService.addOrUpdate(repoAuth);
        return WebResult.result(result);
    }

    @Operation(summary = "删除仓库认证配置", description = "N")
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteRepoConfig(Integer id) {
        Result result = repoAuthConfigService.delete(id);
        return WebResult.result(result);
    }
}
